Find command usage and examples

Source: Internet
Author: User

Use of the Find command

Find

is a real-time lookup tool to complete file lookups by traversing a file system under a specified path;


There are several job features:

The search speed is slightly slower;

Accurate search;

Real-time search;

Use Syntax:

Find [OPTION] ... [Find Path] [Search Criteria] [Handling Action]


Find path: Specify the target path, default to the current directory;


Search criteria: The specified search criteria, can be file name, size, type, permissions and other criteria, the default is to find

All files under the specified path;


Handling actions: What to do with the eligible files; default output to the screen;


explain the search criteria in detail:


Search by file name:

-name "file name": Support for using Glob

*, ?, [], [^]

-iname "file name": Letter case insensitive

-regex "pattern": matches the entire file path string with PATTERN, not just

is the file name;


According to the genus, the genus Group looks for:

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

-group GRPNAME: Find files that belong to a specified group;

-uid UserID: Find the file that belongs to the UID number specified by the owner;

-gid GroupID: Finds files belonging to the specified GID number;

-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

S: Socket file

B: Block device files

C: Character device file

P: Pipeline File


Combination conditions:

with:-A

or:-O

Non:-not,!


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

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


Example: Find out the/tmp directory, the owner is not root, and the file name is not fstab files;

find/tmp \ (-not-user root-a-not-name ' fstab ' \)-ls

find/tmp-not \ (-user root-o-name ' fstab ' \)-ls


Depending on the file size, look for:

-size [+|-] #UNIT

Common units: K, M, G


#UNIT: (#-1, #)

-#UNIT: [0,#-1]

+ #UNIT: (#,oo)


Based on time stamp:

Take "Day" as the unit;

-atime [+|-]#,

#: [#,#+1]

+#: [#+1,oo]

-#: [0,#]


-mtime (with Atime)


-ctime (with Atime)


In "Minutes" units:

-amin

-mmin

-cmin


Search by permissions:

-perm [/|-]mode

MODE: Exact permission match

/mode: The permission of any class (U,g,o) object can only be matched by one match;

-mode: Each type of object must have a permission standard assigned to it at the same time;


Detailed explanation of handling actions:

-print: The default processing action, display to the screen;

-ls: Similar to executing the "ls-l" command on the found file;

-delete: Delete the found file;

-fls/path/to/somefile: Long format information for all files found is saved to the specified file;

-ok COMMAND {} \; Executes commands specified by the command for each file found;

User confirmation is requested interactively before each file executes the command;

-exec COMMAND {} \; Executes commands specified by the command for each file found;

{}: Used to refer to the file name itself;


Note: When find passes the found file to the command specified later, it finds that all eligible files are passed to the following command one time, but some commands cannot accept too many parameters, and the command execution may fail;

Another way to circumvent this problem: find | Xargs COMMAND


Here are a few exercises to deepen your understanding of find:


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

[Email protected] ~]# Find/var-user root-group mail/var/spool/mail/var/spool/mail/root


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

[Email protected] ~]# find/usr-not-user root-a-not-user bin-a-not-user Hadoop

3, look for/etc directory in the most Monday weeks of its content has been modified, but also the main is not root, nor is the file or directory of Hadoop;

[[Email protected] ~] #find/etc-mtime-7-a-not-user root-a-not-user Hadoop

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

[[email protected] ~]# Find/-nouser-a-nogroup-a-atime-7


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

[Email protected] ~]# find/etc-size +1m-a-type ffind: Invalid-size type "M" [[email protected] ~]# find/etc-size +1m-a-T ype F #注意文件大小的符号/etc/udev/hwdb.bin/etc/selinux/targeted/policy/policy.29[[email protected] ~]#


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

[Email protected] ~]# find/etc/init.d-perm-113

That is, all users have 111 permissions, and other users have W permissions that is (2) namely: F**X**X*WX


Find command usage and examples

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.