Linux file search

locate

Find the file system pre-built index database  /var/lib/mlocate/mlocate.db 

Find locate in advance depends on the construction of the first index, the index is constructed (periodic task), or manually update the database administrator (updatedb) automatically when the system is idle more

Index building process needs to traverse the entire root file system, extreme consumption of resources.

Work characteristics:

  1. Find a non-real time (database lookups: locate)
  2. Find Fast
  3. Fuzzy Lookup
  4. Search the full path of the file, not just the file name
  5. The search user may only have read and execute permissions to the directory

locate Usage

Installation locate command 
yum install mlocate 

initialization 
sudo updatedb

Options

Options Explanation
-i Ignore case
-n Lists only the first few qualified
-r It supports regular expressions

find

Implementation Finder, find the file specified by traversing the path to complete

characteristic

  1. Find a bit slower
  2. Exact Match
  3. Find real-time
  4. You may only search for a user with read and execute permissions to the directory

find usage

The basic syntax:  the Find [OPTION] .... [search path] [search criteria] [processing operation] 

  • Search Path: Specify a path as this search location
  • Search Terms: Search criteria can be specified as: file name, size, type permissions.
  • Actions: do what to look for to meet the requirements of the file, the default output to the screen.

Search Terms

1. Search Level

-maxdepth level maximum depth search directory, specify the directory to Level 1 
-mindepth Level minimum depth search directory

2. The file name and inode lookup

-name "filename" Find the file name support the use of wildcard glob 
-iname "file name" is not case sensitive 
-inum n Find by inode number 
-samefile name the same inode number of file 
-links n n number of links to a file 
- regex "PATTERN" PATTERN to match the entire file path string, not just the file name

The owner, is a group lookup

Find owner -user USERNAME specified user (UID) file 
-group GRPNAME lookup for the specified group is a group (GID) file 
-uid UserID lookup file is owned by the specified UID number 
-gid GroupID is a group as specified lookup GID number of files 
-nouser not find the file owner of 
-nogroup find the file does not belong to the group

4. Find the file type

the TYPE -type: 
    F: normal file 
    d: directory file 
    l: symbolic link 
    s: socket file 
    b: block device file 
    c: character device file 
    p: pipe file

The file size

-size [+ | -] # UNIT common unit: k, M, G, c (byte) 
    e.g.: -size 2k match the value of (~ 1K 2K) 
         -size-5K in the range of (0-4k including 5k ) 
         -size +. 5K in the range of (5k + does not include 5k)

The timestamp Find

"Day" as a unit 
-atime [+ | -] # read time 
    #: [#, # + 1] times 
    + #: [# days or more] 
    - #: [0, # # does not include the day] 
-mtime modification time 
-ctime time status changes 

to "minutes" as a unit 
    -amin 
    -mmin 
    -cmin

7. Find eradicate file permissions

-perm # 
    For example: -perm 600 
          /600 or fuzzy matching 
          -602 other has read access permission bits will match

Processing operation

-print print out default 
-ls displays detailed information 
-delete delete files that match 
-fls file to match the file redirected to the specified file 
-ok COMMAND {} \; COMMAND specified command for each file, and each file requires interaction user confirms 
-exec COMMAND {} \; Similarly -ok, it does not require user interaction to confirm.

Combination of conditions

With: -a 
or: -o 
non-: -not,!

This chapter exercises

1. Use locate to ignore case inquiry password file 

2. Use the find query / all files no owner is a group of 

3. Use the find query owner permission bits of a file that contains a link to root 777 is a group not test any 
 
4. use find and delete files to find hh1-10 

5. use find to find the / var / log / type the following as an ordinary file, all files modified in accordance with the seven days before the date of inquiry

answer

1. Use locate to ignore case inquiry password file 
locate -i password 

2. Use find query / all no owner is a group of files 
find / -nouser -nogroup 

3. Use the find query is owned by the root is a group not to test any a permission bits contains 777 linked files 
 find / -user root -not -group the Test -perm / 777 the -type L 
 
4. use find and delete files to find hh1-10 
find / -iname "hh *" -exec rm -rf { } \; 

5. use find find / var / log /, the following types of common file, all files before seven days according to the modification date query 
find / var / log / -type f -mtime +7

  

 

Guess you like

Origin www.cnblogs.com/yanshicheng/p/12323828.html