Linu Study Notes 8 (find files)

Source: Internet
Author: User

In many cases, we need to use conditions to find the desired file. Flexible use of the file search tool can get twice the result with half the effort.

Content:

File Search:

1. Locate:

Non-real-time, fuzzy match, search is performed based on the entire system file database;

# Updatedb: manually generate a file database

Advantage: fast


2. Find:

  • Advantage: Real-Time

Precision

Supports multiple search criteria

  • Disadvantages:

Traversing all files in the specified directory to complete the search is slow;

  • Find Search Basics:

    Find find path search standard search for future processing operations

Search Path: the current directory by default

Search criteria: All files in the specified path by default

Processing operation: Display by default


  • Matching criteria:

-Name 'filename': exact match of file names

Wildcard File Name:

*: Any character of any length

?

[]

  • -INAME 'filename': the file name is case insensitive when matching.

  • -RegEx pattern: file name matching based on regular expressions. Note: Regular Expressions all match each other,

    For example: Find-/etc-name 'passwd'

    /Etc/PAM. d/passwd

    /Etc/passwd

    Find/etc-RegEx 'passwd' does not match any content.

    Find/etc-RegEx '/etc/passwd.

    /Etc/passwd

  • -User Username: search by owner

  • -Group groupname: search by group

  • -UID uid: search by UID (when the user corresponding to the UID is deleted, only the UID number is left, which is convenient for finding the residual files of the deleted users .)

  • -Gid: search by GID (same as above)

  • -Nouser: Search for files with no owner

  • -Nogroup: Search for files without groups

  • -Type

F: Common File

D: Directory

C: character file

B: Block devices

L: Link file

P: MPS queue

S: Socket

  • -Size [+ |-] (any sub-directories that meet the criteria can be displayed, including sub-directories such as parent directories that meet the criteria. If any sub-directories that meet the criteria can be found)

# K (for example: 10 K, So 9-10 K will be displayed, he will think that more than 9 K is also 10 K)

# M

# G

  • Combination conditions: (and by default)

-

-O

-Not

/Tmp directory, which is not a directory and cannot be a socket-type file

Note: this can be combined for example: Find/tmp/-not \ (-type D-O-type S \) is equivalent

Find/TEP/-not-type D-not-type S

In the/tmp/test directory, the owner is neither user1 nor user2;


  • Query by Time:

By days

-Mtime: modification time

-Ctime: Change Time

-Atime: Entry Time

[+ |-] #

By minutes:

-Mmin

-Cmin

-Amin

[+ |-] #


  • Search by permission

-Perm mode: exact match

/Mode: any matching condition is met.

-Mode: The File Permission can fully include this mode.

-644

644: RW-r --

755: rwxr-XR-x

750: rwxr-x ---

Find./-Perm-001

  • Action:

-Print: Display

-Ls: displays the details of each file in a format similar to LS-L.

-OK command {} (here the placeholder represents the query result) \; each operation requires user confirmation

-Exec command {}\;

Exercise:

1. Find all the files whose owner is root and whose group is mail in the/var directory;

Find/var-User Root-group mail


2. Search for files that do not belong to root, bin, or student under the/usr directory;

Find/usr-not-User Root-a-not-user bin-a-not-user student

Find/usr-not \ (-User Root-o-user bin-o-user student \)


3. Search for files in the/etc directory that have been modified in the last week and do not belong to the root and student users;

Find/etc-mtime-7-not \ (-User Root-o-user student \)

Find/etc-mtime-7-not-User Root-a-not-user student



4. Search for files with no owner or group on the current system that have been accessed in the last day, and change their owner group to root;

Find/\ (-nouser-o-nogroup \)-a-atime-1-exec chown root: Root {}\;


5. Search for a file larger than 1 MB in the/etc directory and write the file name to the/tmp/etc. largefiles file;

Find/etc-size + 1 m>/tmp/etc. largefiles

Find/etc/-size + 1 m-exec echo {}>>/tmp/etc. largefiles \;


6. Find the file in the/etc directory where all users do not have the write permission and the detailed information is displayed;

Find/etc-not-Perm/222-ls


The xargs command can always be followed by the find command (no placeholder is required, and the output result is not one in each line, but separated by spaces)

For example, find/etc-size + 1 M | xargs echo>/tmp/etc. largefiles


Conclusion: The find function is still very powerful.


This article is from the "Tiandao reward diligence" blog and is not reposted!

Linu Study Notes 8 (find files)

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.