How to understand the find command
This article is about how to understand the find command, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Find command:
Find-search for files in a directory hierarchy
Find [OPTIONS...] [search path] [search condition] [processing action]
Search path: the default is the current working directory, and you can specify a specific directory path
Search criteria: the criteria for this search can be file name, file size, file type, file permissions, etc.; the default is all files in the specified directory
Processing action: perform a processing operation on files that meet the criteria; output the search results to the monitor by default
Look up by file name:
-name file name, which supports the use of Globbing, (*,?, [], [^]), and search based on keywords.
Example: [root@localhost test] # find. -name "* .log"
. / log_link.log
. / log2014.log
-iname file name, ignore the case of letters, support the use of Globbing, (*,?, [], [^])
Example: find /-iname qin
Look up according to the inode number of the file:
-inum inode number: find the corresponding file name and path through the given inode number
-samefile name: find the corresponding inode number through the given file name, and then determine all the file names and paths with the inode number
-links n: find all files with n links
Look up according to the regular expression:
-regex pattern: matches the entire file path string with pattern, not just the name of the given file
Search according to the owner and group of the file:
-user uname: search based on the user name of the specified user by the owner
-uid UID: search for a UID based on the owner
-group gname:
-gid GID:
-nogroup: there is no group name corresponding to the group in the file.
-nouser: there is no corresponding user name on the owner of the file
Look up according to the type of file:
-type file type:
B: block equipment
C: character device
D: catalog file
F: ordinary files
L: symbolic link file
P: pipe fil
S: socket file
-xtype file type: matching of symbolic link files needs to be matched with other options
Look up according to the timestamp:
In days:
-atime [+ | -] n: search based on access time
-ctime [+ | -] n: search according to the change time
-mtime [+ | -] n: search based on modification time
N: [NJ Numer1)
+ n: [nasty 1pm + ∞)
-n: [now,n)
In minutes:
-amin [+ | -] n
-cmin [+ | -] n
-mmin [+ | -] n
Example: find files in the / etc directory that have been modified in the last week and do not belong to root and suse users
Find / etc-ctime-7-a-user root-a-user suse
Example: look for files with a change time before 5 days in the / l o g s directory and delete them:
Find logs-type f-mtime + 5-exec-ok rm {}\
Look up according to the size of the file:
-size [+ | -] n [cwbkMG]
N: (n Mei 1Jing n]
-n: [05m nmuri 1]
+ n: (NMagna + ∞)
Example:
Find-size + 2k
All files greater than 2KB in the current directory
Find-size 2k
Files between all 1KB-2KB in the current directory
Find-size-2k
All files smaller than 1KB in the current directory
Combination conditions:
-a: logic and, by default, can be omitted
-o: logical or
-not,!: logic not
Example: find files in the / usr directory that do not belong to root, bin, centos and other users
Find / usr-not-user root-a-not-user bin-a-not-user centos
The logical combination conditions follow de Morgan's law:
Not (An and B) = not An or B
Non (An or B) = non-An and non-B
Find based on permissions:
-perm [/ | -] mode
Mode: exactly matches the specified permissions
/ mode: the logical OR relationship is implied. The condition can be satisfied as long as there is a permission match in any permission bit.
-mode: implies the relationship between logic and. The permission of each permission bit must also contain the specified permission bit in order to meet the condition.
Example: find files in the / etc/rc.d/init.d directory where all users have execute permissions and other users have write permissions
Find / etc/rc.d/init.d-prem / 111a-perm-002
All of them are against any one of them.
! (an and b and c) =! an or! B or! C
None of them are reversed. Any one of them has.
! (! an and! B and! C) = an or b or c
Processing actions:
-print: output to the display screen, default action
-ls: execute the ls-li command to display the results found
Example: look for files in the / etc directory that all users do not have write permission to, and display their details
Find / etc-not-perm / 222-ls
-exec COMMAND {}\;:
-ok COMMAND {}\;:
Execute the COMMAND command for the found results
Difference:
-exec is non-interactive
-ok is interactive
{}: placeholder to refer to the path information of all files found by the find command
Ex.: find files that do not have owners or groups on the current system and have been accessed in the last month, and change them to root
Find /-nouser-o nogroup-a-atime-30-exec chown root:root {}\
Instead of performing operations,-exec and-ok:
Chmod Amurr $(find-perm-444-type f)
Find-perm-444-type f | xargs chmod Amurr
Note: the pipeline carries pure string information, so if the command after the pipe is not a command for processing strings, you need to use the xargs command to convert it into parameters that can be processed by subsequent commands.
Example: look for all * .h in / tmp, look for "SYSCALL_VECTOR" in these files, and finally print out all the file names that contain "SYSCALL_VECTOR"
Find / tmp-name "* .h" | xargs-n50 grep SYSCALL_VECTOR
The above is how to understand the find command, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.