The usage practice of the Linux Find command

Source: Internet
Author: User
Tags uppercase letter

Find some common examples of common parameters and some specific usage and considerations.

1. Use the name option:

The file name option is the most common option for the Find command, either used alone or in conjunction with other options.  You can use a file name pattern to match files, remembering to enclose the filename pattern in quotation marks. No matter what the current path is, if you want to find the file name in your root $home that matches *.log, use ~ as the ' pathname ' parameter, and the tilde ~ represents your $home directory.

Find ~-name "*.log"-print

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

Find. -name "*.log"-print

You want the current directory and subdirectories to find file names that begin with an uppercase letter, which can be used:

Find. -name "[a-z]*"-print

To find files with the file name beginning with host in the/etc directory, you can use:

Find/etc-name "host*"-print

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

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

To get the system running at a high load, start looking for all the files from the root directory.

Find/-name "*"-print

If you want to find the file name in the current directory starting with a lowercase letter, the last file that is 4 to 9 plus. Log Ends:

Command:

Find. -name "[A-z]*[4-9].log"-print

Output:

[email protected] test]# LL

Total 316

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

-rw-r--r--1 root root 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

[[email protected] test]# find. -name "[A-z]*[4-9].log"-print

./log2014.log

./log2015.log

./test4/log2014.log

[Email protected] test]#

2. With the PERM option:

Follow the file permission mode with the-perm option to find files by file permission mode. It is best to use the octal permission notation.

For example, in the current directory to find file permission bit 755 file, that is, the file owner can read, write, execute, other users can read, execute files, can be used:

[[email protected] 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

[Email protected] test]#

There is also a way of expression: in front of the octal number to add a bar-, the expression is matched, such as 007 is equivalent to 777,-005 equivalent to 555,

Command:

Find. -perm-005

Output:

[email protected] test]# LL

Total 316

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

-rw-r--r--1 root root 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

[[email protected] 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

[Email protected] test]#

3. Ignore a directory:

If you want to ignore a directory when you're looking for a file, because you know that directory doesn't have the file you're looking for, you can use the-prune option to indicate which directories you want to ignore. Be careful when using the-prune option, because if you use the-depth option at the same time, the-prune option is ignored by the Find command. If you want to find the file under the test directory, but do not want to find it in the Test/test3 directory, you can use:

Command:

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

Output:

[Email protected] 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

[Email protected] soft]#

4. How to avoid a file directory when finding files using find:

Example 1: Find all files in the test directory that are not within the TEST4 subdirectory

Command:

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

Output:

[[email protected] 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

[Email protected] 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

[Email protected] soft]#

Description

Find [-path ...] [Expression]

After the list of paths is the expression

-path "Test"-prune-o-print is a shorthand expression for-path "test"-a-prune-o-print, evaluated sequentially,-A and-O are short-circuit evaluated, with Shell's && and | | Similar if

-path "Test" is true, the-prune is evaluated,-prune returns TRUE, and the logical expression is true, otherwise no value-prune, and the logical expression is false. If the-path "test"-a-prune is false, the evaluation is-print,-print returns True, or the logical expression is true, otherwise no value-print, or the logical expression is true.

This combination of expressions can be written in pseudo-code:

If-path "Test" then

-prune

Else

-print

Example 2: Avoid multiple folders:

Command:

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

Output:

[[email protected] 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

[Email protected] soft]#

Description

Parentheses represent the combination of expressions. \ denotes a reference, which instructs the shell not to give a special explanation of the characters that follow, leaving the Find command to explain its meaning.

Example 3: Find a certain file,-name and other options after-O

Command:

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

Output:

[[email protected] 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

[Email protected] soft]#

5. Use the user and Nouser options:

Find files by file owner:

Example 1: Find files in the $home directory where the file belongs to the master Peida

Command:

Find ~-user peida-print

Example 2: Look for files in the/etc directory where the file belongs to the main peida:

Command:

Find/etc-user Peida-print

Description

Example 3: To find files that are already deleted by the master account, you can use the-nouser option. Find all of these files in the/home directory

Command:

Find/home-nouser-print

Description

This will enable you to find files that are not valid accounts in the/etc/passwd file. When using the-nouser option, you do not have to give the user name; the Find command can do the work for you.

6. Use the group and Nogroup options:

Just like the user and Nouser options, the Find command has the same options for the group of users that the file belongs to, in order to find files belonging to the Gem User group in the/apps directory, you can use:

Find/apps-group Gem-print

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

Find/-nogroup-print

7. find files by time of change or access time:

You can use the Mtime,atime or CTime option if you want to find the file by changing the time. if the system suddenly does not have free space, it is possible that the length of a file grows rapidly during this period, you can use the Mtime option to find such a file.

Use a minus sign-to limit the time to change the file within the current n days, and use the Plus + to limit the change time before the current n days of the file.

To find files that change within 5th of the system root directory, you can use:

Find/-mtime-5-print

In order to find files that change time before 3rd in the/var/adm directory, you can use:

Find/var/adm-mtime +3-print

8. To find new or older files than a file:

You can use the-newer option if you want to find all files that have changed time than one file but older than the other.

The general form of it is:

Newest_file_name! Oldest_file_name

Among them,! is a logical non-symbol.

Example 1: Find changed times than files Log2012.log new but older files than files Log2017.log

Command:

Find-newer Log2012.log! -newer Log2017.log

Output:

[email protected] test]# LL

Total 316

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

-rw-r--r--1 root root 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

[[email protected] test]# find-newer Log2012.log! -newer Log2017.log

.

./log2015.log

./log2017.log

./log2016.log

./test3

[Email protected] test]#

Example 2: Find the change time in a new file than the Log2012.log file

Command:

Find. -newer Log2012.log-print

Output:

[Email protected] test]# Find-newer Log2012.log

.

./log2015.log

./log2017.log

./log2016.log

./test3

[Email protected] test]#

9. Use the type option:

Example 1: Find all directories in the/etc directory

Command:

Find/etc-type D-print

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

Command:

Find. ! -type D-print

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

Command:

Find/etc-type L-print

10. Use the size option:

files can be searched by file length, and the length of the file referred to here can be measured either in blocks or in bytes. The length of the measured file in bytes is expressed as n C, and the length of the block measurement file is only represented by a number.

When looking up files by file length, this is generally the size of the file in bytes, and it is easier to convert by using blocks to measure the file system.

Example 1: Find files with file lengths greater than 1 m bytes in the current directory

Command:

Find. -size +1000000c-print

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

Command:

Find/home/apache-size 100c-print

Example 3: Find a file with a length of more than 10 blocks in the current directory (a piece equals 512 bytes)

Command:

Find. -size +10-print

11. Using the depth option:

When you use the Find command, you may want to match all the files and find them in the subdirectory. Use the depth option to enable the Find command to do so. One reason for this is that when you use the Find command to back up the file system to tape, you want to back up all the files first, and then back up the files in the subdirectories.

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

12. Using the Mount option:

You can use the Mount option of the Find command to find a file in the current file system (without entering another file system).

Example 1: Starting from the current directory find files in the file system with the file name ending in XC

Command:

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

13.

Check all the normal files in the current directory and use the LS-L command in the-e x E C option to list them

# Find. -type f-exec ls-l {} \;
-rw-r–r–1 root root 34928 2003-02-25./conf/httpd.conf
-rw-r–r–1 root root 12959 2003-02-25./conf/magic
-rw-r–r–1 root root 2003-02-25./conf.d/readme

The usage practice of the Linux Find command

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.