Find&locate of Linux File search

Source: Internet
Author: User

Find&locate of Linux File search

I. Overview

One of the core ideas of Linux system "Everything is file", for so many files, how to quickly find filtering it? Let's take a look at the system-provided file lookup commands find and locat, skilled use of the Find command is the only way to Ops


Ii. usage and examples of find

1.find Features

Find speed slightly slower

Exact search

Real-time Search

Only directories with Read and execute permissions can be searched


2.find usage

Usage: Find [options] [Find path] [find condition] [processing action]

Search criteria:

Find-type f|d.......f normal files according to file type D directory L link s socket B block device C character device P pipe file according to the genus Master, genus Group find-user name find directory subordinate name of file-group G1 find the group G1 file-uid # Find files with uid #-gid #查找gid为 # file-nouser find files with no master-nogroup find file-specific options that are not a group-mindepth level set minimum search levels-maxdepth Level set maximum search hierarchy-name match file name search, wildcard-iname to name search, ignore file name case-inum #按inode号搜索-samefile Search for files of the same inode number-links #搜索链接数为 # The file-regex "pattern" searches for files that match the string-prune to exclude a directory from the search path find-size [+|-]# (Units) Common units: K M G 6k means 5k<s≤6k-6k K means s>6k find-atime [+|-]#+5:5 days ago-10:10 days or less-atime access time,-mtime change time in days,-ctime change time in days, -amin in minutes-mmin in minutes-cmin in minutes per day

Find based on permissions

-perm [/|-]mode

Mode exact permissions match such as: Find-perm 222 matches a file with a permission of 222

/mode permission bit (UGO), as long as there is a match, or the relationship

such as: find-perm/222 only need a permission bit write permission can

-mode The specified permission permission bit must match

such as: find-perm-222 each permission bit has write permission

Notice the difference between the three types of permissions

Combination conditions

-A and

- O or

-not,! Non-

Non-option of burning brain

! A-a! b=! (A-o B)

! A-o! b=! (A-a B)

Handling actions

-print Printing matching files

-ls Long format display matching file

-delete Deleting a matching file

-fls outfile output to the specified file

-ok cmd {} \; performs a cmd command on each file found, interactive acknowledgement

-exec cmd {} \; executes the cmd command without an interactive acknowledgment

{} represents the found file itself

Note: Because it is found files, is a one-time pass to the following CMD command, there is a write command error phenomenon, you can use Xargs to solve

Find ... | Xargs cmd

3.find usage Examples

①, search for file with log in file name under/var directory

Find/var-name "*log*"

②, search/app directory subordinate to Tom, array of G1 files

Find/app-user tom-a-group G1

③, search/app directory subordinate owner is not Tom, nor Joe's file.

Find/app-not-user tom-a-not-user-joe

Find/app-not \ (-user tom-o-user joe\)

④, deletion of more than 3 days, the main is Tom's temporary file

Find/tmp-ctime +3-user tom-ok rm {} \;

⑤, find files that your home directory can be written to by other users, and remove other users ' Write permissions

Find ~-perm-022-exec chmod o-w {} \;

⑥, find files modified before 10 in/var directory

Find/var-mtime +10-print

⑦, find files modified within 10/var directory

Find/var-mtime-10

⑧, Find/var directory change time is newer than File1.log, older than File2.log file

Find/var-newer file1.log-a-not-newer File2.log

⑨, find all directory files in/etc level directory

Find/etc-maxdepth 1-type D

Files larger than 100M in ⑩, query/directory

Find/-size +100m

11. Delete the normal file of non-hidden files in/root level directory

Find ~-maxdepth 1-type f |grep-v "/root/\."

12, query the current system does not belong to the host group of files

Find/-nouser-a-nogroup

13. Find all files with suid permission in the system

Find/-perm/u=s

14. Find all files with Sgid permission in the system

Find/-perm/g=s

15. Find all executable files in/etc

Find/etc-perm/a=x

16. Find all empty files in the system

Find/-type F-empty

17. Check all hidden files in/etc directory

Find/etc-type f-name ". *"

18. Find files that have been modified in 50-100

Find/-mtime +50-mtime-100

19. Find files that have been modified in the last 1 hours

Find/-mmin-60


Iii. Locate usage and examples


1. Locate Overview

Faster than Find,locate lookup, but low usage because it supports only non-real-time lookups


2, locate work characteristics

Fast Search Speed

Non-real-time lookup

Search file Full path

Only directories where the user has read and execute permissions can be searched


3. Locate usage

Locate [options] Match mode

- I case-insensitive search

-N # list only the first # matches


4. Example

Locate conf find file with conf in file name


Find&locate of Linux File search

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.