12 common parameters of the find command in linux (including usage and precautions)

Source: Internet
Author: User
This article describes in detail some common parameters and examples of the find command in linux, as well as some specific usage and precautions. if you need it, refer to the "LinuxFind" tab.

1. use the name option:
The file name option is the most common option for the find command. you can either use this option independently or use it with other options.
You can use a certain file name pattern to match the file. remember to use quotation marks to cause the file name pattern.
No matter what the current path is, if you want to find a file with a file name *. log in your root directory $ HOME, use ~ As the 'pathname' parameter, the tilde ~ Represents your $ HOME directory.

Copy codeThe code is as follows: find ~ -Name "*. log"-print
To search for all '*. log' files in the current directory and subdirectory, you can use:

Copy codeThe code is as follows: find.-name "*. log"-print
To search for a file whose name starts with an uppercase letter in the current directory and subdirectory, you can use:

Copy codeThe code is as follows: find.-name "[A-Z] *"-print
To search for a file whose file name starts with host in the/etc directory, use:

Copy codeThe code is as follows: find/etc-name "host *"-print
To search for files in the $ HOME directory, you can use:

Copy codeThe code is as follows: find ~ -Name "*"-print or find.-print
To make the system run at a high load, search for all the files from the root directory.

Copy codeThe code is as follows: find/-name "*"-print
If you want to search for files with names starting with lowercase letters in the current directory and ending with. log in 4 to 9:
Command:

Copy codeThe code is as follows: find.-name "[a-z] * [4-9]. log"-print
Output:

Copy codeThe code is as follows: [root @ localhost test] # ll
Total 316
-Rw-r -- 1 root 302108 11-13 06:03 log2012.log
-Rw-r -- 1 root 61 11-13 06:03 log2013.log
-Rw-r -- 1 root 0 11-13 06:03 log2014.log
-Rw-r -- 1 root 0 11-13 06:06 log2015.log
Drwxr-xr-x 6 root 4096 10-27 0scf
Drwxrwxr-x 2 root 4096 11-13 06:08 test3
Drwxrwxr-x 2 root 4096 11-13 05:50 test4
[Root @ localhost test] # find.-name "[a-z] * [4-9]. log"-print
./Log2014.log
./Log2015.log
./Test4/log2014.log
[Root @ localhost test] #

2. use the perm option:
Use the-perm option according to the file permission mode and find the file in the file permission mode. It is best to use the octal permission notation.
For example, in the current directory, find a file with a permission of 755, that is, the file owner can read, write, and execute the file. other users can read and execute the file. you can use:

Copy codeThe code is as follows: [root @ localhost test] # find.-perm 755-print
.
./Scf
./Scf/lib
./Scf/service
./Scf/service/deploy
./Scf/service/deploy/product
./Scf/service/deploy/info
./Scf/doc
./Scf/bin
[Root @ localhost test] #
 
There is another way to express it: add a horizontal bar before the octal number to indicate that all matches. for example,-007 is equivalent to 777, and-005 is equivalent to 555,
Command:

Copy codeThe code is as follows: find.-perm-005
Output:

Copy codeThe code is as follows: [root @ localhost test] # ll
Total 316
-Rw-r -- 1 root 302108 11-13 06:03 log2012.log
-Rw-r -- 1 root 61 11-13 06:03 log2013.log
-Rw-r -- 1 root 0 11-13 06:03 log2014.log
-Rw-r -- 1 root 0 11-13 06:06 log2015.log
Drwxr-xr-x 6 root 4096 10-27 0scf
Drwxrwxr-x 2 root 4096 11-13 06:08 test3
Drwxrwxr-x 2 root 4096 11-13 05:50 test4
[Root @ localhost test] # find.-perm-005
.
./Test4
./Scf
./Scf/lib
./Scf/service
./Scf/service/deploy
./Scf/service/deploy/product
./Scf/service/deploy/info
./Scf/doc
./Scf/bin
./Test3
[Root @ localhost test] #

3. ignore a directory:
If you want to ignore a directory when searching for a file because you know that there is no file in the directory, you can use the-prune option to specify the directory to be ignored. Be careful when using the-prune option, because if you use the-depth option at the same time, The-prune option will be ignored by the find command. If you want to search for files in the test Directory but not in the test/test3 directory, you can use:
Command:

Copy codeThe code is as follows: find test-path "test/test3"-prune-o-print
Output:

