Meaning of different file colors
Color modification:[root@fyr ~]# ls --color=no / / turn off color [root@fyr ~]# ls --color=yes / / open color
- Black: ordinary file (e.g. txt)
- Blue: folder
- Sky blue: linked files
- Green: executable
- Red: compressed file
- Yellow: block device files (e.g. hard disk, U SB flash disk)
Expansion:
- -Ordinary file
- d directory file directory
- b device file block
- p pipe file pipe
- s socket file socket
- l symbolic link file link
1. Basic operation of directory
1.1 view current directory - pwd
pwd command is the abbreviation of print working directory.
[root@fyr home]# pwd / / view the current path directory /home [root@fyr home]# cd /bin [root@fyr bin]# ll /bin lrwxrwxrwx. 1 root root 7 1 April 4:37 /bin -> usr/bin [root@fyr bin]# pwd -P /usr/bin #-P displays the true path
1.2 switching working directory - cd
Change the user's working directory to another location
command | effect |
---|---|
cd cd ~ | If the target location is not specified, switch to the home directory |
cd - | Switch to the directory where the last operation was performed |
cd . . | Switch to the previous directory (parent directory) |
ls . | View all files and folders in this directory (display the files in the current directory) |
cd . | Indicates the current directory (current level directory) |
[root@fyr /]# cd /home/ [root@fyr home]# cd /mnt/ [root@fyr mnt]# cd - [root@fyr home]# cd . [root@fyr home]# cd .. [root@fyr /]# ls . [root@fyr /]# cd [root@fyr ~]# cd ~ [root@fyr ~]#
For example:
1.2.1 manifestation of relative path
Form of expression | effect |
---|---|
Use directory name or file name directly | Indicates the subdirectory of the current working directory and the location of files |
A dot "." start | Indicates that the current working directory is used as the starting point (the one that starts with the dot is) |
Two periods begin with "..." | Indicates that the parent directory (parent directory) of the current directory is used as the starting point |
Begin with "~ user name" | Indicates that the starting point is the host directory (home directory) of the specified user |
cd (relative path, absolute path)
command | explain |
---|---|
cd/home | Followed by the absolute path, switch to the home directory |
cd./home | Followed by the relative path, switch to the home directory |
cd ~user | Switch to a user's home directory. Only root can do it. Other users can only do it at their own home |
1.3 view files in the current directory - ls
ls command is the abbreviation of list files. Chinese Translation: display the files and directories in the current directory.
Format: ls [option] [File or directory...]
-l | The file information attributes are listed in detail. Usually, ll is used directly because the alias is set |
---|---|
-h | To display the size of a directory or file in a more Human way, this option needs to be used in conjunction with the - l option |
-R | Recursive display. For example, if there are files in a directory, list that file together |
-d | View the information of the directory itself, and do not display the contents under the directory |
-i | Display inode value |
-a | Traverse All files and directories, and display the information of All (All) subdirectories and files, including "." and "." “ |
-A | The function is basically similar to the - a option, but there are two special ones. The hidden directory will not be displayed, "." and "." |
- LS - L: displays a list of files and directories in Long format, including details such as permissions, size, last update time, etc. ll=ls -l
- ls -h: displays the size of directories or files in a more Human way. This option needs to be used in combination with the - l option
- ls -R: recursive display. For example, if there are files in a directory, list that file together
- ls -d: view the information of the directory itself without displaying the contents under the directory|
- ls -i: display inode value
- ls -a: displays information about All (All) subdirectories and files, including "." and "." “
- ls -A: the function is basically similar to that of the - a option, but there are two special ones. The hidden directory will not be displayed, "." and "." “
1.4 wildcards
Symbol | effect |
---|---|
? | Match one character f txt |
* | Matches all non hidden characters, no matter how long or short, but does not match "." The first file is the hidden file |
{1...10} | 1 to 10 |
{a...z} | a b c ...z |
{A...Z} | A B C ...Z |
[123] | It doesn't matter if you take one of them |
[a-z] | Scope needs attention |
[0-9] | Matching number range (0123456789) |
\ | Escape character, indicating the original meaning |
[[:lower:]] | Lowercase letters indicate a-z |
[[:upper:]] | Capital letters indicate A-Z |
[^zhou] | Matches characters other than all characters in the list |
[[:digit:]] | Any number, equivalent to 0-9 |
give an example:
1.5 statistics of directory and file space occupation - du
Format: du [option] [File or directory ]
du [options] [file or directory...]
option | explain |
---|---|
-a | When counting the disk space occupation, all files are included, but only directories are counted |
-h | Display the statistical results in a more user-friendly way (counting in KB by default, but not in units) |
-s | Only the total size of the occupied space is counted, not the size of each subdirectory and file |
–max-depth=n | Maximum display to n th layer |
-s stands for summary, - h stands for humanized display in KB, MB, GB and TB formats.
Note that a and s cannot be used at the same time
For example:
[root@fyr ~]# du -a
For example:
[root@fyr ~]# du -h
For example:
[root@fyr ~]# du -s [root@fyr ~]# du -sh
Supplement: it is a very practical function. The production environment is often used in this way to count and display the size of all files in the current directory. It is often used when the disk water level is too high.
[root@fyr ~]# du -sh *
2. Basic operation of documents
-
The maximum length of the file name is 255 bytes
-
The maximum length of file name including path is 4095 bytes
-
All characters are valid except slashes and NULL. However, directory names and files with special characters are not recommended. Some characters need to be quoted in quotation marks.
2.1 create a new directory - mkdir
Format: mkdir [option] Directory location and name
[root@fyr home]# mkdir a / / create a single folder [root@fyr home]# mkdir b c d / / create multiple folders [root@fyr home]# ls a b c d
For example:
Install tree command:
[root@fyr home]# yum install tree -y
-p create directories recursively (create nested multi-level directories at one time)
Create directory/aa,stay/aa Create subdirectories under directory bb,stay/aa/bb Create subdirectories under directory cc [root@fyr home]# mkdir -p a/aa/bb/cc / / create folder recursively #Recursive creation no folders will be automatically created recursively
For example:
View the created directory
[root@fyr home]# ls a / / view the first directory created [root@fyr home]# ls a/aa / / view the second directory created [root@fyr home]# tree a / / view all directories created
For example:
2.2 create an empty file - touch
Format: touch file...
touch (three refresh times)
If no new empty file exists
- touch new file... (can be empty)
- touch will not destroy the refresh time of existing files
Each file has three timestamps:
- Access time, access time, atime, read file content
- modify time, mtime, change file content (data)
- change time, ctime, metadata changes
- Create a new file, or an empty new file
[root@fyr home]# touch ky18 ky19 [root@fyr home]# ls
For example:
[root@localhost data]# echo "" > 4 //#Create 4 empty files [root@localhost data]# ls 1 2 3 4
For example:
- View three times
[root@localhost bin]# stat /etc/passwd
For example:
Refresh three times
[root@localhost bin]# stat /etc/passwd
For example:
2.2.1 three ways to create an empty file (summary)
[root@fyr home]# touch 1.txt [root@fyr home]# echo " " > 3.txt [root@fyr home]# vim 2.txt
For example:
2.3 create link file - ln (link)
Format: ln [-s] Source file or directory... Linked file or destination
Case 1: create soft connection (plus - s)
The source file cannot be accessed after being deleted, and it can still be accessed after creating a new file with the same name [root@fyr home]# echo "this is kgc" > demo.txt [root@fyr home]# cat demo.txt [root@fyr home]# ln -s demo.txt test.txt [root@fyr home]# rm -rf demo.txt [root@fyr home]# cat test.txt [root@fyr home]# rm -rf test.txt
For example:
Case 2: creating hard links
Delete the hard link to continue accessing the hard link [root@fyr home]# echo "this is kgc" > demo.txt [root@fyr home]# cat demo.txt [root@fyr home]# ln demo.txt test2.txt [root@fyr home]# rm -rf demo.txt [root@fyr home]# cat test2.txt
Case 3: hard connection cannot establish connection across partitions, while soft connection can
Hard link: cannot establish a connection across partitions. Cannot create a folder Soft link: it can cross devices and support folders (Note: relative path and absolute path, absolute path is recommended) [root@fyr home]# ln test2.txt /boot/demo.txt [root@fyr home]# ln -s test2.txt /boot/demo.txt [root@fyr home]# ls -l /boot/demo.txt
################Practical application#################### Software version upgrade iteration Developed app1.0 Make soft connection app If so app2.0 Just connect the soft connection app Delete new app2.0 Soft connection app $PATH The system will not recognize the software installed by itself in the future httpd mysql ls 1 Put program files into $PATH route usr/bin 2 Make soft connection $PATH 3 modify $PATH route
- Comparison between soft connection and hard connection
Comparison item | Hard connection | Soft connection |
---|---|---|
essence | Is essentially the same file | Not the same file in essence |
inode | identical | Different |
Number of connections | Create new hard links, the number of links will increase, delete hard links, the number of links will decrease | Delete new does not change |
folder | I won't support it | support |
Delete source file | Only the number of links is reduced by one, but the access of linked files is not affected | Unable to access the connection file |
file type | Same as source file | Linked file, independent of the source file |
file size | Same as source file | The length of the path to the source file |
2.4 copy - cp (copy backup)
cp (it has the function of renaming, and the results of the same command three times are different)
atime ctime mtime time stamp
Format: cp [option]... Source file destination //Equivalent to renaming and copying (file equivalent to changing name and saving as) cp [option]... source file... folder Multiple source files can only be copied to folders //Rebuild a copy of the file or directory (source) to be copied and save it as a new file or directory cp[option]... Source file or directory... Target file or directory...
option | explain |
---|---|
-f | When overwriting a file or directory with the same name as the target, you will not be prompted, but will directly force the copy |
-i | Remind the user to confirm when overwriting the file or directory with the same name as the target (i has priority over f) |
-p | Keep the permission, owner, time stamp and other attributes of the source file unchanged during copying (permission) |
-r | This option must be used when copying directories, which means that all files and subdirectories are copied recursively |
-a | Keep all information completely (all soft and hard connections) |
-v | visualization |
-u | Equivalent to adding backup and differential backup (only works for folders) |
-b | Back up first (keep only the most recent) |
[root@localhost zhangsan]# cp -a zhansan /data/ #Retain all permissions, including soft connect files [root@localhost data]# cp -r /etc/ /opt/ #Copy folder with -r no copy [root@localhost data]# cp -r /etc/ /opt/etc.bak #Change name [root@localhost data]# cp -r /etc/ /opt/etc.bak #Become a subdirectory of etc.bak [root@localhost data]# cp -r /etc/ /opt/etc.bak #You will be asked whether you want to overwrite it. cp is an alias plus \ cp direct overwrite [root@localhost data]# cp /etc/passwd /etc/shadow /opt [root@localhost data]# cp /etc/passwd /etc/shadow /opt/f111.txt #Copying multiple files must be a directory, not a file, and the directory must exist. cp: target"/opt/f111.txt" Not a directory
2.5 moving and renaming - mv
If the destination location is the same as the source location, it is equivalent to renaming Moving to the current directory is renaming, moving to a different directory is cutting (similar) Format: mv [option] ... Source file or directory... Destination file or directory single file
For example:
2.5.1 batch rename
Format: rename Old character new character target file
For example:
[root@fyr opt]# rename txt back *.txt
2.6 deleting files or directories - rm
Format: rm [option] File or directory to delete //Deletes the specified file or directory
Common usage: rm -rf/* (current root directory, do not execute)
Delete empty directory: rmdir
-i | Remind users to confirm when deleting files or directories (Interactive (prompt)) |
---|---|
-r | This option must be used when making and removing directories, which means that the entire directory tree is deleted recursively (it should be used with caution). Recursion (required to delete directory) |
-f | When deleting a file or directory, it is forced to delete directly without reminding (forced) |
Case 1:
[root@fyr home]# rm -f 1.txt [root@fyr home]# rm -i 2.txt [root@fyr home]# rm fyr [root@fyr home]# rm -r fyr
[root@localhost ~]# rm -rf / #No execution [root@localhost ~]# echo * #View after deleting root [root@localhost ~]# rm -rf /data/* [root@localhost ~]# rm -rf /data /* ##The difference between misoperation is equal to deleting the root
2.7 find file or directory - find
Features of the command:
- Exact search
- Real time search
- There are many search conditions supported
The recursive method is adopted to carry out fine search according to different attributes such as the name, type and size of the target Format: find [OPTION]... [Find path] [Search criteria] [Processing action] find Find the directory location of a file or subdirectory Find condition type 1 Find path: specify the specific target path; Do not specify default to current directory 2 Search criteria: you can search the file name, size, type, permission and other criteria; The default is to find all files in the specified path 3 Processing action: operate the qualified files and output them to the screen by default( print)
Common options
Find type | keyword | explain |
---|---|---|
Find by name | -name | Search according to the name of the target file, and "*" and "? Are allowed wildcard |
Find by file size | -size | The search is based on the size of the target file. Generally, the "+" and "-" signs are set to be greater than or less than the specified size as the search criteria. The commonly used capacity units include kB (note that k is lowercase), MB and GB |
Find by master file | -user | Find according to whether the file belongs to the target user |
Find by file type | -type | Search according to the file type. The file types include ordinary file (f), directory (d), block device file (b), character device file (c), etc |
Maximum search directory depth | -maxdepth level | Find your file in a graded form |
Minimum search directory depth | -mindepth level | ditto |
- Logical operators are used between expressions
"- a" means and (and)
"- o" indicates or (or)
Example 1: - name
[root@fyr opt]# find /etc/ -name passwd [root@fyr opt]# find /etc/ -name *.conf [root@fyr home]# find /etc/ -name "resolv*.conf"
Example 2: - size
[root@fyr opt]# find /etc/ -size +4M [root@fyr opt]# find /etc/setuptool.d -size -4M [root@fyr opt]# find /etc/ -size 4M [root@localhost boot]# find ./ -name "vm*" #Find the beginning of vm [root@localhost boot]# find /boot/ -size -10M -a -name "*img" #img files smaller than 10M
[root@localhost etc]# find /etc/ -maxdepth 2 -mindepth 2 #Just look at the second level directory [root@localhost opt]# find /boot -d #First display the file in the display directory #Find the file belonging to zhangsan under / home and list the properties [root@localhost opt]# find /home -user zhangsan -ls #Find the file belonging to zhangsan under / home and list the properties [root@localhost opt]# useradd hehe [root@localhost opt]# userdel hehe [root@localhost opt]# ls /home/ hehe mysql zhangsan [root@localhost opt]# ll /home/ Total consumption 0 drwx------. 3 1002 1002 78 8 September 26:27 hehe drwx------. 3 mysql mysql 78 8 November 25:45 mysql drwx------. 3 zhangsan zhangsan 78 8 June 20:57 zhangsan [root@localhost opt]# find /home/ -nouser #Find the homeless folder under home /home/hehe /home/hehe/.mozilla /home/hehe/.mozilla/extensions #Or priority with and [root@localhost opt]# find /etc/ -type d -o -type l |wc -l 1008 [root@localhost opt]# find /etc/ -type d -o -type l -ls |wc -l 1008 [root@localhost opt]# find /etc/ -type d -o -type l -a -ls |wc -l 274 [root@localhost opt]# find /etc/ \( -type d -o -type l \) -a -ls |wc -l 1008 [root@localhost opt]# find -size 1G #Looking for 1G files will show everything . ./100.img ./99.img #Find two new files by size [root@localhost opt]# dd if=/dev/zero of=99.img bs=99M count=1 [root@localhost opt]# dd if=/dev/zero of=100.img bs=100M count=1 [root@localhost opt]# ls 100.img 99.img [root@localhost opt]# find -size 100M ./100.img [root@localhost opt]# find /opt/ -size 100M /opt/100.img [root@localhost opt]# ll 99.img -rw-r--r--. 1 root root 103809024 8 October 26:02 99.img [root@localhost opt]# echo >>99.img [root@localhost opt]# ll 99.img -rw-r--r--. 1 root root 103809025 8 October 26:03 99.img [root@localhost opt]# find /opt/ -size 100M /opt/100.img /opt/99.img #This should be because 100M in linux is not exactly 100M. It starts with 99 and does not include 99 to 100 (99-100] #Think 1024M is the same as 1G? 1024M Representative 1023 M - 1024M Excluding 1023 1G Represents 0 G -1G Exclude 0 G -size 10k Represents 9 k To 10 k And excluding 9 k -size -10k Represents 9 k within Including 9 k -size +10k Indicates 10 k above Excluding 10 k #Permission lookup - perm [root@localhost opt]# find -perm 644 ./100.img ./99.img ./a.txt [root@localhost opt]# find -name "*.img" -delete #Find and delete (find name and delete) [root@localhost opt]#find -name /data /* -delete #Pattern root deletion ##################action -exec COMMAND {} \; Execute by for each file found COMMAND Specified command {}: Used to reference the found file name itself -ok COMMAND {} \; Execute by for each file found COMMAND The specified command will be executed for each file before executing the command Interactive require user confirmation -ls -delete [root@localhost opt]# find -name "*.txt" -ok mv {} /mnt \; [root@localhost opt]# find -name "*.txt" -exec mv {} /mnt \; #In "days" -atime [+|-]# # #express[#,#+1) +# #express[#+1,∞] -# #express[0,#) -mtime -ctime #In minutes -amin -mmin -cmin find -mtime 10 #10 to 11 days find -mtime +10 #Represents more than 11 days find -mtime -10 #Within 10 days
2.7.4 which
Only the location of the command can be viewed
The location of the command is in the system echo $PATH (variable)
/usr/bin:/usr/sbin...: / root/bin ls (program qq.txt) XXX
which command | program name
which ls
3 Summary
- Shell is a special program
- Classification, format and usage of Linux commands
- Linux common commands
- View and switch directories (pwd, cd, Is, du)
- Create directories and files (mkdir, touch, In)
- Copy, delete, move directories and files (cp, rm, mv)
- Find directories and files (which, find)