Explanation of parameters of common Linux commands (21)-find

Source: Internet
Author: User

Explanation of parameters of common Linux commands (21)-find

I.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.

Find ~ -Name "*. log"-print


To search for all '*. log' files in the current directory and subdirectory, you can use: find.-name "*. log"-print

You can use: find.-name "[A-Z] *"-print

To search for a file whose file name starts with host in the/etc directory, use: find/etc-name "host *"-print

To search for files in the $ HOME directory, use: find ~ -Name "*"-print or find.-print

To make the system run at a high load, search for all the files from the root directory. Find/-name "*"-print

If you want to search for file names in the current directory starting with lowercase letters, add 4 to 9. log end file: find. -name "[a-z] * [4-9]. log "-print


Ii. EnableUse 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:


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: find.-perm-005


III.Ignore a directory-prune:

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.


Instance 1:If you want to search for files in the test directory but not in the test/test3 directory, you can use:

Command: find test-path "test/test3"-prune-o-print

Note:

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. They are similar to shell's & | if-path "test" is true,-prune and-prune return true values, true for the logical expression; otherwise, the value-prune is not required, 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: find test \ (-path test/test4-o-path test/test3 \)-prune-o-print

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: find test \ (-path test/test4-o-path test/test3 \)-prune-o-name "*. log"-print



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: find ~ -User peida-print


Instance 2:Find the file whose owner is peida In the/etc directory:

Command: find/etc-user peida-print

Note: instance 3: You can use the-nouser option to find files that have been deleted by the owner account. Search for all such files in the/home Directory


Command: find/home-nouser-print

Note: in this way, you can find files with no valid accounts 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: 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:

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:

Find/-mtime-5-print

To search for files whose modification time is earlier than 3 days in the/var/adm directory, you can use:

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.

The general format is: newest_file_name! Oldest_file_name where ,! Is a logical non-sign.


Instance 1:Find the files whose change time is earlier than log2012.log but earlier than log2017.log

Command: find-newer log2012.log! -Newer log2017.log


Instance 2:Find the file whose change time is earlier than log2012.log

Command: find.-newer log2012.log-print


9.Use the type option:

Instance 1:Search for all directories under the/etc directory

Command: find/etc-type d-print


Instance 2:Search for all types of files except directories in the current directory

Command: find .! -Type d-print


Instance 3:Search for all symbolic link files under the/etc directory

Command: 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: find.-size + 000000c-print


Instance 2:In the/home/apache directory, find the file with a length of exactly 100 bytes:

Command: 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: 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 disk, 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: 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: find.-name "*. XC"-mount-print






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.