Copy codeThe code is as follows: [root @ localhost soft] # find test-path "test/test3"-prune-o-print
Test
Test/log2014.log
Test/log2015.log
Test/test4
Test/test4/log2014.log
Test/test4/log2013.log
Test/test4/log2012.log
Test/scf
Test/scf/lib
Test/scf/service
Test/scf/service/deploy
Test/scf/service/deploy/product
Test/scf/service/deploy/info
Test/scf/doc
Test/scf/bin
Test/log2013.log
Test/log2012.log
[Root @ localhost soft] #

4. how to avoid a file directory when searching for files using find:
Instance 1: Search for all files not in the test4 subdirectory under the test Directory
Command:

Copy codeThe code is as follows: find test-path "test/test4"-prune-o-print
Output:

Copy codeThe code is as follows: [root @ localhost soft] # find test
Test
Test/log2014.log
Test/log2015.log
Test/test4
Test/test4/log2014.log
Test/test4/log2013.log
Test/test4/log2012.log
Test/scf
Test/scf/lib
Test/scf/service
Test/scf/service/deploy
Test/scf/service/deploy/product
Test/scf/service/deploy/info
Test/scf/doc
Test/scf/bin
Test/log2013.log
Test/log2012.log
Test/test3
[Root @ localhost soft] # find test-path "test/test4"-prune-o-print
Test
Test/log2014.log
Test/log2015.log
Test/scf
Test/scf/lib
Test/scf/service
Test/scf/service/deploy
Test/scf/service/deploy/product
Test/scf/service/deploy/info
Test/scf/doc
Test/scf/bin
Test/log2013.log
Test/log2012.log
Test/test3
[Root @ localhost soft] #
Note:

Copy codeThe code is as follows: find [-path...] [expression]
The expression is behind the path list.
-Path "test"-prune-o-print is a short expression of-path "test"-a-prune-o-print, which is evaluated in order, -both a and-o are short-circuit values, which are similar to shell's & |
-Path "test" is true, evaluate-prune,-prune to return true, and the logical expression is true; otherwise, do not seek a value-prune, and the logical expression is false. If-path "test"-a-prune is false, evaluate-print,-print to return true, or the logical expression is true; otherwise, the value-print is not required, or the logical expression is true.
The special expression combination can be written:
If-path "test" then
-Prune
Else
-Print
Instance 2: Avoid multiple folders:
Command:

Copy codeThe code is as follows: find test \ (-path test/test4-o-path test/test3 \)-prune-o-print
Output:

Copy codeThe code is as follows: [root @ localhost soft] # find test \ (-path test/test4-o-path test/test3 \)-prune-o-print
Test
Test/log2014.log
Test/log2015.log
Test/scf
Test/scf/lib
Test/scf/service
Test/scf/service/deploy
Test/scf/service/deploy/product
Test/scf/service/deploy/info
Test/scf/doc
Test/scf/bin
Test/log2013.log
Test/log2012.log
[Root @ localhost soft] #
 
Note:
Parentheses indicate the combination of expressions. \ Indicates a reference, that is, it indicates that shell does not give a special explanation for the subsequent characters, but leaves it to the find command to explain its meaning.
Instance 3: search for a specific file, and add-name and other options after-o
Command:

Copy codeThe code is as follows: find test \ (-path test/test4-o-path test/test3 \)-prune-o-name "*. log"-print
Output:

Copy codeThe code is as follows: [root @ localhost soft] # find test \ (-path test/test4-o-path test/test3 \)-prune-o-name "*. log "-print
Test/log2014.log
Test/log2015.log
Test/log2013.log
Test/log2012.log
[Root @ localhost soft] #

5. use the user and nouser options:
Search for files by file owner:
Instance 1: find the file whose owner is peida in the $ HOME directory
Command:

Copy codeThe code is as follows: find ~ -User peida-print
Instance 2: find the file whose owner is peida in the/etc directory:
Command:

Copy codeThe code is as follows: find/etc-user peida-print
Note:
Instance 3: You can use the-nouser option to find files that have been deleted by the primary account. Search for all such files in the/home directory
Command:

Copy codeThe code is as follows: find/home-nouser-print
Note:
In this way, you can find the files whose owner does not have a valid account in the/etc/passwd file. When you use the-nouser option, you do not need to give a user name. The find command can complete the corresponding work for you.

6. use the group and nogroup options:
Like the user and nouser options, the find Command also has the same options for the user group to which the file belongs. to find files belonging to the gem user group in the/apps directory, you can use:

