Get the App
SLTechnology News&Howtos  ›  Servers  › 

Introduction and usage of find command

Shulou Source: shulou.com Published: 2022-06-02 14:41:22 11月12日 Update

Find: file lookup

Function: real-time search tool, complete question search by traversing the specified path, and recursively process files in the hierarchical directory

Characteristics of find:

1. Accurate search, real-time search 2. It is possible to search only directories 3. 5 for which the user has read and execute permissions. The speed of searching is a little slower.

Find (nothing to follow will show all the lists in the current directory. Find is searched recursively by default)

Syntax: find [OPTION]... [search path] [search condition] [processing action]

Find path: specify a specific target path; default is the current directory

Search criteria: specified search criteria, such as file name, size, type, permissions, etc.; default is to find all files under the specified path

Processing action: operate on files that meet the criteria, and output to the screen by default

Options:

Search directory level

-maxdepth level: maximum search directory depth-mindepth level: minimum search directory depth example: find directory-maxdepth 2-mindepth 2: search only level 2 directories-depth:find directories-depth: process files first and then process directories

Look up by file name and inode:

-name "file name": support the use of glob *,?, [], [^]-iname "file name": case-insensitive-inum n search by inode number

Example:

[root@localhost data] $find / data-inum 67

/ data/123.txt

-samefile name files with the same inode number

Example:

Find / data-samefile / data/fi.sh: directories can be inconsistent before and after viewing files with the same node number

The directory in which the standard files are searched

-links n files with n links-regex "PATTERN": matches the entire file path with PATTERN instead of the file name (matches the full path)

Search according to the document owner and subordinate group

-user username: find files with specified user ownership-group groupname: find files with specified group ownership-uid useruid: find files with specified UID owner-gid groupgid: find files with specified GID ownership group-nouser: find files without ownership-nogroup: find files without group ownership

Look up by file type:

-type:

B special block file (buffered) c special character file (unbuffered) d directory p named pipe (FIFO) f ordinary file l symbolic link s socket

Empty file or directory

-empty

-type d-empty

Depending on the file size:

-size: commonly used units (K M G c (Byte))

+ # M: indicates a file greater than # M, + 5m

-# M: represents a file less than # M,-5m

Look up according to time:

In days:

-atime: access time

+, -: + 10, files visited in more than ten days,-files accessed in ten days

-mtime: modification time, time when the file content was last modified, and file content modification

-ctime: change time, last change time of the file, attribute and file name change

In minutes:

-amin: access time

-mmin: modification time

-cmin: change the time

Find based on file permissions:

-perm:

Mode: precise search

/ mode: the permission of any kind of object meets the condition as long as it matches one bit. Or the relationship between. In the early days, it was indicated by a + sign.

-mode: each type of object must have the specified permission to meet the condition

0: does not care

For example: find-perm 755 file, which exactly matches the file with permission 755

Find-perm / 222file, owner, group, others, one of whom has write permission to match

Find-perm-22222 file, owner, group, others, each must have write access to match

Find-perm-002file, which is matched only if others have write permission

Action:

-print: default processing action to display the matching to the screen

-ls: similar to ls-l viewing files

-delete: delete the matching content (dangerous, do not use it)

-fls file: the found file is saved to the specified file with long format information

-ok command {}\;: execute the command specified by the command bit for the found file. The disadvantage is that you need to confirm before executing the command on each matching file.

Example: find / data-name "* .sh"-ok mv {} / tmp\

{}: reference the file matched by the previous find, and refer to the name of the file found, for example, the file name matched within ping.sh,name {} {ping.sh}

\;: syntax format, which must end with\;

-exec command {}\;: execute the command specified by the command bit for the found file. Unlike ok, a confirmation is not required.

Example: find / data-name "* .sh"-ok mv {} .bak / tmp\

{} .bak: move to the / tmp directory to rename the file with the suffix .bak

Find all files in the / var/ directory that do not belong to root, lp, gdm

[root@centos7 ~] # find / var!-user root!-user lp!-user gdm-type f

/ var/lib/colord/mapping.db

/ var/lib/colord/storage.db

/ var/lib/nfs/state

/ var/lib/unbound/root.key

/ var/lib/setroubleshoot/setroubleshoot_database.xml

/ var/lib/setroubleshoot/email_alert_recipients

/ var/lib/postfix/master.lock

/ var/spool/mail/rpc

/ var/spool/mail/cheng

/ var/spool/mail/ning

Tags: File directory time permission path search processing condition action name command precision content unit size file name standard special same real-time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi MySQL Shulou Technology Shulou Tech Info