A daily Linux command (--find) command

Source: Internet
Author: User
Tags ack

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. Regardless of the current path, if you want to find files in your root directory that match the *.log file name, 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

To find files with a file name beginning with a capital letter in the current directory, you can use:

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 Note: ~ That is $HOME user home directory

To get the system running at a high load, look for all the files starting with the 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:

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

2. Using 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:

Find. -perm 755-print

Alternatively, precede the octal data with a bar to indicate that all matches, such as 007 equals 777, 005 equals 555.

Find. -perm-005

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 Ask price in the test directory, but do not want to find it in the Test/test3 directory, you can use:

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

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

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

Description

Find [-path ...] [Expression]

After the path list 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.

-path "Test" is true,-prune is evaluated,-prune returns TRUE, and logical expression is true, otherwise no value-prune and 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

Find test \ (-path test/test4-o-path test/test3 \)-prune-o-print Comment: Parentheses denote the union 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

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

5. Use the user and Nouser options:

Find files by file owner:

Example 1. Find files in $HOME directory The main file is ACK

Find ~-user ack-print

Example 2: Find file in/etc directory the main ACK file

Find/etc-user Ack-print

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

Find/home-nouser-print

Note: This will allow 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 to which the file belongs.

7. Find files by change time or access time

If you want to change the time to find files, you can use the Mtime, Atime, CTime options, if the system suddenly has no space available, it is likely that the length of a file grows rapidly during this period, you can use the Mtime option to find such a file.

Use minus-to limit the time to change the file within the current n days, and use the Plus + to limit the change time in the file before the current n days

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

Find/-mtime-5-print

To find files that change time before 3rd in the/var/dam directory, you can use:

Find/var/adm-mtime +3-print

8. Find newer or older files than a file

You can use the-newer option if you want to find all files that have changed more than one file but are newer 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 time than file 001.log new but older than file 002.log file

Command:

Find-newer 001.log! -newer 002.log <<<<<<<<<<<<<<<<<<<

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

Find. -newer 002.log-print

9. Use the Type option:

Example 1: Find all directories in the/etc directory

Find/etc-type D-print

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

Find.  ! -type D-print

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

Find/etc-type L-print

10. Use the size option:

Files can be found according to the length of the file, the length of the file referred to here can be measured in blocks (block), or you can use your own to measure. 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

Find. -size +1000000c-print

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

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)

Find. -size +10-print

11. Use 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 a file system to tape, you want to back up all of the files first, followed by the files in the subdirectory.

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

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

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

12. Use 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 that are located in the file system with the end of the file name XC

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

----------This lesson is so long today, my neck is sour.

    

A daily Linux command (--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.