Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of find command

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

A brief introduction to find

Find is a real-time lookup tool that finishes file lookup by traversing the file system hierarchy under the specified starting path

Its working characteristics: slightly slower search speed; accurate search; real-time search

II. Find usage

Find [OPTIONS] [find start path] [find condition] [process Action]

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

Search criteria: the specified search criteria can be carried out according to file name, size, type, dependency, permissions, etc.; the default is to find all files under the specified path

Processing actions: actions taken on files that meet the search criteria, such as deletion, etc.; output to standard output by default

Search criteria:

Expressions: options and test

Test: the result is usually Boolean ("true", "false")

2.1. Find by file name

-name "pattern"

-iname "pattern"

Support for glob-style wildcards

*, [], [^]

-regex pattern: finds the file based on the regular expression pattern, matching the entire path, not its name

Example: find / usr-name nginx

2.2. Find according to file dependency

-user USERNAME: find all the files of the user specified by the owner

-group GRPNAME: find all files that belong to the specified group

-uid UID: find all the files of the UID specified by the owner

-gid GID: find all files of the GID specified by the group

-nouser: find files without owners

-nogroup: find files that do not belong to a group

2.3. Look up according to the type of file:

-type TYPE:

F: ordinary files

D: catalog file

L: symbolic link file

B: block device file

C: character device file

P: pipe fil

S: socket file

Combined testing:

And:-a, default combinational logic

Or:-o

Non:-not!

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

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

Exercise:

1. Find out all the files under the / tmp directory that are mainly non-root.

2. Find the files in the / tmp directory that do not contain the fstab string in the file name.

3. Find out the files whose main part of the / tmp directory is non-root and whose file name does not contain the fstab string.

2.4. Find based on the size of the file

-size [+ | -] # UNIT

Common units: K, M, G

# UNIT: #-1, #]

-# UNIT: [0memorie muri 1]

+ # UNIT: (# oo)

2.5. Find according to the timestamp

In terms of "days":

-atime [+ | -] #

#: [#, #-1)

-#: (#, 0]

+ #: (oo, #-1]

-mtime

-ctime

In minutes:

-amin

-mmin

-cmin

Find based on permissions

-perm [/ | -] mode

Mode: exact permission matching

/ mode: any one of the permissions of any type of user (umeme gpeno) satisfies the conditions; there is an "OR" relationship between 9 permissions

-mode: every member of the permissions of each type of users (uMagingPol x) is satisfied if they meet the conditions at the same time; there is a "and" relationship between 9-bit permissions.

Processing action

-print: output to standard output; default action

-ls: similar to executing the "ls-l" command on the found file to output the details of the file

-delete: delete the found file

-fls / PATH/TO/SOMEFILE: saves the long format information of all found files to the specified file

-ok COMMAND {}\;: execute the command indicated by COMMAND for each file found; each operation is confirmed by the user

-exec COMMAND {}\;: execute the command indicated by COMMAND for each file found

Note: when find passes the found file path to the following command, it first finds out all the qualified file paths and passes them to the following command at one time

However, some commands cannot accept excessively long parameters, and command execution will fail; another way to avoid this problem:

III. The usage of the args command

Xargs-build and execute command lines from standard input

Description:

The key reason why this command can be used is that many commands do not support | pipes to pass parameters, which is necessary in daily work, so there is a xargs command. Xargs can read the data of stdin and separate the data of stdin into arguments by distinguishing between blank characters or line breakers. Because it is separated by white space characters, xargs may misjudge if some file names or nouns with other meanings contain white space characters.

Common options:

-o: when sdtin contains special characters, treat it as general characters, like / 'spaces, etc.

-a file: read as sdtin from a file

-e flag: note that sometimes it may be-E flag flag must be a space-separated flag, and stop when xargs parses the flag containing the flag flag.

-p: ask the user when executing one argument at a time

-n num: add the number of times to indicate the number of argument used at a time when the command is executed. Default is to use all.

-t: indicates that the command is printed before execution

-I or-I: it depends on the linux support. Assign each name of xargs, usually line by line, to {}, which can be replaced by {}.

-r no-run-if-empty: stop xargs when the input to xargs is empty, and you don't have to execute it again.

-s num: the best number of characters on the command line, which refers to the maximum number of command line characters for the command after xargs

-L num Use at most max-lines nonblank input lines per command line.-s contains spaces

L is the same as-L

-d delim: delimiter. The default xargs separator is carriage return, and the delimiter of argument is a space. Here, the delimiter of xargs is modified.

-x exit, mainly in conjunction with-s use

-P modifies the maximum number of processes. Default is 1, and as many as it can is 0.

Exercise:

1. Find all files or directories under the / var directory whose main is root and whose group is mail.

~] # find / var-user root-a-group mail-ls

2. Find all files or directories in the / usr directory that do not belong to root, bin or hadoop; use two methods

~] # find / usr-not-user root-a-not-user bin-a-not-user hadoop

~] # find / usr-not\ (- user root-o-user bin-o-user hadoop\)-ls

3. Find files or directories under the / etc directory whose contents have been modified in the last week and whose owner is neither a root user nor a hadoop user.

~] # find / etc-mtime-7-a-not\ (- user root-o-user hadoop\)-ls

~] # find / etc-mtime-7-a-not-user root-a-not-user hadoop-ls

4. Find files or directories that do not belong to or belong to groups on the current system and have been accessed in the last week

~] # find /\ (- nouser-o-nogroup\)-atime-7-ls

5. Find all files in the / etc directory that are larger than 1m and whose types are ordinary files

~] # find / etc-size + 1m-type f-exec ls-lh {}\

6. Find files in the / etc directory that all users do not have write permission to

~] # find / etc-not-perm / 222-type f-ls

7. Find at least one class of files in the / etc directory for which users do not have the right to execute.

~] # find / etc-not-perm-111111-type f-ls

8. Find all files under the / etc/init.d/ directory where all users have execute permission and other users have write permission

~] # find / etc-perm-113-type f-ls

Blog assignment: find detailed explanation

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report