The real play is expressions, which also has three parts: the options test actions.
Since path is often directly behind Test, the other two appear less, so start with test first.
-name pattern: Matches all files with the file name "pattern". In most cases, we find files by file name, so this is the most frequently used option.
Note: The pattern should not include the path name, only the final file name, that is, basename is the pattern, otherwise it will error, the pathname should be specified in path, here requires that the file name and pattern must be exactly the same. As follows:
m@meng:~$ ls tmp/onlyme onlyme1 test.shm@meng:~$ find tmp/ -name onlymetmp/onlymem@meng:~$ find tmp/ -name onlym@meng:~$
As you can see, writing only part of the name is not matched.
The-name option supports simple shell character extensions such as *, [],?, where * matches any number of characters;? Match an arbitrary character; [] matches any one of the characters appearing in parentheses:
m@meng:~/tmp$ find . -name onlym[eg]./onlymem@meng:~/tmp$ find . -name ??lyme./onlymem@meng:~/tmp$ find . -name on*./onlyme
More complex extensions are not supported, and the following-regex options are required.
-name also has several variants:
-iname: Ignoring uppercase and lowercase-name;
-lname:file is a symbolic link whose contents match shell pattern. This means matching the file name that the symbolic link points to, not the symbolic link name itself.
-ilname: Ibid., ignoring case.
-regex pattern: The pattern here can be a complete regular expression, which is best quoted in quotation marks to prevent accidents. Pattern is the file name represented by a regular expression.
-regex also has a variant-iregex, which can be ignored for case.
-path pattern: This option is available because the-name option cannot use the path delimiter ("/" Error in name), that is, you cannot select subdirectories, but in some cases we need to specify subdirectories or exclude certain subdirectories (with-prune).
Note that the pattern after-path must be the starting path for the directory specified earlier: if the specified directory is a relative path ".", then pattern must also be "." If the specified directory is an absolute path, the pattern also adds this part of the path to the front. as follows:
m @meng :~/patches $ ls tmp/new NW onlyme test.shm @meng :~/patches $ find. -path./tmp/on*./tmp/onlymem @meng :~/patches $ find. -path tmp/on*m @meng :~/patches $ m @meng :~/patches Span class= "hljs-variable" >$ find/home/m/patches/-path/home/m/patches/tmp/on*m @meng :~/patches $ /home/m/patches/tmp/onlyme
-path also supports simple meta-character extensions, but for insurance purposes, it is best to enclose the pattern in double quotation marks, as follows:
m@meng:~/patches$ find . -path ./tmp/*find: 路径必须在表达式之前: ./tmp/nw用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]m@meng:~/patches$ "./tmp/*"./tmp/nw./tmp/onlyme./tmp/test.sh./tmp/new
I see a lot of articles using the following method to find files in subdirectories, I found that after the experiment does not work:
m@meng:~/patches$ find . -path ./tmp./tmpm@meng:~/patches$ find . -path ./tmp -name onlymem@meng:~/patches$
We see that if there is only a subdirectory name (not a slash after the name), then only the subdirectory itself will be matched, not all the files under the directory are matched, and using path to specify a subdirectory, then use-name to specify the name will not get the desired result, Therefore, you can only fill in the file name in the pattern. However, the following method can:
m@meng:~/patches$ find . -path ./tmp -o -name onlyme./tmp./tmp/onlyme
The-o option is used here and will be discussed later.
-uid N and-user name: are matched according to the owner of the file, based on the UID of owner, which is based on name.
- -gid N and-group name: Ditto, just replaced by group.
-nouser and Nogroup: matches if the owner or group of a file has been deleted.
-type c: Matches all files with file type C. The specific values for C include: F (normal file), B (block device), C (character file), D (directory), L (symbolic Link), and so on.
-size: Match According to the size of the file. Supported units are C (bytes), W (word,2bytes), B (Blocks,512bytes), K (1024bytes), M (1024k bytes), G (1024M bytes).
[Email protected]:~/workspaces$ Find. -size1C./. Metadata/. Plugins/org. Eclipse. Core. Resources/. Root/. Indexes/history. Version./. Metadata/. Plugins/org. Eclipse. Core. Resources/. Root/. Indexes/properties. Version./. Metadata/. Plugins/org. Eclipse. CDT. make. Core/specs. C./. Metadata/. Plugins/org. Eclipse. CDT. make. Core/specs. cpp[Email protected]:~/workspaces$ Find. -size-1C./. Metadata/. Plugins/com. Genuitec. org. Hibernate. Eclipse/hibernate-tools. Log./. Metadata/. Plugins/org. Skyway. Core/compass/index/core/clearcache[email protected]:~/workspaces$ Find. -size +2C-size-5C./. Metadata/. Plugins/org. Eclipse. JDT. Core/assumedexternalfilescache./. Metadata/. Plugins/org. Eclipse. JDT. Core/invalidarchivescache
The numbers are often used in test, so it is bound to involve numerical range problems. Find gives you the option to precede the number with a "+" representation greater than that number, while "-" means less than that number, and no symbol equals that number.
-inum N and-samefile name: matches according to whether the same file is the same. Inum represents the inode number, when the indoe of a file matches the specified n phase, and-samefile uses the file name directly if a file has the same inode as the specified name, matching.
-readable,-writable,-executable: Obviously, this is filtering files by file permissions. For example, if you add-executable, files that do not have executable permissions will be filtered out.
However, as you all know, the permissions of a file are divided into three groups, namely User (U), Group (g), and others (O), and each group's permissions are divided into read (R), write (w), and execute (x). So which group do these three options belong to? I haven't studied it yet, but fortunately find has a more detailed and more common option for matching permissions, which is the-perm below.
-perm mode: Matches according to the permissions of the file. However, there are three modes when assigning permissions mode: Mode,-mode,/mode.
The pure mode format implies a strict match, that is, each set of permissions must be specified and exactly the same, not just one set and the other group defaults.
-mode format will be relaxed many, such as-o=x, so long as a file of the O Group at least X permissions can be matched (there is no R and W permissions do not matter), and other groups regardless of, or 221, that means that a file of the U Group and G Group at least have the W permission o Group at least X permissions can match, so-O =x is equivalent to-001. So the-mode format is "at least mode".
/mode format is also relaxed, in the/mode format specified in the three set of permissions, as long as a group reached the "minimum mode", you can successfully match. You can use a formula to express/mode, such as/124 <==> 100 | | -020 | | -004; /u=w,o=r <==>-u=w | | -o=r. Give some examples:
M@meng: ~/tmp$ Ls-la Total Dosage8---x-w---x1Mm6 6Month at on: Aonlyme-rw-rw-r--1Root root0 6Month - A:Onlyme1-r-xr-x--x1Mm the 6Month at Geneva: $Test.shm@meng: ~/tmp$ Find. -perm/242.. /onlyme1./test.shm@meng: ~/tmp$ Find. -perm/go=w. /onlyme./onlyme1m@meng: ~/tmp$ Find. -perm/go=x. /onlyme./test.shm@meng: ~/tmp$ Find. -perm-g=x. /test.shm@meng: ~/tmp$ Find. -perm- -.. /onlymem@meng: ~/tmp$ Find. -perm121./onlyme
-amin,-atime,-anewer: These three items are matched according to the file's "Last Accessed Time" attribute.
-atime N: The last access time of the file is N days before the match. Strictly speaking, n is actually not "day", but 24 hours, that is, from now on, the Forward 24*n~24* (n+1) hours if the file is accessed, then match, attention must be strictly this interval. Here you can change n to-N or +n, representing a range of 0~24*n and 24* (n+1) ~ Infinity.
-amin N: Here the n represents the minute. Matches if the number of minutes between the last access time of the file and the current time is n-1.
-anewer file: Calculates the mtime of the file, if the last access time of a document is after this mtime, then matches.
Similar combinations are:-ctime,-cmin,-cnewer, which only changed the last access time to the last change time,-mtime,-mmin,-newer, "Last access Time" changed to the last change time. The difference between these three types of time is described in the Stat command.
-newerxy File/time: This is the culmination of those options above. Newer is followed by two placeholders whose values can be a, M, C, T represent the last access time, last modify time, last change time, and absolute time, and find calculates a timestamp of file based on the value of Y, and then makes a match based on the value of X. Here's no example:
M@meng: ~/tmp$ Stat *File:' Onlyme 'Access: -- .- - -: -:31.859458991+0800Modify: -- .- - A: the:16.115352584+0800 Change: -- .- - -: -:31.859458991+0800File:' Test.sh 'Access: -- .- - -: the: -.203429170+0800Modify: -- .- - -: +:16.739430951+0800 Change: -- .- - -: the:Geneva.779433692+0800M@meng: ~/tmp$ Find. -neweram Onlyme. /onlyme./test.shm@meng: ~/tmp$ Find. -newerma Onlyme m@meng: ~/tmp$M@meng: ~/tmp$ Find. -newermt"2015-06-28 13:23:16".. /test.sh
This deletes the output of a portion of the stat command. As you can see, the same is File=onlyme, when Xy=am, find calculates the Onlyme mtime for 2015-06-28 12:49:16, and then searches the specified directory for those atime files after this time, Obviously the atime of Onlyme and test.sh are eligible, while Xy=ma, the Onlyme atime is 2015-06-28 13:58:31, but the mtime of any file in the specified directory is before this time, so there is no match.
The last example, make y=t, then specify an absolute time, and then search for the files in the specified directory after mtime at this time. Obviously, the other x=t is meaningless, so it will be an error.
- Empty: Matches if a file is empty and is not an ordinary file or directory. It can also be said that if the result of a file is empty, it is matched.
- -links N: Matches if the number of hard links for a file is n.
At this point, the test of those commonly used in the introduction, you can see that find to do file matching is mainly based on the following types of file attributes: Name and path, the owner and the group, permission, size, type and inode, timestamp;
The next article describes other miscellaneous.