Find the Linux file

Source: Internet
Author: User

Find-search for files in a directory hierarchy
Find a file tool in the hierarchy directory

Grammar

Find [OPTIONS] [Find starting path] [find condition] [processing action]

Common options
  • -name name,-iname name: file with Name. Iname ignores case. Supports GLOB-style wildcard characters;
  • -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;

  • Look up depending on the type of file:-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:

    • With:-A, default combination logic;
    • Or:-O
    • Non:-not,!
  • Find by File Size:

    • -size [+|-] #UNIT; common units: K, M, G
    • #UNIT: (#-1, #)
    • -#UNIT: [0,#-1]
    • + #UNIT: (#, OO)
  • Find by Time stamp:

    • In "Day" units: the day here is in 24-hour units
      • -atime [+|-]# Access time
      • -mtime modification Time
      • -ctime creation Time
    • In "Minutes" units:
      • -amin
      • -mmin
      • -cmin
  • Search by permissions:-perm [/|-]mode;0 indicates not participating

    • Mode: precise permission matching;
    • /mode: Any one (r,w,x) of the permissions of any class of users (U,g,o) satisfies the condition, and there is a "or" relationship between 9-bit permissions;
    • -mode: Each class of users (U,g,o) in the permissions of each bit (r,w,x) at the same time meet the conditions are satisfied; 9-bit permissions exist between the "and" relationship;
  • 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;
DEMO:

Find all files in the/tmp directory that are subordinate to the main non-root

[[email protected] ~]# find /tmp/   -not -uid 0 -ls[[email protected] ~]# find /tmp/   -not -user root -ls

Find all files or directories in the/var directory that are subordinate to root and belong to the group mail;

[[email protected] test]# find /var/ -user root -group mailfind /var/ -uid 0 -group mail

File Size Lookup

[[email protected] test]# ll -h总用量 52K-rw-------. 1 root root 24K 4月  22 13:31 messages-rw-------. 1 root root 25K 4月  22 13:31 messages.bak[[email protected] tmp]# find ./  -size -25k -ls     [[email protected] tmp]# find ./  -size 25k -ls      (24k,25k]    24k< x <=25k./messages.bak[[email protected] test]# find ./ -size -25k -type f    [0,24k]    0<= x <=24k./messages[[email protected] test]# find ./ -size +24k -type f   [25k,  ]    25<= x./messages.bak

Follow file time to find

[[email protected] test]# date2018年 04月 22日 星期日 13:49:30 EDT[[email protected] test]# find /etc/ -type f -atime 3 -ls[[email protected] ~]# stat /etc/kdump.conf  File: ‘/etc/kdump.conf’  Size: 7265        Blocks: 16         IO Block: 4096   regular fileDevice: fd00h/64768d    Inode: 17265603    Links: 1Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)Context: system_u:object_r:kdump_etc_t:s0Access: 2018-04-18 23:03:40.914728405 -0400Modify: 2018-04-16 13:12:52.585009262 -0400Change: 2018-04-16 13:12:52.585009262 -0400 Birth: -

Find all files or directories that are not root, bin, or Zander in the/usr directory;

[[email protected] test]# find /usr -not \( -user root -o -user bin -o -user zander  \)[[email protected] test]# find /usr -not -user root  -a -not -user bin -a -not  -user zander

Find in/etc directory in the last week of its content has been modified, and the owner is not the root user is not the Zander user's files or directories;

[[email protected] test]# find /etc/ -mtime -7 -a  -not -user root -a -not -user zander[[email protected] test]# find /etc/ -mtime -7 -a  -not \( -user root -o  -user zander \)

Find files or directories on the current system that are not part of a group and have been visited in the last week;

[[email protected] test]# find / \( -nouser -o -nogroup \) -a -atime -7

Find all files that are larger than 1M in the/etc directory and are of the normal file type;

[[email protected] test]# find /etc/ -size +1M -type f -exec ls -lh {} \;

Find files in/etc directory where all users do not have permission to write;

先找任何一个有写权限 && 取反[[email protected] test]# find /etc/ -not -perm /222 -type f -exec ls -lh {} \;

Find/etc directory at least one type of user does not have permission to execute files;

所有用户都有权限  && 取反[[email protected] test]# find /etc/ -not  -perm  -111 -type  f  -exec ls -lh {} \;

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

[[email protected] test]# find /etc/init.d/ -perm -111 -a -perm -112 -type f  -ls[[email protected] test]# find /etc/init.d/ -perm -113 -type f  -ls

Find files in the/tmp directory that do not contain the Fstab string in the file name

[[email protected] ~]# find /tmp/  -type f -not -name   ‘*fstab*‘

Find files in the/TMP directory where the main is non-root and the file name does not contain the Fstab string

[[email protected] ~]# find /tmp/  -not -name   ‘*fstab*‘ -not -user root

Find the Linux file

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.