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

How to use the find command of Linux system

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "how to use the find command of the Linux system", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn how to use the find command of the Linux system "this article.

The find command is used to find files in the specified directory. Any string that precedes a parameter is treated as the name of the directory you are looking for. If you use this command without setting any parameters, the find command looks for subdirectories and files under the current directory.

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 find a file with a matching file name in your root directory, use it as a parameter, and the tilde represents your HOME directory.

Example 1: want to find all the'* .log 'files in the current directory and subdirectories

Find. -name "* .log"-print

Example 2: look for files whose names begin with an uppercase letter in the current directory and subdirectories you want

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

Example 3: want to find a file whose name begins with host in the / etc directory

Find / etc-name "host*"-print

Example 4: want to find a file in the $HOME directory

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

Example 5: if you want the system to run under heavy load, look for all the files from the root directory

Find /-name "*"-print

Example 6: you want to find the file name in the current directory that begins with a lowercase letter and ends with a file command that ends with 4 to 9 plus .log:

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

Output:

[root@localhost test] # ll Total 316 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.logdrwxr-xr-x 6 root root 4096 10-27 01:58 Scfdrwxrwxr-x 2 root root 4096 11-13 06:08 test3drwxrwxr-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

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. Example: find a file with a file permission bit of 755 in the current directory, that is, a file whose owner can read, write and execute, and which other users can read and execute

[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

* * there is another way of expression: * * add a horizontal bar before the octal number to indicate that they all match. For example,-007 is equivalent to 777, which is equivalent to 555:

Find. -perm-005

Output:

[root@localhost test] # ll Total 316 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.logdrwxr-xr-x 6 root root 4096 10-27 01:58 Scfdrwxrwxr-x 2 root root 4096 11-13 06:08 test3drwxrwxr-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

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. Example: look for files in the test directory, but do not want to find commands in the test/test3 directory:

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

Output:

[root@localhost soft] # find test-path "test/test3"-prune-o-printtesttest/log2014.logtest/log2015.logtest/test4test/test4/log2014.logtest/test4/log2013.logtest/test4/log2012.logtest/scftest/scf/libtest/scf/servicetest/scf/service/deploytest/scf/service/deploy/producttest/scf/service/deploy/infotest/scf/doctest/scf/bintest/log2013.logtest/log2012.log

How to avoid a file directory when using find to find files

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

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

Output:

[root@localhost soft] # find testtesttest/log2014.logtest/log2015.logtest/test4test/test4/log2014.logtest/test4/log2013.logtest/test4/log2012.logtest/scftest/scf/libtest/scf/servicetest/scf/service/deploytest/scf/service/deploy/producttest/scf/service/deploy/infotest/scf/doctest/scf/bintest/log2013.logtest/log2012.logtest/test3 [root@localhost soft] # find test-path "test/test4"-prune-o-printtesttest/log2014.logtest/log2015.logtest/scftest / scf/libtest/scf/servicetest/scf/service/deploytest/scf/service/deploy/producttest/scf/service/deploy/infotest/scf/doctest/scf/bintest/log2013.logtest/log2012.logtest/test3

Description: find [- path..] [expression] after the path list is the expression-path "test"-prune-o-print is the abbreviated expression-path "test"-a-prune-o-print is evaluated sequentially, and-an and-o are short-circuited, similar to shell's & & and | |. 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-pruneelse-print

Example 2: avoid multiple folders: command:

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

Output:

[root@localhost soft] # find test\ (- path test/test4-o-path test/test3\)-prune-o-printtesttest/log2014.logtest/log2015.logtest/scftest/scf/libtest/scf/servicetest/scf/service/deploytest/scf/service/deploy/producttest/scf/service/deploy/infotest/scf/doctest/scf/bintest/log2013.logtest/log2012.log

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 to the command after-o:

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

Output:

[root@localhost soft] # find test\ (- path test/test4-o-path test/test3\)-prune-o-name "* .log"-printtest/log2014.logtest/log2015.logtest/log2013.logtest/log2012.log

Use the user and nouser options

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

Find ~-user peida-print

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

Find / etc-user peida-print

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

Find / home-nouser-print

* * Note: * * this allows you to find files for which 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.

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. Example: find files that belong to the gem user group in the / apps directory

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:

Find /-nogroup-print

Find files by change time or access time, etc.

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. Example 1: look for files with a change time of less than 5 days in the system root directory

Find /-mtime-5-print

Example 2: look for files whose change time is less than 3 days in the / var/adm directory

Find / var/adm-mtime + 3-print

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:

Newest_file_name! Oldest_file_name

Among them,! It's logical, not symbolic. Example 1: find a file whose change time is newer than the file log2012.log but older than the file log2017.log:

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

Output:

[root@localhost test] # ll Total 316 root root RW root root-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.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxr-x 2 root root 4096 11-13 06:08 test3drwxrwxr-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

Example 2: find a file command with a change time newer than a log2012.log file:

Find. -newer log2012.log-print

Output:

[root@localhost test] # find-newer log2012.log../log2015.log./log2017.log./log2016.log./test3

Use the type option

Example 1: find all directory commands under the / etc directory:

Find / etc-type d-print

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

Find. !-type d-print

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

Find / etc-type l-print

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. When looking up files by file length, you generally use this file length in bytes to look at the size of the file system, because it is easier to convert when using blocks to measure.

Example 1: command to find files with a length greater than 1m bytes in the current directory:

Find. -size + 1000000c-print

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

Find / home/apache-size 100c-print

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

Find. -size + 10-print

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. Example: the find command starts at the root of the file system and looks for a file named CON.FILE. Command:

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

Description: it will first match all the files and then go to the subdirectory to find it.

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: the command to find files whose names end in XC in this file system starting from the current directory:

Find. -name "* .XC"-mount-print is all the contents of this article "how to use find commands in Linux system". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report