Copy codeThe code is as follows: find/apps-group gem-print
You can use the nogroup option to find all files that do not have a valid user group. The following find command looks for such a file from the root directory of the file system:

Copy codeThe code is as follows: find/-nogroup-print

7. search for files based on the change time or access time:
You can use the mtime, atime, or ctime option to find files based on the change time. If the system suddenly has no available space, it is very likely that the length of a file will increase rapidly during this period, then you can use the mtime option to find such a file.
Use minus signs-to limit the files whose change time is earlier than n days ago, and use the plus sign + to limit the files whose change time is earlier than n days ago.
To search for files whose modification time is less than 5 days in the root directory of the system, you can use:

Copy codeThe code is as follows: find/-mtime-5-print
To search for files whose modification time is earlier than 3 days in the/var/adm directory, you can use:

Copy codeThe code is as follows: find/var/adm-mtime + 3-print

8. search for new or old files:
You can use the-newer option to find all files whose modification time is newer than a file but older than the other file.
It generally takes the following form:

Copy codeThe code is as follows: newest_file_name! Oldest_file_name
Here ,! Is a logical non-sign.
Instance 1: find the files whose change time is earlier than log2012.log but earlier than log2017.log
Command:

Copy codeThe code is as follows: find-newer log2012.log! -Newer log2017.log
Output:

Copy codeThe code is as follows: [root @ localhost test] # ll
Total 316
-Rw-r -- 1 root 302108 11-13 06:03 log2012.log
-Rw-r -- 1 root 61 11-13 06:03 log2013.log
-Rw-r -- 1 root 0 11-13 06:03 log2014.log
-Rw-r -- 1 root 0 11-13 06:06 log2015.log
-Rw-r -- 1 root 0 11-16 14:41 log2016.log
-Rw-r -- 1 root 0 11-16 14:43 log2017.log
Drwxr-xr-x 6 root 4096 10-27 0scf
Drwxrwxr-x 2 root 4096 11-13 06:08 test3
Drwxrwxr-x 2 root 4096 11-13 05:50 test4
[Root @ localhost test] # find-newer log2012.log! -Newer log2017.log
.
./Log2015.log
./Log2017.log
./Log2016.log
./Test3
[Root @ localhost test] #
Instance 2: find the file whose change time is earlier than log2012.log
Command:

Copy codeThe code is as follows: find.-newer log2012.log-print
Output:

Copy codeThe code is as follows: [root @ localhost test] # find-newer log2012.log
.
./Log2015.log
./Log2017.log
./Log2016.log
./Test3
[Root @ localhost test] #

9. use the type option:
Instance 1: Search for all directories under the/etc directory
Command:

Copy codeThe code is as follows: find/etc-type d-print
Instance 2: Search for all types of files except directories in the current directory
Command:

Copy codeThe code is as follows: find .! -Type d-print
Example 3: Search for all symbolic link files in the/etc directory
Command:

Copy codeThe code is as follows: find/etc-type l-print

10. use the size option:
You can search for a file based on the file length. the file length referred to here can be measured by block or byte. The length of a byte metering file is N c. The length of a block metering file is represented by only numbers.
When searching for a file based on the file length, this file length in bytes is generally used. you can view the file system size because block metering is easier to convert.
Instance 1: search for files with a length greater than 1 MB in the current directory
Command:

Copy codeThe code is as follows: find.-size + 000000c-print
Example 2: find a file with a length of exactly 100 bytes in the/home/apache Directory:
Command:

Copy codeThe code is as follows: find/home/apache-size 100c-print
Instance 3: search for files with more than 10 blocks in the current directory (one block equals 512 bytes)
Command:

Copy codeThe code is as follows: find.-size + 10-print

11. use the depth option:
When using the find command, you may want to match all the files first and then search for them in the subdirectory. Use the depth option to run the find command. One reason for this is that when you use the find command to back up the file system on the tape, you want to back up all the files first, and then back up the files in the subdirectories.
Instance 1: The find command starts from the root directory of the FILE system and looks for a FILE named CON. FILE.
Command:

Copy codeThe code is as follows: find/-name "CON. FILE"-depth-print
Note:
It will first match all the files and then go to the subdirectory to find them.

12. use the mount option:
You can use the mount option of the find command to find files in the current file system (not to access other file systems.
Instance 1: search for files whose names end with XC in the current directory
Command:

Copy codeThe code is as follows: find.-name "*. XC"-mount-print

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.