Use of the Find command in Linux

Source: Internet
Author: User

Find is a search command in linux. It is not very efficient, but often used. Therefore, you must master the usage of find.
Find [directory] search for conditional operations
There is also a way to express: find path option [-exec COMMAND {}\;
# If the desired output result is an absolute directory, write the absolute path to the directory to be searched. If the relative path is written, the output is also a relative path.
The search condition in this command can be a composite condition consisting of not, and, or, a logical operator. Logical operators and, or, and not have the following meanings:

(1) and: logic and is represented by "-a" in the command. It is the default option of the system. It indicates that the search condition is satisfied only when all conditions are met. For example:
Find-name 'tmp '-xtype c-user 'inin'
This command is used to search for all files whose conditions are met.
(2) or: logical or, represented by "-o" in the command. This operator indicates that the search condition is satisfied as long as one of the given conditions is satisfied. For example:
Find-name 'tmp '-o-name 'mina *'
This command is used to query all objects whose names are 'tmp 'or match 'mina.
(3) not: Non-logical. Use "!" in the command. . This operator is used to search for objects that do not meet the given conditions. For example:
Find! -Name 'tmp'
This command is used to query all objects whose names are not 'tmp.
Note: When many logical options are used, you can enclose these options in parentheses. To avoid Shell misunderstanding, you must add the Escape Character "\" before the phone number to remove the meaning of the brackets. Example:
Find \ (-name 'tmp '-xtype c-user 'inin '\)

Next, let's talk about the option of the find command.
In option, specific parameters are as follows:
-Name 'string': searches for all files whose names match the given strings. Wildcards *,? , [].
-Lname 'string': searches for all symbolic link files of the given string matching the file name. Wildcards *,? , [].
-Gid n: searches for all files belonging to the user group with ID n.
-Uid n: searches for all files belonging to users with ID n.
-Group 'string': searches for all files belonging to the string given by the user group name.
-User 'string': searches for all files belonging to the string given by the user name.
-Empty: searches for directories or files with a size of 0.
-Path 'string': searches for all files whose path names match the given strings. Wildcards *,? , [].
-Perm permission: searches for files and directories with the specified permission. The permission expression can be 711,644.
-Size n [bckw]: searches for objects of the specified file size. The character after n indicates the unit. The default value is B, which indicates the block size of 512 bytes.
-Type x: Search for objects of the x type. x is one of the following characters:
-Maxdepyh n only searches for matching files or directories under the specified directory.
Block B Device Files
C character Device File
D directory file
P named pipe (FIFO)
F Common File
L symboliclinks)
S socket File
-Xtype x and-type are basically the same, but only the symbolic link file is searched.
Search by Time
-Amin n: searches for all files that were accessed n minutes ago.
-Atime n: searches for all files that have been accessed n days ago.
-Cmin n: searches for all files whose status has been modified n minutes ago.
-Ctime n: searches for all files whose status has been modified n days ago.
-Mmin n: searches for all files whose contents have been modified n minutes ago.
-Mtime n: searches for all files whose contents have been modified n days ago.
-Print: outputs the search result to the standard output.
Example: in root and subdirectory search does not include directory/root/bin, greek user, file type is normal file, 3 days ago named test-find.c file, and output the structure. The find command is as follows:
Find/-name "test-find.c"-typef-mtime + 3-user greek-prune/root/bin-print
Of course,-print is a default option, so we do not have to configure it.
Let's take a look at the exec options:
-Exec: Specifies the shell command for the structure command to be searched. Note that the format must be correct: "-exec command {}\;"
There must be spaces between} And;
{} Indicates that the command parameter is the file found. The end of the command must end.
Example: run the following command to delete the files found in the preceding example:
Find/-name "test-find.c"-typef-mtime + 3-user greek-prune/root/bin-exec rm {}\;
Find command instance:
Find.-name 'main * '-exec more {}\;
% Search for all files starting with main in the current directory and display the contents of these files.
Find. \ (-name a. out-o-name '*. o' \)>-atime + 7-exec rm {}\;
% Delete All a. out or *. o files not accessed in the current directory within one week.
"." In the % command indicates the current directory. At this time, find will start from the current directory and find the files that meet the specified conditions one by one in its subdirectories.
% "\ (" And "\)" indicate parentheses (), and "\" is called escape characters. The reason for writing this is that (and) have different meanings for Shell, rather than the purpose of combining conditions.
% "-Name a. out" refers to the file named a. out;
% "-Name '*. o'" refers to all files whose names end with. o.
The-o between the two-names indicates the logic or (or), that is, to find the file named a. out or whose name ends with. o.
The find Command finds the delimiter file in the current directory and Its subdirectories, and then checks whether the last access time is 7 days ago (condition-atime + 7). If yes, run rm (-exec rm {}\;) on the file {}\;).
{} Indicates the name of the file that meets the criteria found, and \; indicates the syntax requirements.
The last \ In the first line of the above command is the line feed. When a command is too long to be written in a row, you can enter one \. Then, the system displays one>, indicating that the user can continue to enter the command.
The above is all the content I learned about the find command today. If there are any deficiencies, please point out.

Basic usage and advanced usage of the find command in Linux

Linux O & M tool-find command

Common usage examples of find in Linux

Linux find command details

How to Use the find command in Linux

Find basic usage commands

15 advanced Linux find command examples

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.