Command learning
Current directory
View the current working directory -pwd( Absolute path )
pwd The command displays the current working directory location of the user , The working directory is the default location for users to operate files or other subdirectories .
As shown in the figure :
Switch the user's working directory (~ Represents the current user's home directory )
- cd Target location : Switch to the target location
- cd ~ or cd: If the target location is not specified , Switch to the host directory of the current user
- cd -: Switch to the last execution cd The directory before the command
- Absolute path : The path starting from the root directory , No matter which path you are in, you can accurately switch to the specified directory
- Relative paths : The path starting from the current directory
Relative path representation
form | explain | Example |
---|---|---|
Use directory name or file name directly | Represents a subdirectory in the current working directory 、 The location of the file | cd groub2/ |
With a dot “.” start | Indicates that the current working directory is used as the starting point | cd ./grub.conf |
With two dots “. .” start | Represents the directory above the current directory ( Parent directory ) As a starting point | . ./vmlinuz |
With “~ user name ” Form begins | Indicates starting from the specified user's host directory | ~teacher |
With “~” Form begins | Indicates that starting from the current user's Host Directory | ~ |
The list shows the contents of the catalog -ls
ls Common options for
- -l
In long format (Long) Show a list of files and directories , Including authority 、 size 、 Last update time and other details .ll Command and ls-l Function the same - -a
Show all (All) Information about subdirectories and files , Include the name with a point number "." Hidden directories and hidden files at the beginning . - -A
And -a The function of options is basically similar to , But there are two special hidden directories that won't be displayed , That is to say, the ".“ And... Representing the parent directory ". ." - -d
Show directory (Directory) Its own attributes , Instead of showing the contents of the catalog . - -h
To be more human (Human) Displays the size of a directory or file in the same way , The default size is in bytes (B) , Use -h Option will be displayed as KB、MB Equal unit . This option needs to be combined with -l Use options together . - -R
By recursion (Recursive) Displays all the contents of the specified directory and its subdirectories - –color
Color different files in character mode . The default is on .
Usually , Dark blue means the directory ,, White means general document , Green means executable file , Yellow means device file , Red means compressed file , Sky blue indicates soft connection file - -i
Showing files and directories inode Number
Add : use ls -l Command to view a directory will get a 7 A list of fields
-
The first 1 Field : File property word
The first character represents the type of file
"-” Indicates that the file is a normal file
"d” Indicates that the file is a directory
“l” Indicates that the file is a soft link file . Be similar to windows Next shortcut
The first 1 After the character 9 A letter indicates the permission bit of the file or directory :
r The watch is read ( see 、 download )、w Said to write ( add to 、 change 、 Delete 、 Upload )、x Indicates execution ( Run the file 、 Toggle directory )
The first three represent the file owner ( Belong to ) Authority , The middle three represent the permissions of the group to which the file belongs , The last three represent the permissions of other users . -
The first 2 Field : Number of file hard links
If a file is not a directory , This field indicates the number of hard links the file has
If it's a directory , Is the first 2 Field represents the number of subdirectories in the directory .
If you create an empty directory , The second field of this directory is 2, Indicates that there are two subdirectories under the directory .
- The first 3 Field : file \ Directory owner
- The first 4 Field : file \ The group of the directory owner
- The first 5 Field : The space occupied by the file ( Soft recognition is in bytes ) If it's a directory , Indicates the size of the directory , Not the total size of the directory and the files under it .Linux Everything in the system is documented , So a directory is also a kind of file .
- The first 6 Field : file \ Last access or modification time of the directory
- The first 7 Field : file name , If it's a symbolic link ( Soft link file ), Then there will be a “->” The arrow symbol , The next root is the file name it points to
wildcard
Combining wildcards
“?”: Represents any character , There is one character
“*”: Represents any number of characters , It can be 0 A or 1 One or more
“[]”: Indicates that any character in a character group can be matched [abc]、[a-f]
Set alias -alias
- Simplify the common 、 A longer order
- Remove alias
Statistics directory and file space occupancy -du
- Count the specified directory ( Or document ) The amount of disk space occupied
- Common options
- -a : Include all files when calculating disk space usage , It's not just catalogues
- -h : In a more humane way ( Default to KB Count , But it doesn't show units ) Show the statistics
- -s : Only count the total size of the occupied space , Instead of counting every subdirectory 、 File size
- a key :–max-depth=n —— Count all less than or equal to n The total size of the directory of the layer
In the production environment, the system disk is occupied. To find the cause of the exception, you need to find the directory layer by layer
for example : According to the statistics /var/log The size of the space occupied by the directory
notes : Options -a and -s Do not use at the same time
Create a new directory mkdir
- Create a new empty directory
- Add multiple directories at a time and directly add (mkdir abc 123)
- -p: Create nested multiple directories at once
for example : Create a file in the current directory called public_html A subdirectory
for example : Create directory /aa, stay /aa Create subdirectories under the directory bb, stay /aa/bb Create subdirectories under the directory cc
Create an empty file touch
- Update the time stamp of the file
- Often used to create multiple new empty files
touch file …
for example :
Create two empty files
The file names are aa.txt and bb.txt:
Create two different suffixes with the same file name
Create link file In
- Create link file -In
Create a linked file for a file or directory , Be similar to Windows System shortcuts
Link file type
- Soft link ( Also known as symbolic links ln -s)
When establishing a soft connection, the source file needs to be represented by an absolute path . The source file location remains unchanged , Soft link file locations are useful wherever they are , Changing the file location relative to the path will invalidate
example :ln -s Source file or directory … Linked file or target location
- Hard links ( Only to understand )
ln Source file or directory … Linked file or target location
The source file is represented as an absolute path , Hard links are relative to creating an alias file , The source file is still useful after deletion
Copy files or directories cp
Files or directories that will need to be copied ( Source ) Rebuild a , And save it as a new file or directory
- cp [ Options ]… Source file or directory … Target file or directory …
- cp Common options
- -f: Do not remind when overwriting a file or directory with the same name as the target , And direct forced replication
- -i: Remind the user to confirm when overwriting a file or directory with the same name as the target
- -p: Permission to save files when copying 、 Attributes such as owner and time stamp remain unchanged
- -r: This option must be used when copying directories , Means to copy all files and subdirectories recursively
notes : When copying multiple files or directories , The target location must be a directory , And the target directory must already exist .
Delete files or directories rm
- rm [ Options ] Files or directories to delete …
- Common options
- -f: Do not remind when deleting files or directories , And directly force deletion .
- -i: Remind users to confirm when deleting files or directories .(y Said to delete ,n Means not to delete )
- -r: This option must be used when deleting directories , Means to delete the entire directory tree recursively ( Use with caution ).
notes : Do not directly delete existing directories or configuration files in the system , To avoid unexpected failures
When deleting files in the directory, it is recommended to switch to the directory first and then execute rm -rf command
Move files or directories mv
- Specify file or directory transfer location
- If the destination location is the same as the source location , It is equivalent to performing rename operation
- mv The command can only rename a single file , You can use rename Command to batch modify the file name
Batch modify file name :rename
mv The command can only rename a single file , You can use rename Batch modify file name
Find the directory where the command file is stored which
Use which When looking for internal commands , The corresponding program will not be found
Find a file or directory find
- Recursively , According to the name of the target 、 type 、 Size and other attributes for fine search
- Common types of search criteria
Find type | keyword | explain |
---|---|---|
Search by name | -name | Search by the name of the target file ( Name and “ ” Double quotes , Allow to use “*” And “?” wildcard |
Check by file size | -size | Search based on the size of the target file , In general use “+”、“-” Number is set to be greater than or less than the specified size as the search condition , Common units of capacity include kB( Be careful k It's lowercase )、MB、GB |
Search by file owner | -user | Search according to whether the file belongs to the target user |
Find by file type | -type | Find according to the type of file , File types include ordinary files (f)、 Catalog (d)、 Block device file (b): A device that reads data in chunks ( Hard disk , Memory , CD drive ) Character device file (c) A device that reads data as a single character ( keyboard , mouse ) |
- Use find Command to implement multiple search conditions
- Use logical operators between expressions
- “-a” And (and) ### It means that you need to meet
- “-o” Express or ( or) ### Indicates that only one of the
The experiment is shown in the figure below :
- find And exec usage
- -exec Parameters are followed by Linux command , It's a semicolon ";" For the end sign , Because semicolons have different meanings in different systems , So add the backslash escape character before the semicolon “\”.
{} For the front find Find out the file name . - find The command matches all the normal files in the current directory , And in -exec Use... In the options ls-l The command lists them
Priority of executing commands
- First priority
Command to specify the path . Absolute path /root/pwd.sh Or relative path ./pwd.sh - Second priority
Alias specified command alias pwd=/root/pwd.sh - Third priority
Internal orders - Fourth priority
hash command .
Linux There will be one under the system hash surface , When you first turned it on, this hash Table is empty , Every time you execute an order , hash The table records the path of the command , It's like caching . For the first time shell By default, the interpreter will start from PATH Path to find the path of the command , The second time you use this command ,shell The interpreter first looks at hash surface , Without that order, I would go PATH Find... Under the path .hash Tables increase the speed of command calls . - Fifth priority
adopt PATH Defined find order find - If none of the above can be found , Will report “ Command not found …” Error of