当前位置:网站首页>File operation command

File operation command

2022-07-07 12:56:00 LC181119

One 、 File operations

1. View the current working directory

Every shell And system processes have a current working directory CWDcurrent work directory
Show the current shell CWD The absolute path of
pwd command : printing working directory
  •   -P Show real physical path
  •   -L Show link path ( Default )

2. Absolute and relative paths

  • Absolute path                
         With a forward slash / The root directory starts
         Full file location path
         Can be used whenever you want to specify a filename
  • Relative paths
         Don't start with a slash
         In general , The path relative to the current working directory , In a special situation , It's a location relative to a directory
         You can specify a filename as a short form
Base name :basename, Just take the file name instead of the path
Directory name :dirname, Path only , Don't use the file name

3. Change directory

command cd change directory Change the directory
Options :-P Switch to physical path , Instead of the softlink Directory
You can use absolute or relative paths
  • Switch to the parent directory : cd ..
  • Switch to the current user's home directory : cd
  • Switch to the previous working directory : cd -

4. List the contents of the catalog

ls The command can list the contents of the current directory or specify a directory
usage :
ls [options] [files_or_dirs]
Common options :
  • -a Contains hidden files
  • -l Show additional information
  • -R Directory recursion
  • -ld Catalog and symlink information
  • -1 The document line shows
  • -S Sort from big to small
  • -t Press mtime Sort
  • -u coordination -t Options , Show and press atime Sort from new to old
  • -U Display... In the order in which the contents are stored
  • -X Sort by file suffix
  • -F Attach different symbols when displaying different types of files :*/=>@|
  • -C Documents are long , Display files in multiple columns , The default is a column ( standard output )
explain :
        ls The color when viewing files with different suffixes is determined by /etc/DIR_COLORS and @LS_COLORS Variable definitions
        ls -l See the size of the file , It's not necessarily the size of the space actually occupied by the actual file

5. View file status stat

Document related information :metadata, data
Each file has three timestamps :
  • access time Access time ,atime, Read file contents
  • modify time Modification time ,mtime, Change file content ( data )
  • change time Change time ,ctime, Metadata changed

6. Determine the contents of the document

Files can contain multiple types of data , Use file Command to check the type of file , Then determine the appropriate open command or application to use
Format
file [options] <filename>...
Common options :
  • -b When listing file identification results , Do not show file name
  • -f filelist Lists the files filelist The file type of the file name in
  • -F Replace the output file name with the specified separator ”:” Separator
  • -L View the file type of the corresponding soft link
  • --help Display command online help

7. File wildcard pattern wildcard pattern

File wildcards can be used to match multiple files that meet the criteria , It is convenient to manage files in batches
The wildcard has a specific symbol , Express a specific meaning , This special symbol is called yuan meta character
Common wildcards are as follows :
*  Matches zero or more characters , But don't match  "."  Opening file , That is, hidden files 
?  Match any single character , A Chinese character is also a character 
~  Current user home directory 
~mage  user mage Home directory 
.  and  ~+  Current working directory 
~-    Previous working directory 
[0-9]  Match number range 
[a-z]  A letter 
[A-Z]  A letter 
[wang]  Match any character in the list 
[^wang]  Match all characters in the list except for 
[^a-z]  Match all characters in the list except for 

[:digit:]: Arbitrary number , amount to 0-9
[:lower:]: Any lowercase letter , Express  a-z
[:upper:]:  Any capital letter , Express  A-Z 
[:alpha:]:  Any upper and lower case letters 
[:alnum:]: Any number or letter 
[:blank:]: Horizontal white space characters 
[:space:]: Horizontal or vertical white space characters 
[:punct:]: Punctuation 
[:print:]: Printable characters 
[:cntrl:]: control ( unprintable ) character 
[:graph:]: Graphic characters 
[:xdigit:]: Hexadecimal characters 

8. Create empty files and refresh time

touch Command can be used to create an empty file or refresh the file time
Format :
touch [OPTION]... FILE...
Option description :
  • -a Just change atime and ctime
  • -m Just change mtime and ctime
  • -t [[CC]YY]MMDDhhmm[.ss] Appoint atime and mtime The timestamp
  • -c If the file doesn't exist , Do not create

9. Copy files and directories

utilize cpcopy) Commands can be used to copy files or directories
Format
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
Common options
  • -i If the goal already exists , Prompt whether to cover before covering
  • -n No coverage , Pay attention to the order of the two
  • -r, -R Recursively copies directories and everything inside
  • -a file , amount to -dR --preserv=all, Commonly used for backup functions
  • -d --no-dereference --preserv=links Do not copy the original file , Copy only the link name
  • --preserv[=ATTR_LIST]

                mode: jurisdiction

                ownership: Belong to the main genus group

                timestamp:

                links

                xattr

                context

                all

  • -p equivalent --preserv=mode,ownership,timestamp
  • -v --verbose
  • -f --force
  • -u --update Copy only files that the source updates or the destination does not exist
  • -b Goals exist , Backup before overwriting , The default form is filename~ , Keep only the most recent backup

10. Move and rename files

        mv Commands can be used to move and rename files or directories
         Move data in the same partition , fast : There's no change in the location of the data
         Different partitions move data , It's relatively slow : The location of the data has changed
         Format :
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
Common options :
  • -i Interactive
  • -f mandatory
  • -b Goals exist , Backup before overwriting
utilize rename You can batch modify the file name
Format :
rename [options] <expression> <replacement> <file>...

11. Delete file

Use rm The command can delete files
Be careful : This command is very dangerous , Careful use , It is recommended to use mv Instead of rm
Format :
rm [OPTION]... FILE...
Common options :
  • -i Interactive
  • -f Mandatory deletion
  • -r recursive
  • --no-preserve-root Delete /
rm Although the file was deleted , But deleted files can still be recovered , In scenarios with high security requirements , have access to shred Safely delete files
shred Format
shred [OPTION]... FILE...
Common options :
  • -z   Last overlay add 0, To hide the override operation
  • -v Able to display operation progress
  • -u Truncate and delete files after overwriting
  • -n # Specify the number of times to overwrite the contents of the file ( The default value is 3 Time )

Two 、 Directory operation

1. Show tree tree

Common options :
  • -d: Show only directories
  • -L level: Specify the number of levels to display
  • -P pattern: Only display specified by wild-card pattern Match the path to

2. Create directory mkdir

Common options :
  • -p: There is no error reporting , And can automatically create the required directories
  • -v: Show details
  • -m MODE: When creating a directory, you can directly specify permissions

3. Delete empty directory rmdir

Common options :
  • -p Recursively delete the parent empty directory
  • -v Show details

Be careful :rmdir Only empty directories can be deleted , If you want to delete a non empty directory , have access to rm -r command , Delete tree recursively

原网站

版权声明
本文为[LC181119]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130616434381.html