1.9 linux base (nine) - File Finder find

1.9 linux base (nine) - File Finder find

1.9.1 File Finder

  • Finding qualified files on the file system
  • File Find: locate, find
  • Find a non-real time (a database lookup): locate
  • Real-time look: find

1.9.2 locate

  • The query system pre-built index file database
  • /var/lib/mlocate/mlocate.db depends on the index built in advance
  • The index is constructed when the system is relatively idle automatic (periodic task), the administrator manually update the database (updatedb)
  • Index building process needs to traverse the entire root file system, extreme consumption of resources
  • Work characteristics:
    • Find Fast
    • Fuzzy Lookup
    • Find a non-real time
    • Search is a full path to the file, not just the file name
    • You may only search for a user with read and execute permissions to the directory
  • locate command
  • locate KEYWORD
  • Common options
    • -i case-insensitive search
    • N -n include only matches the first N
    • -r using regular expressions
  • File name or path in search examples with "conf" of
    • locate conf
  • Use Regex to search for files with ".conf" at the end of
    • locate -r ‘.conf$’

1.9.3 find

  • Real-Time Finder, to complete the path to the file specified by traversing Find
  • Work characteristics:
    • Find a bit slower
    • Exact Match
    • Find real-time
    • You may only search for a user with read and execute permissions to the directory
  • find syntax: find [OPTION] ... [to find the path] [search criteria] [processing operation]
    • Search Path: Specifies the specific target path; defaults to the current directory
    • Search Terms: specified search criteria can be standard file name, size, type, permissions, and so on; the default is to find all the files in the specified path
    • Actions: do operations that meet the conditions file, the default output to the screen

1.9.3.1 search criteria

  • It refers to the hierarchical search
    • -maxdepth level maximum depth search directory, specify the directory to Level 1
    • -mindepth level minimum depth search directory
  • According to the file name and inode lookup:
    • -name "file name": supports glob *,, [], [^]?
    • -iname "File Name": not case sensitive
    • -inum n Find -samefile name by the same inode number inode number of the file
    • -links n n number of links to files
    • -regex "PATTERN": PATTERN to match the entire file path string, not just the file name
  • According to owner, is a set of lookup:
    • -user USERNAME: Find owner for the specified user (UID) file
    • -group GRPNAME: Find is a group for the specified group (GID) file
    • -uid UserID: Find owner's UID number for the specified file
    • -gid GroupID: Find GID is a group for the specified number of files
    • -nouser: Find the file is not owner of
    • -nogroup: Find the file does not belong to the group

      1.9.3.2 depending on the file type to find:

  • -type TYPE:
    • f: regular file
    • d: directory file
    • l: symbolic link file
    • s: socket file
    • b: block device file
    • c: character device file
    • p: pipe file
    • Empty file or directory
    • -empty find /app -type d -empty

      1.9.3.3 combination of conditions

  • A combination of conditions:
    • And: -a
    • Or: -o
    • Non-: -not,!
  • De Morgan's Law:
    • (Non-A) or (non-B) = non (A and B)
    • (Non-A) and (non-B) = non (A or B)
    • Example:! A -a B = (A -o B)!!
    • !A -o !B = !(A -a B)
  • find examples
  • find -name snow.png
  • find -iname snow.png
  • find / -name “*.txt”
  • find /var –name “*log*”
  • find -user joe -group joe find -user joe -not -group joe
  • find -user joe -o -user jane
  • find -not ( -user joe -o -user jane )
  • find / -user joe -o -uid 500

find examples

  • Find out the / tmp directory, the owner is not root, and the file name does not begin with 'f' file
    • find /tmp ( -not -user root -a -not -name 'f*' ) -ls
    • find /tmp -not ( -user root -o -name 'f*' ) –ls
  • Excluding directories Example:
    • Find / etc / down, in addition to all other .conf file suffix /etc/sane.d directory of
    • find /etc -path ‘/etc/sane.d’ -a –prune -o -name “*.conf”
    • Find / etc / down, all .conf suffix /etc/sane.d and / etc / fonts directory of files other two
    • find /etc (–path ‘/etc/sane.d’ –o –path ’/etc/fonts’ ) -a -prune –o -name “*.conf”

