Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of 12 commonly used parameters of find command in linux

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "detailed explanation of 12 commonly used parameters of find command in linux". In daily operation, it is believed that many people have doubts about the detailed explanation of 12 common parameters of find command in linux. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "detailed explanation of 12 common parameters of find command in linux". Next, please follow the editor to study!

1. Use the name option:

The file name option is the most commonly used option for the find command, either alone or in conjunction with other options.

You can use some kind of file name pattern to match files, and remember to enclose the file name pattern in quotation marks.

No matter what the current path is, if you want to look in your root directory $HOME for files whose names match * .log, use ~ as the 'pathname' parameter, and the tilde ~ represents your $HOME directory.

The code is as follows:

Find ~-name "* .log"-print

To find all the'* .log 'files in the current directory and subdirectories, you can use:

The code is as follows:

Find. -name "* .log"-print

To find files whose names begin with an uppercase letter in the current directory and subdirectories you want, you can use:

The code is as follows:

Find. -name "[Amurz] *"-print

To find files whose names begin with host in the / etc directory, you can use:

The code is as follows:

Find / etc-name "host*"-print

To find the files in the $HOME directory, you can use:

The code is as follows:

Find ~-name "*"-print or find. -print

If you want the system to run under heavy load, look for all the files from the root directory.

The code is as follows:

Find /-name "*"-print

If you want to look in the current directory for files whose names begin with lowercase letters and end with 4 to 9 plus .log:

Command:

The code is as follows:

Find. -name "[a murz] * [4-9] .log"-print

Output:

The code is as follows:

[root@localhost test] # ll

Total 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log

Drwxr-xr-x 6 root root 4096 10-27 01:58 scf

Drwxrwxr-x 2 root root 4096 11-13 06:08 test3

Drwxrwxr-x 2 root root 4096 11-13 05:50 test4

[root@localhost test] # find. -name "[a murz] * [4-9] .log"-print

. / log2014.log

. / log2015.log

. / test4/log2014.log

[root@localhost test] #

2. Use the perm option:

According to the file permission mode, use the-perm option to find files by the file permission mode. It is best to use the octal representation of permissions.

If you look for files with a file permission bit of 755 in the current directory, that is, files that the file owner can read, write and execute, and other users can read and execute, you can use:

The 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 of expression: put a horizontal bar in front of the octal number to indicate that they all match. For example,-007 is equivalent to 77710, which equals 555.

Command:

The code is as follows:

Find. -perm-005

Output:

The code is as follows:

[root@localhost test] # ll

Total 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log

Drwxr-xr-x 6 root root 4096 10-27 01:58 scf

Drwxrwxr-x 2 root root 4096 11-13 06:08 test3

Drwxrwxr-x 2 root 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 looking for files, because you know that there are no files you are looking for, you can use the-prune option to indicate the directory you need to ignore. Be careful when using the-prune option, because if you also use the-depth option, the-prune option will be ignored by the find command. If you want to look for files in the test directory, but not in the test/test3 directory, you can use:

Command:

The code is as follows:

Find test-path "test/test3"-prune-o-print

Output:

The 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 using find to find files:

Example 1: find all files in the test directory that are not in the Test4 subdirectory

Command:

The code is as follows:

Find test-path "test/test4"-prune-o-print

Output:

The 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] #

Description:

The code is as follows:

Find [- path..] [expression]

After the path list is the expression

-path "test"-prune-o-print is the abbreviated expression of-path "test"-a-prune-o-print is evaluated sequentially. Both-an and-o are short-circuited, similar to shell's & & and | similar to if

If-path "test" is true, the evaluation-prune,-prune returns true, and the logical expression is true; otherwise, it does not evaluate-prune, and the logical expression is false. If-path "test"-a-prune is false, the evaluation-print,-print returns true, or the logical expression is true; otherwise, it does not evaluate-print, or the logical expression is true.

The special case of this expression combination can be written in pseudo code as:

If-path "test" then

-prune

Else

-print

Example 2: avoid multiple folders:

Command:

The code is as follows:

Find test\ (- path test/test4-o-path test/test3\)-prune-o-print

Output:

The 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] #

Description:

Parentheses indicate the combination of expressions. \ indicates a reference, which instructs shell not to make a special interpretation of the following characters, but leaves it to the find command to explain its meaning.

Example 3: find a certain file, and add options such as-name after-o

Command:

