A brief analysis of Find command

Source: Internet
Author: User

There are many functions for querying on Linux systems. However, find is one of the best commands under Linux and its query functionality is very powerful. Next, let's look at its strengths.


    • The first is the syntax of find:

    • Find [Start directory] Find conditional actions



    • The most uses of find are described in detail below:

    • How it works: it traverses down the file hierarchy, matches files that match the criteria, and performs the appropriate actions


    1. Print a list of files and directories

      650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/80/52/wKiom1c9wO3AL-91AAAhkKdERLc830.png "title=" 1.PNG " alt= "Wkiom1c9wo3al-91aaahkkderlc830.png"/>


      This shows that find. -print #打印文件和目录列表

      When using-print, ' \ n ' as the delimiter used to separate files.

      -print0 indicates that each file name that matches is printed with '% ' as a delimiter. This method is very useful for filenames that contain ' \ n '.

However, in the actual test on CentOS, find directly shows the matching files, do not need to add the-print option.


2. Match search by file name or regular expression

Option-name Specifies the string that the file name must be skinned.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/80/52/wKiom1c9wb7SZoj1AAAoy6D5mzI100.png "style=" float: none; "title=" 2.PNG "alt=" Wkiom1c9wb7szoj1aaaoy6d5mzi100.png "/>

Print files ending in. txt


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/80/4F/wKioL1c9wqqxwR2lAAAfm7wv3vU961.png "style=" float: none; "title=" 3.PNG "alt=" Wkiol1c9wqqxwr2laaafm7wv3vu961.png "/>

-iname indicates that a matching file name ignores case


3. Condition limit, match one of multiple conditions

-A: Represents and

-O: Indicates or

-not: Indicates non-

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/80/52/wKiom1c9wmKhSudOAAAhK2c7uh0656.png "style=" float: none; "title=" 4.PNG "alt=" Wkiom1c9wmkhsudoaaahk2c7uh0656.png "/>

Print files ending in. txt or. pdf in the current directory

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/80/50/wKioL1c9w07D_wvWAAAcZbdtL3c404.png "style=" float: none; "title=" 5.PNG "alt=" Wkiol1c9w07d_wvwaaaczbdtl3c404.png "/>

Print a file that starts with a and ends in a. txt in the current directory

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/80/50/wKioL1c9w0_gVnLUAAAnovrRY70929.png "style=" float: none; "title=" 6.PNG "alt=" Wkiol1c9w0_gvnluaaanovrry70929.png "/>

Prints files in the current directory that do not start with a and end with a. txt

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/80/52/wKiom1c9wmOyElWlAAAifa_zuC8693.png "style=" float: none; "title=" 7.PNG "alt=" Wkiom1c9wmoyelwlaaaifa_zuc8693.png "/>

Print a file that does not end with TXT in the current directory or a file that ends in a PDF


-path: This parameter is used to match the file path as a whole.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/80/50/wKioL1c9xLjRc7coAAAc_ZbiCkU468.png "title=" 8.PNG " alt= "Wkiol1c9xljrc7coaaac_zbicku468.png"/>

-regex: This parameter is similar to-path, except that it matches a file path based on a regular expression.

$find. -regex ". *\ (\.py \| \.sh \) $ "

4. Negative parameters

Find can also be used "! "As a negative argument, how does it do that?"

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/50/wKioL1c9xSfSRsedAAAmyNFgiTg849.png "title=" 9.PNG " alt= "Wkiol1c9xsfsrsedaaamynfgitg849.png"/>

, matching all files not ending with. txt

5. Directory-based Deep search

When the find command iterates through all subdirectories, we can use some parameters to limit the depth of its traversal.

-maxdepth: Maximum depth limit, 1 means only in the current directory, 2 is two levels down. And so on

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/80/52/wKiom1c9xLGz6_uuAABNiV8jI8Q590.png "title=" 10.PNG "alt=" Wkiom1c9xlgz6_uuaabniv8ji8q590.png "/>

$find. -maxdepth 1-type F-print #只列出当前目录下的所有普通文件

$find. -mindepth 2-type F-print #列出距当前目录至少两个子目录的所有文件


