Find command details based on file size, name and permissions, and so on search strategy and-path and-prune detailed explanation

Source: Internet
Author: User
Tags chmod stdin
(1) Search by filename and inode:
    -name "file name": Supports the use of Glob
        find/etc/-name "*pas?wd*"
        *,?, [], [^]
    -iname "file name": Case-insensitive
        find/etc/-iname *pas?wd*
    -inum n  Press the inode number
        to find Find-inum
    -samefile name  the same inode number
        Find-samefile
    -links n   files
        find-links 2
    -regex "pattern": matches the entire file path string in pattern, not just the file name
    find/etc/ -regextype Posix-egrep-regex  ". */pa[sa]{2}wd"

Example:

Find-name snow.png  
• Search for files named snow.png  
find-inamesnow.png  
• Case-insensitive search named Snow.png, Snow.png, snow. PNG  and so on file     Find/-  name  "*.txt"   
• Use wildcard characters to locate files ending with. txt
find/var   –    name "*log*"    
• Find all log files in the/var directory

(2) According to the owner, the group to find:

    -user USERNAME: Find a file that is the owner of a specified user (UID)
    -group grpname: Find a file that is a group of specified groups (GID)-uid
    : Find a file that is the owner of the specified UID number
    UserID GroupID: Find files belonging to the specified GID number
    -nouser: Find files that have no owner
    -nogroup: Find files without a group
        Find-user Guanyunchang    


(3) Find according to file type:

    -type type:
    f: Normal file
    d: catalog file
    L: Symbolic Link file
    s: Socket file
    B: Block device file
    C: Character device file
    p: Pipeline file
        find /dev/-type C



(4) According to the size of the file to find:

    -size [+|-] #UNIT
        common units: K, M, G,c (byte)
    #UNIT: (#-1, #]
        such as: 6k (5k,6k]
    -#UNIT: [0,#-1]
        such as: -6k said [0,5k]
    + #UNIT: (#,∞)
        such as: +6k Express (6k,∞)

(5) According to the time stamp:

    In "Days"
       ,-atime [+|-]#,  
        #: [#,#+1]
        +#: [#+1,∞]
        -#: [0,#)-mtime-ctime
    to "minutes":
        - Amin
        -mmin
        -cmin
    find/etc/-mtime-1



(6) According to the right to find:

-perm [/|-]mode
    MODE: exact permission matching
    /mode: Any Class (U,g,o) object's permissions can be a match, or a relationship, + from the Centos7 start elimination, CENTOS6 and its previous system/+ are available.
    -mode: Each type of object must have the specified permission at the same time, and relationship
        0 is not concerned about
Find-perm 755 will match the permissions mode is exactly 755 of the file
as long as any person has write permission, Find-perm + 222 will match   when the permission is described as a combination of permissions, that is, when the 5,6,7, the relationship between the permissions or
only when everyone has write permission, find-perm-222 will match  when the permission is described as a combination of permissions, that is, 5,6,7, The relationship between the rights and the
find-perm-002 will only match if other people (other) have write permission


Handling Actions

    -print: Default processing action, displaying to screen
    -ls: Similar to performing a "ls-l" command on a found file
    - Delete: Remove the found file
        find-size +6k-delete
    -fls File: Long format information for all files found is saved to the specified file
    -ok-exec   OK Interactive, exec run directly
    find- Name "*.tmp"-ok rm-f {} \;
    find-type f-name "*.sh"-exec chmod a+x {} \;
    find-name "*.conf"-exec cp-i {} {}.bak \;


Because many commands do not support piping | To pass parameters, and this is necessary in daily work, there is a xargs command
Xargs used to produce parameters for a command, Xargs can read stdin data, and spaces or carriage returns to stdin Data separation becomes arguments
Note: file names or other meaning nouns contain spaces conditions
Some commands cannot accept too many parameters, command execution may fail, Xargs can resolve
Example:
     LS |xargs   rm
    find/sbin-perm +700 |ls-l       This command is wrong
    find/sbin-perm +7000 | xargs ls–l
Find and Xargs format: Find | xargs COMMAND


Comprehensive Case: example: (Two methods)
Find files in the/tmp directory where the owner is not root and the filename does not start with F
find/tmp \ (-not-userroot-a-not-name ' f* ')-ls
Find/tmp-not \ (-userroot-o-name ' f* ') –ls
Example: (primarily for handling cases other than this directory)
Find files for all other. conf suffixes under/etc/except/ETC/SANE.D directory
find/etc/-path '/etc/sane.d '-a-prune-o-name ' *.conf '

Add execute permissions to all files ending with. Sh in the current directory

Find-type f-name "*.sh"-exec chmod +x {} \;

Practice:
1, look for all files
    find/var-user root-group Mail
2, Find//var directory under the owner of Root and group mail All files in the Var directory that do not belong to root, LP, GDM
    find/var-not \ (-user root-o-user lp-o-user GDM \)
3, find the/var directory most Its contents have been modified in the last week, and the owner is not root or postfix file
    find/var-mtime-7-not \ (-user root-o-user postfix \)
4. Find the file
    find/-type f-atime-7-nouser-nogroup

Job:
1, find/et on the current system that has no owner or group, and has been accessed in the last week All files in the C directory that are greater than 1M and of type normal files
    find/etc/-size +1m-type F
2, find files in/etc directory where all users do not have write permissions
    find/etc-not-perm/222
3, find files in/etc directory where at least one type of user does not have Execute permissions
    find/etc-not-perm-111
4, Find/ ETC/INIT.D directory, all users have execute permissions, and other users have write permissions to the file
    find/etc/init.d/-perm-113



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.