1.9.3.4 based on file size to find:

  • -size [+|-]#UNIT
    • Common units: k, M, G, c (byte)
    • UNIT: (# -1, #] as: 6k represents (5k, 6k]

    • - # UNIT: [0, # - 1] as: -6k represents [0,5k]
    • + # UNIT: (#, ∞) such as: + 6k represents (6k, ∞)

1.9.3.5 According to the time stamp

  • "Day" as a unit;
    • -atime [+|-]#,
    • : [#,#+1)

    • +#: [#+1,∞]
    • -#: [0,#)
    • -mtime
    • -ctime
  • To "minutes" for the unit:
    • -amin
    • -mmin
    • -cmin

1.9.3.6 look under the authority

  • -perm [/|-]MODE
    • MODE: exact permissions match
    • / MODE: any class rights (u, g, o) as long as an object can be matched, or relationship, centos7 start out from the +
    • -MODE: each type of object must have a designated authority, and relations
    • 0 indicates no concern
    • find -perm 755 matches permission mode exactly 755 files
    • Whenever any person has write permissions, find -perm +222'll match
    • Only when everyone has write permissions, find -perm -222 will match
    • Only when other people (other) have write access, find -perm -002 will match

1.9.3.7 processing operation

  • -print: default processing operation, display to the screen -
  • -ls: Performs like "ls -l" command to find files
  • -delete: Delete files found
  • -fls file: save the long form information to find all the files to the specified file
  • -ok COMMAND {}; find each file specified by a COMMAND execution command, for each file before executing the command will require the user to confirm the interactive
  • -exec COMMAND {} \; each of the found file designated by the execution command COMMAND
  • {}: Look for references to the file name itself
  • When find to find the files delivered to the specified after the command, find command all eligible to file a one-time transfer back
如:find / -name zhu.sh -exec rm {} \\; 容易误删除  
如:find / -name zhush -ok rm {} \\;
[root@centos77 ~]# find / -name a -ok rm {} \;
< rm ... /root/a > ? n
< rm ... /var/lib/yum/yumdb/a > ? n
< rm ... /usr/share/terminfo/a > ? n

1.9.3.8 parameter substitution xargs

  • Since many command does not support pipeline | to pass parameters, and daily work there is need, so there xargs command
  • Xargs parameters for generating a command, data can be read stdin xargs, and a space character or a carriage return arguments become separated data stdin
  • Note: The file name contains a space character within the meaning of the noun or other circumstances
  • Some commands can not accept too many parameters, the command may fail, xargs can be resolved
  • Example: ls f * | xargs rm find / sbin -perm +700 | ls -l command is wrong find / sbin -perm +7000 | xargs ls -l
  • find and xargs format: find [option] ..... [search path] [search criteria] [processing operation] | xargs COMMAND will find as xargs cmd argument
[root@centos77 ~]# find / -name a |xargs rm   不会进行交互,且忽略别名会直接删除;
find / -type f -name "*.conf"|xargs grep -n '80'

1.9.3.9 find examples

  • Backup configuration file, add this extension .orig
  • find -name “*.conf” -exec cp {} {}.orig ;
  • Tip delete temporary files for more than 3 days in the presence of joe
  • find /tmp -ctime +3 -user joe -ok rm {} ;
  • Looking file can be written by other users in the home directory
  • find ~ -perm -002 -exec chmod o-w {} ;
  • Find privileges under the data / 644, as an ordinary file suffix sh, adding execute permissions
  • find /data –type f -perm 644 -name “*.sh” –exec chmod 755 {} ;
  • View / home directory find / home -type d -ls

1.9.3.10 practice questions:

1. Find all the file / var / directory under the main root and belongs to a group mail

find /var -user root -a -group mail 

2, Find / does not belong to all the files root, bin or under the usr directory hadoop

find /usr -nouser root -a -nouser bin -a -nouser hadoop

3, look in / etc / directory last week revised its content and does not belong and do not belong to the root file hadoop

find /etc/ -mtime -7-a -not -user root -a -not -user hadoop

4, find the file currently no owner, or belong to groups on the system, and within a month had been recently visited;

find  / \(-nouser -o -nogroup\) a  -atime  -30 

5, find / etc 1M and all files larger than an ordinary type of file / directory;

find /etc/ -size +1M -a -type f (这里不能加-f)

6. Find / etc / directory of all users do not have write permission

find /etc/ -not -perm +222

7. Find / etc / directory of all users have write access to the file

find /etc/  -perm -222

8. Find / etc / directory with at least one type of user does not have write permission

find /etc/ -not -perm -222

At least look at / etc / directory of a class of users have write access

find /etc/  -perm  +222

9. Find /etc/init.d directory and all users have execute permissions to other users with write access to the file

find /etc/init.d -perm -113

Guess you like

Origin www.cnblogs.com/huangsefeizhu/p/11506302.html