Linux Basics Primer--find (file lookup)

Source: Internet
Author: User

File Lookup: Find eligible files on the file system, provided that you


Implementation Tools on Linux systems: Locate,find


Locate

Depending on the implementation of the built-in index library, the system is automatically implemented, once the file changes need a certain period of time to update. You can also update the data manually. The entire file system needs to be traversed during the index build process, which consumes resources extremely.


Working characteristics:

1. Fast query speed

2. Fuzzy Search

3, non-real-time search


Locate can't be found in real time so that's it.


============================================================


Find

Real-time Lookup tool to complete file lookups by traversing the file system hierarchy under the specified starting path


Operating characteristics:

1, the search speed is slightly slow

2. Exact search

3, real-time search


Usage:

Find [OPTIONS] [Find starting path] [find condition] [processing action] (e.g. using LS action requires-ls)

Find the starting path: Specify a specific search target starting path, default to the current directory;

Find criteria: Specify the search criteria, can be based on file name, size, type, affiliation, permissions and other criteria, the default is to find all the files under the specified path;

Handling actions: Actions that are made on files that match the search criteria, such as deletions, which are output to standard output by default;


Search criteria:

Search by file name:

-name "pattern"

-iname "pattern":

Supports GLOB-style wildcard characters;

*, ?, [], [^]


-regex pattern: Find a file based on the regular expression pattern, matching is the entire path, and the file name;

Find based on file affiliation:

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

-group GRPNAME: Finds all files belonging to the specified group of groups;


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

-gid 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 without a group;


Find by File type:

-type Type:

F: Normal file

D: Catalog file

L: Symbolic Link file

B: Block device files

C: Character device file

P: Pipeline File

S: Socket file


Combination test:

With:-A, default combination logic;

Or:-O, meet the conditions one of them

Non:-not,!


! A-a! B =! (A-o B)

! A-o! B =! (A-a B)


The combination test compares around, look. If you do not understand Baidu it!

Xiao Ming is not a clever and diligent child: ___

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/98/09/wKioL1k2bz3jTGR8AAAh6GvubVc866.jpg "title=" Zuhe.jpg "alt=" Wkiol1k2bz3jtgr8aaah6gvubvc866.jpg "/>



Practice:

1, find out the/tmp directory subordinate to the main non-root all files;

2. Find the file name in the/tmp directory that does not contain the fstab string;

3, find out the/tmp directory subordinate Master is non-root, and the file name does not contain fstab string files;


For:

1, [[email protected] sh]# Find/sh-not-user root

2, [[email protected] sh]# find/sh-not-name "*fstab*"

3, [[email protected] sh]# find/sh-not-user root-a-not-name "*fstab*"



Find by File Size:

-size [+|-] #UNIT)

Common units: K, M, G


#UNIT: (#-1, #], exact match without +/-number

-#UNIT: [0,#-1], does not contain the given value

+ #UNIT: (#, OO), greater than the given value, and infinity


Find by Time stamp:

In "Days" as the unit:

-atime [+|-]#

#:[#, #-1)

-#: (#, 0]; #天内

+#: (Oo, #-1]; #天外, not including #

-mtime

-ctime


Timestamp, which was said in the previous article Stat command.



In "Minutes" units:

-amin

-mmin

-cmin


Search by permissions:

-perm [/|-]mode

Mode: precise permission matching;

/mode: Any one (r,w,x) of the permissions of any class of users (U,g,o) is satisfied with the condition;

There is a "or" relationship between 9-bit permissions;

Note: Any class has, in turn, expressed: none.

-mode: Each class of users (U,g,o) in the permissions of each bit (r,w,x) at the same time meet the condition is satisfied;

There is a "and" (and) relationship between 9-bit permissions;

Note: All have, conversely, at least one class without



Handling actions:

-print: Output to standard output, default action;

-ls: Similar to the "ls-l" command for the found file, the output file details;

-delete: Delete the found file;

-fls/path/to/somefile: Saves the long format information of all files found to the specified file;

-ok COMMAND {} \; : Executes commands for each file that is found, and each operation is confirmed by the user;

-exec COMMAND {} \; : Executes commands for each file that is found;


Note: Find passes the file path found to the following command, it is first to find out all eligible file paths, and one-time pass to the following command;

However, some commands cannot accept too long arguments, at which point the command execution fails, and another way to circumvent this problem:

Find | Xargs COMMAND



Practice:

1. Find the main root of the/var directory, and belong to all files or directories of mail;

[Email protected] ~]# find/var-user root-a-group mail-ls


2. Find all files or directories that do not belong to root, bin or hadoop under the/usr directory; in two ways;

[Email protected] usr]# find/usr-not-user root-a-not-user bin-a-not-user hadoop-ls

[Email protected] usr]# find/usr-not \ (-user root-o-user bin-o-user hadoop \)-ls



3, find in/etc directory in the last week of its content has been modified, and the owner is not the root user is not a Hadoop user files or directories;

[Email protected] usr]# find/etc-mtime-7-a-not \ (-user root-o-user hadoop \)-ls

[Email protected] usr]# find/etc/-mtime-7-A! -user root-a! -user Hadoop

4. Find files or directories that are not in or belong to the current system and have been visited in the last week;

[[email protected] usr]# Find/\ (-nouser-o-nogroup \)-atime-7-ls (because-ls is very special, sometimes you need to say the query conditions, you can use-ls)

[[email protected] usr]# Find/-not-nouser-o-nogroup-a-atime-7-ls



5. Find all files that are larger than 1M and type ordinary files in/etc directory;

[Email protected] usr]# find/etc/-size +1m-a-type f-exec ls-lh {} \;



6, look for all the users in/etc directory do not have permission to write files;

[Email protected] usr]# Find/etc-not-perm/222-type F-ls



7, Find/etc directory at least one class of users do not have permission to execute files;

[Email protected] usr]# Find/etc-not-perm-111-type F-ls



8, find/etc/init.d/directory, all users have execute permission, and other users have write permission to all files;

[Email protected] usr]# Find/etc-perm-113-type F-ls

[Email protected] usr]# find/etc/init.d/-perm-111-a-perm-002-ls



This article from "Disguised geek" blog, declined reprint!

Linux Basics Primer--find (file lookup)

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.