Shell Script (ii)
#find查找文件 to find based on time, name, UID, GID, permissions, size, etc.
A file or directory accessed within #-amin-10:10 minutes; a file or directory accessed +10:10 minutes ago. The time unit is minutes.
#查找10分钟之前30分钟之内访问过的文件或目录
Find/home-amin +10-a-amin-30
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/13/wKiom1VyuynxRIgqAAEuwlLInbU726.jpg "/>
Files or directories accessed within #-atime-2:48 hours, or files or directories accessed +2:48 hours ago.
#时间单位为24小时, i.e. 2*24=48 hours.
#查找1天之前3天之内访问过的文件或目录
find/var/log/-atime +1-a-atime-30-exec ls-l {} \;
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/0F/wKioL1VyvMnDDsF4AAE4iYY8UDg141.jpg "/>
A file or directory in which permission properties have been modified within #-cmin-2:2 minutes; + 2: The file or directory where the permission properties were modified 2 minutes ago. The time unit is minutes.
#查找2分钟之前60分钟之内修改过权限的文件或目录
Find/home-cmin +2-a-cmin-60
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/13/wKiom1VyuymStBg9AAGk-M5D2Ic235.jpg "/>
#-ctime-2:48 the file or directory in which the permission attribute was modified within the hour, or +2:48 the file or directory in which the permission properties were modified before the hour. The time unit is 24 hours, i.e. 2*24=48 hours.
#查找3天之前5天之内修改过权限属性的文件或目录
find/var/log/-ctime +3-a-ctime-5-exec ls-l {} \;
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/0F/wKioL1VyvMnhk9eMAAE0TVrI2aE652.jpg "/>
Files or directories that have changed content within #-mmin-2:2 minutes; + 2:2 minutes ago Modified the content of the file # or directory, the time unit is minutes.
#查找1分钟之前30分钟之内修改过文件内容的文件或目录
Find/home-mmin +1-a-mmin-30
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/13/wKiom1VyuyryTBs3AAENN7RUJfk229.jpg "/>
A file or directory in which content was modified within #-mtime-2:48 hours, or a file or directory in which the content was modified +2:48 hours ago. The time unit is 24 hours, i.e. 2*24=48 hours.
#查找1天之前30天之内修改过内容的文件或目录
Find/home-mtime +1-a-mtime-30
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/0F/wKioL1VyvMrQD6grAAIt9JLqmmE823.jpg "/>
#-uid (+ or-) value (0): 10: Indicates less than 10;+1: Indicates size or no +-5: equals 5
#在当前目录下查找uid a =0 file or directory
Find. -uid 0
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/13/wKiom1VyuyuAj5gSAAGoxyj7EuY584.jpg "/>
#-gid (+ or-) value (0): 10: Indicates less than 10;+1: Indicates size or no +-5: equals 5
#在当前目录下查找gid a =0 file or directory
Find. -gid 0
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/0F/wKioL1VyvMvDAeJXAAGkykVAd0I667.jpg "/>
#-name find a file or directory name: You can use the *, + match
#查找/var/log/all files ending with. log
Find/var/log/*.log
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/13/wKiom1VyuyuzBQAwAACp9Ocl_j8584.jpg "/>
#查找当前目录下uid >0,gid<2223 files or directories
Find. -uid +0-gid-2223
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/0F/wKioL1VyvMugq33TAAIjp_BxZDo997.jpg "/>
#-perm mode:
#-Value: Mode is converted to binary, 1 of the four values must be matched, such as 0700 (111000000).
#0700-7777 are matched to each other and can be understood to be more than 0700 matched
#查找权限大于744的文件或目录, because mode 1th is 7, all 1, so all master
#权限不为7的都不会符合, the group privilege value is 4, 5 (4+1), 7 (4+2+1), and the phase has 1
#符合要求, other permissions require the same as group permissions, so the test catalog meets the requirements
Find. -perm-744
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/13/wKiom1VyuyzR9k2CAAG7G6nlC2c976.jpg "/>
#+ Value: Mode is converted to binary, four values bits any of the 1 places
#有其中1个值相与等于1就可以了
#+001 matches permissions that are 1, 5 (=4+1), and 7 (=4+2+1) as long as the last permission value is
#查找包含有权限值001的文件或目录
The #user3, User4, and User5 permissions are 001 and 1, so they do not meet the requirements, while DDD
#其他权限中相与有1, so meet the requirements
#+mode can be understood as conforming to the requirements that include this mode pattern
Find. -perm +001
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/0F/wKioL1VyvMyA9r1gAAIpRepvEpk676.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/0F/wKioL1VyvMzChTNKAAISliXFUCs336.jpg "/>
#mode: Indicates equals.
#查找权限值等于001的文件或目录
Find. -perm 001
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/13/wKiom1Vyuy3hrWizAAGcs2praGQ690.jpg "/>
#-size File Size:-The numeric representation is less than, + the value is greater than, no-+ + = equals.
#查找大于10k小于30k的文件或目录
Find. -size +10-size-30k
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/0F/wKioL1VyvM3icAeuAALY3mOqUQU338.jpg "/>
#查找大小等于4k的文件或目录, K can be converted to M or G
Find. -size 10k
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/13/wKiom1Vyuy7QUAp-AAHndM7iYYg329.jpg "/>
#-empty: Find an empty directory with a size of 0 bytes file, or no subdirectories or files in the directory
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/0F/wKioL1VyvM6QtOpNAAJRXhH2UkM444.jpg "/>
#-exec shell command: Execute shell command on the found file or directory
#查找大于10k小于30k的文件或目录, and copy it to/tmp
Find. -size +10k-size-30k-exec CP-RF {}/tmp \;
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/13/wKiom1Vyuy-CDmPAAACj5G8J3zw245.jpg "/>
#whereis,-B: Find only binaries,-u: Find files that do not contain the specified type,-B: only the specified
#录查找二进制文件,-M: whenever you find a Help file in the specified directory
#-s: Find source files only in specified directory
Search under #whereis只会在 $PATH path
Whereis ls
#aaa文件在/home, not in $path, so the search is not
Whereis AAA
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/0F/wKioL1VyvM_jzoHkAAFZ0a107kg841.jpg "/>
#which, find the executable file and list specific additional information if it is an alias command
which LS
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/13/wKiom1VyuzDygjkEAABkdV4CMEc729.jpg "/>
#locate, search in the database of established files and paths, and change the database before general search
#如果不使用updatedb更新, the newly created file cannot be searched after the system has been started.
#最小化安装是没有locate命令的, yum-y install mlocate installation
#它依赖/var/lib/mlocate/mlocate.db Database
Locate One_day
#新建一个文件abcd123456, and then find, because there is no updatedb, so it is not found file
Touch abcd123456
Locate abcd123456
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/13/wKiom1VyuzCwPSrnAADVX8lpFJw607.jpg "/>
2. File sorting, merging and segmentation
(1) Sort command
#-c: The test file is sorted;-K: Specifies the sort field;-M: merges two sorted files;
#-n: Sort by number size;-o: Writes output to file;-r: Reverses sorting results
#-t: Changing the field delimiter;-U: Removing duplicate rows from the result
Reverse sort #将a. txt and remove duplicate rows
Sort-u-R A.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/0F/wKioL1VyvM-jIhMAAACL5nWYlss935.jpg "/>
#以 ":" is a delimiter that sorts/etc/passwd according to the size of the 3rd field number
SORT-T:-k3n/etc/passwd
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/0F/wKioL1VyvNChGP1aAAMRrUH0Wns657.jpg "/>
#合并已经排序的两个文件, the two files that need to be merged must have been sorted.
Sort-u-M A.txt b.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/13/wKiom1VyuzCx60aRAACcA49_NiI023.jpg "/>
#根据第2域来排序
Sort-k2 A.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/0F/wKioL1VyvNCwMnRXAABr_8PtRws856.jpg "/>
(2) Uniq command
#去除文本中重复的行,-C: Prints the number of occurrences of the first line in the text;-D: Only duplicate records are displayed.
#每个重复记录只出现一次;-U: Show only records with no duplicates
#需要特别注意的是所说的 "Repetition" refers to repeated occurrences! such as file Test.txt content:
1
1
2
1
Uniq-c Test.txt
#结果为, the 1th row number 1 appears as 2, the 2nd row number 2 appears 1, the 4th number 1 appears 1
2 1
1 2
1 1
#再如
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/13/wKiom1VyuzHDVEBVAACsq75LAwI876.jpg "/>
#如果需要统计文件中所有重复的行, you need to sort it first
Sort 10.txt | Uniq-c 10.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/13/wKiom1VyuzHRJBrgAABiNgzk164439.jpg "/>
#注意与uniq-C 10.txt Difference
#只显示重复的行, notice the difference.
Uniq-d 10.txt
Sort 10.txt | Uniq-d 10.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/0F/wKioL1VyvNCTXcC6AACES8IjnVA478.jpg "/>
#只显示没有重复的行, note the comparison of the result of removing duplicate rows before and after file sorting
#因为第1行开始数字1连续出现3次, so Uniq-u 10.txt will not show 1 on line 1th
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/13/wKiom1VyuzHwzGnJAAC3DWT4wbA178.jpg "/>
(3) Join command
shell script (ii)