The code is as follows:

Find test\ (- path test/test4-o-path test/test3\)-prune-o-name "* .log"-print

Output:

The 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:

Find the file by file owner:

Example 1: find the file whose owner is peida in the $HOME directory

Command:

The code is as follows:

Find ~-user peida-print

Example 2: find the file whose owner is peida in the / etc directory:

Command:

The code is as follows:

Find / etc-user peida-print

Description:

Example 3: to find files whose master account has been deleted, you can use the-nouser option. Find all such files in the / home directory

Command:

The code is as follows:

Find / home-nouser-print

Description:

This allows you to find files where the owner does not have a valid account in the / etc/passwd file. When using the-nouser option, you don't have to give a user name; the find command can do the job for you.

6. Use the group and nogroup options:

Just like the user and nouser options, the find command has the same option for the user group to which the file belongs. To find files that belong to the gem user group in the / apps directory, you can use:

The code is as follows:

Find / apps-group gem-print

To find all files that do not have a valid user group, you can use the nogroup option. The following find command looks for such files from the root directory of the file system:

The code is as follows:

Find /-nogroup-print

7. Find the file according to the change time or access time:

If you want to find the file by when it was changed, you can use the mtime,atime or ctime option. If the system suddenly runs out of free space, there is a good chance that the length of a file will grow rapidly during this period, and you can use the mtime option to find such a file.

Use the minus sign to limit files whose change time is less than n days, and the plus sign to limit files whose change time is less than n days.

If you want to find files with a change time of less than 5 days in the system root directory, you can use:

The code is as follows:

Find /-mtime-5-print

To find files with a change time before 3 days in the / var/adm directory, you can use:

The code is as follows:

Find / var/adm-mtime + 3-print

8. Find a file that is newer or older than a file:

If you want to find all files whose change time is newer than one file but older than another, you can use the-newer option.

Its general form is:

The code is as follows:

Newest_file_name! Oldest_file_name

Among them,! It's logical, not symbolic.

Example 1: find files whose change time is newer than the file log2012.log but older than the file log2017.log

Command:

The code is as follows:

Find-newer log2012.log!-newer log2017.log

Output:

The code is as follows:

[root@localhost test] # ll

Total 316

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log

-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log

-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log

-rw-r--r-- 1 root root 0 11-16 14:41 log2016.log

-rw-r--r-- 1 root root 0 11-16 14:43 log2017.log

Drwxr-xr-x 6 root root 4096 10-27 01:58 scf

Drwxrwxr-x 2 root root 4096 11-13 06:08 test3

Drwxrwxr-x 2 root 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] #

Example 2: find files whose change time is newer than log2012.log files

Command:

The code is as follows:

Find. -newer log2012.log-print

Output:

The 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:

Example 1: find all directories under the / etc directory

Command:

The code is as follows:

Find / etc-type d-print

Example 2: find all types of files except the directory in the current directory

Command:

The code is as follows:

Find. !-type d-print

Example 3: find all symbolic link files in the / etc directory

Command:

The code is as follows:

Find / etc-type l-print

10. Use the size option:

Files can be found by file length, which can be measured either in blocks (block) or in bytes. The expression of measuring file length in bytes is NC; measuring file length in blocks can only be expressed in numbers.

Example 1: find files with a length of more than 1 M bytes in the current directory

Command:

The code is as follows:

Find. -size + 1000000c-print

Example 2: find a file with a length of exactly 100 bytes in the / home/apache directory:

Command:

The code is as follows:

Find / home/apache-size 100c-print

Example 3: look for files longer than 10 blocks in the current directory (one block equals 512 bytes)

Command:

The 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 before looking in subdirectories. Using the depth option, you can make the find command do this. One reason for this is that when you use the find command to back up a file system on tape, you want to back up all the files first, and then back up the files in the subdirectory.

The instance 1:find command starts at the root of the file system and looks for a file named CON.FILE.

Command:

The code is as follows:

Find /-name "CON.FILE"-depth-print

Description:

It will first match all the files and then go into the subdirectory to find it.

12. Use the mount option:

To find files in the current file system (without entering other file systems), you can use the mount option of the find command.

Example 1: start from the current directory to find files in this file system whose filenames end with XC

Command:

The code is as follows:

Find. -name "* .XC"-mount-print

At this point, the study of "detailed explanation of the 12 common parameters of the find command in linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report