The carding and induction of the Find command

Source: Internet
Author: User
Tags glob

All documents, this is the highest philosophical thought of Linux, doomed to the study of the document is one of the key points of Linux learning; we deal with all kinds of documents every day, but the file system is complicated, and the filenames are numerous, do you often encounter the path or file name of forgetting a file? I think this is an unavoidable problem, so there is a fast and accurate and efficient file finder is of great significance to us, then comb two of our daily use more files Find tool, convenient for their future search and use;

The first is the Locate command: FindFiles by name

Locate is a tool that relies on a pre-built index library for file lookups with the following three features:

1. Fast Lookup: The reason why locate has efficient search efficiency is that locate uses a pre-built index library for retrieval, and does not need to traverse the file system;

2. non-real-time lookup: But precisely because locate uses the index library to retrieve, the index library is the system periodically updates, therefore locate cannot do the real-time search;

3. Fuzzy Search: This is very good understanding, locate in the search, do not need to do the exact match, the file name contains the pattern string can be;

Note: We can also use the UpdateDB command to update the file index library manually, but updating the index library requires traversing the entire file system, which is extremely resource-intensive;


Format and common options:

Format: Locate [OPTION] ... PATTERN ...

Common options:

- b : Matches only the file base name (basename);

- C : Count the number of files that match the criteria option;

- R,--regexp : Supports the format of regular expressions for retrieval;

In general, the locate command has obvious advantages, but also has a prominent short board; Next, more common, more widely used, more powerful file finder: find;


Find : Real-time Lookup tool to complete file search by traversing the file system hierarchy under the specified starting path;


Operating characteristics:

1. the lookup speed is slightly slow: because the entire file system needs to traverse, so the speed relative locate slower;

2. Exact Search: In addition to string matching, there are a lot of matching conditions, you can do relatively accurate search;

3. Real-time search: Directly traverse the file system, real-time search;

Usage: Find [OPTIONS] [Find starting path] [find condition] [processing action];


To find the starting path:

Specifies the path of the specific search target, by default it iterates through the root directory, which helps to reduce the scope of the lookup, improve efficiency, less resource consumption;

Search criteria:

The specified lookup criteria, based on file name, size, type, affiliation, permissions, etc., by default to find all files under the specified path;

match file name/pattern/;

-name "PATTERN" support glob wildcard mechanism;

-iname "PATTERN" is not case-sensitive; supports glob wildcard mechanism;

-regex Pattern : Finds a file based on a regular expression, matching the entire path, not the base name;

Find according to the file's dependencies:

-user : Find all files belonging to the owner of the specified user;

-group : Finds all files that belong to a specified group;

-uid : Finds all files that belong to the specified UID of the master;

-gid : Finds all files belonging to the specified GID of the group;

-nouser : Find files that are not owned by the master;

-nogroup : Find files that are not part of a group;

Find by File type:

-type : f;d;l;b;c;p;s;

Combination test:

With:-A, default combination condition;

Or:-O, a condition to meet on the line;

Non:-not,! ;

For example: Find the TMP directory subordinate to the main is not root, and the file name contains fstab character files;

find/tmp-not \ (-uid 0-o-iname "*fstab*" \) ;

Root Search by file size:

-size[+|-]=unit, common units: k,m,g;

#unit : (#-1,#]

-#unit : [0,#-1]

+ #unit:(#,oo) ;

To find by time stamp:

In days: (The following # All indicate the past time, for a negative better understanding;)

-atime :

# : (#-1,#]

-#:(#,0]

+#:(Oo,#-1]

-mtime :

-ctime :

In minutes:

-amin :

-mmin :

-ctime :

To find according to permissions:

-perm (/|-) mode:

Mode : exact permission matching;

/mode : Any one of the permissions of any class of users (U,g,o) is eligible and satisfied; there is a "or" relationship between nine-bit permissions;

-mode : Each of the user's permissions meets the criteria at the same time; there is a "and" relationship between nine-bit permissions

Note: Find/path/to/file-perm/|-###/And-two options, 0 of the permission bit can be ignored directly, do not need to match;

Handling actions: to the eligible files, for example, delete (-delete), the default is output to the standard output, often use is listed (-LS) to find the file specific information;


Handling actions;

-print: output to standard output, default action;

-ls : Similar to the "ls-l" of the file to be found, the details of the output file;

-delete : Delete, Danger!

-okcommand {} \ ;: Executes command commands on each file found, and each operation is confirmed by the user;

-exec COMMAND {} \; : Execute command directly on the found file, no interactive action;

Several exercises:

Find all files or directories that do not belong to Root,bin or Hadoop under the/usr directory, two methods;

Find the most recent week in the/etc directory has been modified, and the owner is not root or hadoop files or directories;

Find files or directories that have been visited in the last week of the system and are not owned by the master or group;

Note that this adds-O has the potential to affect the following options;

Find directories that are larger than 1M and type as normal files under/etc;

Find files that all users do not have write permission under/etc;

Find at least one class of users who do not have permission to execute files under/etc;

Find the/etc/init.d/directory, all users have execute permissions, and other users have write permission to all files;


The carding and induction of the 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.