-mindepth: Minimum depth limit, similar to the above usage.


6. Search by File type

Types of files under Linux

File Type type parameter

Normal file F

Symbol File L

Catalog D

Character Device C

Block Device B

Socket s

FIFO (pipe file) p

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/52/wKiom1c9xPrAQZjHAAAkQ0eoLaA219.png "title=" 11.PNG "alt=" Wkiom1c9xpraqzjhaaakq0eolaa219.png "/>

Because this usage is mostly consistent, it is not listed here.


7. Search by file time

Each file in the Linux file system has three timestamps:

Access time (atime--access time): Is the date the file was last accessed.

Change Time (Ctime--change): Is the time of the file's inode change (what is the inode?). When you add data or delete data to a file, when you change the file owner, the CTime of the file changes when the link changes.

Modified (mtime--modification time): Is the time when the content of the file was last changed.

-atime,-ctime,-mtime can be used as a time parameter for find. Units are days, and these values usually also have a + or-:+ representation greater than,-indicating less than.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/80/52/wKiom1c9xYbifwM8AAA9jOWfgio709.png "title=" 12.PNG "alt=" Wkiom1c9xybifwm8aaa9jowfgio709.png "/>

By the Text.txt, the rest of the day was created. If the reader is not familiar with it, it can be understood by looking at the date behind it.

The above time is based on "days", and also based on "minutes".

-amin

-mmin

-cmin

Since the usage is similar to the above, again it is not an example.

Another nice feature of find is the-newer parameter, which allows us to specify a reference file to compare the timestamp, and then find all the files that are newer than the referenced file.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/80/50/wKioL1c9xv6j85UmAAAeCfAUihw820.png "title=" 13.PNG "alt=" Wkiol1c9xv6j85umaaaecfauihw820.png "/>


8. FILE-size-based search

-size: Usage is similar to the above

$find. -type f-size +2k #查找大于2k的文件

$find. -type f-size 2k #查找等于2k的文件

$find. -type f-size-2k #查找小于2k的文件

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/80/50/wKioL1c9x1qwU5R9AABLZ-98QC8011.png "title=" 14.PNG "alt=" Wkiol1c9x1qwu5r9aablz-98qc8011.png "/>

In addition to K, there are other file size units

b--block (512 bytes).

c--bytes.

w--Word.

k--kilobytes.

m--megabytes.

g--gigabytes.


9. Delete a matching file

See here, you may be puzzled, find is clearly a query command, how to do delete it? Then let's see how it's going to come true

The-delete parameter allows this functionality to be implemented.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/80/50/wKioL1c9x9PTlWD7AAAp9WwhusU875.png "title=" 15.PNG "alt=" Wkiol1c9x9ptlwd7aaap9wwhusu875.png "/>


10. Based on file permissions and ownership memory matching

-perm:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/80/52/wKiom1c9xyaQ9loIAAAq1280jlo680.png "title=" 16.PNG "alt=" Wkiom1c9xyaq9loiaaaq1280jlo680.png "/>

-user: After the user name or UID can be solved

such as $find. -type f-user Root-print # Print files owned by the root user

It is no longer listed here, the reader can try it out on their own, then know.


11. Execute commands or actions with find

-exec: This parameter is one of the most powerful parameters of find.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/80/50/wKioL1c9yNiw5RLKAAA4QC-So1U021.png "title=" 17.png "alt=" Wkiol1c9yniw5rlkaaa4qc-so1u021.png "/>

#将所有c程序拼接起来写入单个文件file. txt


12. Let find skip a specific directory

$find. \ (-name ". Git"-prune \)-o \ (-type f-print \)



13.find combined with Xargs

Xargs: Converts standard input to command-line arguments.

$find. -type f-name "*.txt"-print0 |xargs-0 rm-f #将匹配到的文件删除, xargs-0 as input delimiter.

$find. -type f-name "*.C"-print0 | Xargs-0wc-l #统计所有c程序文件的行数


I summarize probably so much, if there is a supplement or insufficient, please point out a lot, and revise. As the saying goes, the good open source is a kind of spirit, sharing is a kind of virtue. So make progress together.


A brief analysis of 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.