Linux Find command

Source: Internet
Author: User

Introduction

The Find command is one of the most important commands in Linux and can be used to find files and perform specific operations on the files that are found, using a very frequent command, followed by a look at some of its main uses.

Grammar

Find < Table of Contents > < search methods > [< execution Action;]

Directory list

Directory list is the directory of Find search, directories can be arbitrarily specified, you can specify a number of directories separated by a space, the current directory can be used ".", if you want to traverse a subdirectory of the specified directory can use the-P option, in CentOS, the Find command will traverse the sub-directory by default

Search method

1) According to the name of the file search, is also the most commonly used search method,

-name < file name > for example:

" vcsa* "

-iname < file name;: Case of no file name

Note: File names can be queried using wildcards, and file names using double quotation marks

2) According to the file type of retrieval, can be used alone can also be placed after-name, where the file type is actually the first character of the file permissions.

-type < file types >

    • F: General Documents
    • D: Catalog file
    • C: Character device file
    • B: Block device files
    • P: Pipeline File
    • L: Symbolic Link

For example:

" vcsa* " -type C

3) Search by Time

    • Access Time (-atime (24 hours),-amin (min)): The user's last access time.
    • Modified Time (-mtime (24 hours),-mmin (min)): The last time the file was modified.
    • Change Time (-ctime (24 hours),-cmin (min)): File data elements (such as permissions, and so on) last modified time.
    • -newer < file name;: Query

      Modify a file that is more recent than the < file name > specified file

You can use the Stat command to query for these three times of a file:

Access:atime, reading a file modifies this time, such as cat, more, and less. The LS, stat command does not modify the file access time.

Modify:mtime, modification and modification of the file will update this time, such as Vim operation, this time is the default file display time.

Change:ctime, the time is updated for modifying the file properties, such as the Chmod,chown operation

Attention:

-mtime-1: Files modified in one day, such as the current time is 2016-05-09 20:00:00, then-mtime-1 can only query to the modified time is [2016-05-08 20:00:00-2016-05-09 20:00:00] files.

-mtime 1: The modified time is exactly 1 days before the day of the file, earlier than the current time-24 hours of the day's files, such as the current time is 2016-05-09 20:00:00, then-mtime 1 can only query to the modified time for [2016-05-08 0:00:00-2016-05-08 19:59:59] 's file.

-mtime + 1: Files modified a day ago, such as the current time is 2016-05-09 20:00:00, then-mtime +1 can only query to the modified time is 2016-05-07 23:59:59 before the file.

In fact, these three parameters of mtime exactly include all time values that are less than or equal to the current time.

Example: Query starts with 2016 and the modified time is exactly 1 days before the file directory

" 2016* " 1

4) Search by file size

-size < file size;: The default unit is K, you can specify the following units, you can also use +-in front of the file size to match files that represent greater or smaller than the current size, by default equals

B: Block (512Byte)

C:byte

W: Word (2Byte)

K:kb

M:mb

G:gb

For example:

1. In the current directory, the query begins with 2016 directories with a size equal to 4096 bytes.

" 2016* "  -type d-size 4096c

2. In the current directory, query for a directory with a size greater than 4096 bytes at 2016.

" 2016* "  -type d-size +4096c

3. In the current directory, the query begins with 2016 directories with a size smaller than 4096 bytes.

" 2016* "  -type d-size-4096c

-empty: Query file size 0byte file or file directory below is an empty directory

Find. -type d-size +4000c-empty

Note: The retrieval access involved in the retrieval method can be used alone or combined.

The above < search methods > can also be combined by logical operations. These logical operations include:

    • With: that is, and. operation, without special operator expression, only the search method should be listed, the middle with a space to separate.
    • Or: the. Or. operation, using the operator-O, to place "-o" between the two search criteria when two search conditions are "or" relationships.
    • No: that is. not. operation, with operator!

You can use () to set some conditions as a combination, but () you need to write them as "\ (" and "\") with the escape character "\".

For example, a query is not a directory that starts with 2016 or name

" 2016* " " name* " \)-type D

5) Other search conditions:

-link  < number >: Retrieve-uid < owner user id> based onthe number of files connected : -user < Owner account name >  : retrieving-gid < groups according to the owner of the file id>: Retrieve -group < group name >: Retrieved According to the group name to which the file belongs-fstype < file system type >: Search by File system type -maxdepth< directory hierarchy >: Set maximum directory level-mindepth< directory level;: Set minimum directory level

Perform actions

-printf< output format: Assuming that the return value of the Find directive is ture, the file or directory name is listed to standard output. The format can be specified by itself;
-exec < command > \: If you want to use the retrieved file name in the command, use the symbol "{}" instead, pass the retrieved file name to the command through {}, and be careful not to miss the "\;"

For example:

1. Delete the found empty file

Find. -empty-type f-exec rm {} \;

2. Modify the directory and sub-file permissions starting with 2016 to 750

" 2016* "  {} \;

3. In combination with grep, find the number of rows with a file AAA starting with a

" AAA " ' ^a ' {} \; |wc-l
Summary

"-exec {} \;" is required to perform operations on the found file in Find. With the exception of wc-l operations.

Note:

pursuer.chen

Blog:http://www.cnblogs.com/chenmh

This site all the essays are original, welcome to reprint, but reprint must indicate the source of the article, and at the beginning of the article clearly give the link.

Welcome to the exchange of discussions

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.