Linux Learning: Find command details

Source: Internet
Author: User

Find command:
In Linux file management, the Find command is a powerful real-time lookup tool that completes file lookups by traversing the file system hierarchy under a specified path.
Features: Slow lookup (because you are looking from the root), exact lookups, real-time lookups.

Usage:
Find [OPTION] [Find starting Path] [search criteria] [Handling Action]
Find the starting path: Specify the specific search target starting path, default to the current directory;
Find criteria: The specified lookup criteria can be based on file name, size, type, affiliation, permissions, and so on, the default is to find all the files under the specified path;

    1. Search criteria: BR style= "Text-align:left;" >            Expressions: Options and tests
            Test: The results are usually Boolean ("true", "false")     

Search by file name:
-name "pattern"
For example: Find/etc-name "passwd" #精确匹配文件名, a file named passwd
-iname "pattern": does not differentiate case in file names, supports GLOB-style wildcard characters *,?,[],

For example:
Find/etc-iname "passwd"#不区分大小写查找passwd命名的文件或目录
Find/etc-iname "*PASSWD"#查找文件名以passwd结尾的文件or directory
Find/etc-iname "passwd*"#查找文件名以passwd开头的文件or directory
-regex pattern; finding based on regular expressions
Find based on file affiliation:
-user USERNAME: Find all files belonging to the owner of the specified user;
Example: Find/tmp-user Shakura#查找tmp目录下shakura用户的文件or directory
-group USERNAME: Finds all files belonging to the specified group;

Example: Find/tmp-group root # Find a file or directory under the root of the TMP directory

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

Example: Find/tmp-uid 1003 # Find a file or directory with a user ID of 1003 under the TMP directory

-gid GID: Finds all files belonging to the specified GID group;
-nouser: Find files that are not owned by the master;

-nogroup: Find files without a group;

Note: If you delete an account in the system, but the account has created a lot of files in the system, you can use the-nouser command to find these files;

For example: Find/-nouser(#-nogroup) #在根目录下查找没有属主, a group of files or directories
Find by File type:
-type type

The main types of files are: normal file (f), directory file (d), Symbolic Connection file (L),

Device file (b: Block device file, C: Character device file), socket file (s), pipe file (p)

Example: Find/var-type s #查找/var directory file type socket file

Combination test:
With:-A
Or:-O
Non:-not

Find by File Size:
-size[+|-] #UNIT
Common units: K,m,g
#UNIT: (#-1, #] #以50k为例, less than 50k small 1 k or equal to 50k files

-#UNIT: [0,#-1] #以50k为例, smaller than 50k files (not included)

+ #UNIT: (#,+oo) #以50k为例, larger than 50k files (not included)

Find by Time stamp:
In "Day" units:-mtime,-ctime,-atime

-atime [+|-]#:n
#:[#,#-1) #天之前 files accessed "within one day"

+#: (Oo, #-1] #天之前 (No # days) accessed files

-#: (#, 0] #天之内 (with # days) the files that were accessed
In "Minutes" units:-amin, Mmin, Cmin; (Ibid., no longer repeat here)
To find according to permissions:
-perm [+|-]mode
Mode: precise permission matching;

For example: Find the Ask price or directory with a permission of 755, can only match files with file permissions of Rwxr-xr-x

+mode: Any one of the user (U,g,o) permissions (r,w,x) satisfies the condition;
There is a "or" relationship between 9-bit permissions;

For example: Find a file with a permission of 755, that is, rwxr-xr-x, and the file with the permission of RW-------also satisfies the condition.

-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" relationship between 9-bit permissions;

For example: Find a file with a permission of 744, that is, rwxr--r--, and a file with a permission of rw-r-xr-x also satisfies the condition.



2. Handling Actions

Actions that are made on files that match the find criteria, such as deletions, are output to standard output by default.
-print: Output to standard output, default action;
-S: Similar to executing the "ls-l" command on the found file, the output file details;
-delete: Delete the found file;

Find-name 1.txt-delete #查找文件名为1. txt files and deletes

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

For example:
[Email protected] ~]# Find/var/log-fls/1.txt | Cat/1.txt
786433 4 drwxr-xr-x root root 4096 Dec 19:01/var/log
786437 4 drwx------2 root root 4096 Mar 2015/VAR/LOG/PPP
786451 4 drwx------2 root root 4096 Jul 19:53/var/log/httpd
786448 4-rw-------1 root root 591 Dec 19:01/var/log/maillog
786473 8-rw-r--r--2 root root 6772 Dec 19:01/var/log/boot.log

# Find files in/var/log directory, save long format information of file information to 1.txt file, check with cat command

Look at the contents of the 1.txt file, output the information as shown;

-ok COMMAND {} \; : Executes commands for each file that is found, and each operation is confirmed with the user;
For example: Find/-nouser-a-nogroup-ok chmod root:root {}\;

# Find files in the root directory that are not owned, belong to a group, and will belong to the owner and belong to the group root

-exec command{}\; : Executes command-directed commands on each file found; Direct modification complete

Find./-perm/002-exe c mv {} {}.dangereous \;
        # Find files with Write permissions for other users and add suffixes after the file name dangerous
Note: When find passes the file path found to the following command, it is the first to find out all eligible file paths and pass them to the following command at once. However, some commands can not accept too long parameters, when the command execution will fail, another way to circumvent the problem;

This article is from the "Antarctic Bear" blog, please be sure to keep this source http://jianweixs.blog.51cto.com/9095175/1726537

Linux Learning: Find command detailed

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.