Command Line has always been the first choice for Linux users to solve the problem. When you need to do something, you only need to think about how to do it. From then on, you will know how to do it.
However, few people spend some time systematically understanding the input and output of the processing tool in the processing process. Therefore, you may not be very familiar with the functions of the most basic utility. In this article, we will discuss an important tool in all shell toolboxes: Find utility.
As its name indicates, find is an application that searches for files and directories on a disk that meet the given criteria. By default, it scans all subdirectories from the current directory. Find performs queries based on different file attributes and performs certain operations on the query results. Generally, it runs some programs for each query result.
Let's take a look at some examples: first, we can use:
Find-name "*. html"-type F
This command has two test conditions,
First, "-
Name
"Is used to check each file name in the search process. If You Need To Be case sensitive, you can use"-INAME
.
The second parameter is "-type ".
It is used to specify the file type you are interested in.
"F
"Indicates that we are looking for common files.
"D
"Indicates the directory or usage
"L
"Indicates a symbolic connection. All options of the tool are displayed in the find manual.
In the preceding example, no path is specified because we need to search in the current directory. You can start searching from any directory in your system. For example, you know that HTML files are stored in one of two locations, then we can specify a start search point to make the query faster and more accurate.
Find/var/WWW
/Home/Lucy/public_html
-Name "*. html"-type F
This query starts from the root directory of the web server and searches for my HTML root directory and its sub-directories, hoping to get the desired results, rather than the web browser cache or HTML Help file.
By default, find traverses all subdirectories, but you can control this behavior by specifying the maximum depth. In the previous example, you can add
Add "-maxdepth 1" to the command. Setting the maximum depth to 0 indicates that only the given files in the command line are verified. Similarly, you can set the minimum depth to avoid searching for files in the root directory.
Another application of find is to search for files belonging to a given user. Therefore, to search all my files in my system, I use the following command:
Find/-user Lucy
You can also search for files based on group users by using "-group
"Parameter.
In addition, the test classification is related to time. We can use time-based methods to search for files.
"-Ctime" last creation time C
Reate time (correct: change time. Thanks for qianlongwydh.
)
"-Atime" last access time
Ccess time
"-Mtime" last modification time m
Odify time
For example, to search for the files created the day before yesterday, you can use: Find-ctime-1.
Note that "-" in front of 1 indicates that we are searching from the previous date range from today.
If you need to search more accurately, you can use the minute variables "-Cmin", "-Amin", and "-mmin ". If you have just made a mistake but are not sure it will affect those files, it is very easy to use the following query: Find-mmin-5
The standard action of find to execute a file is to display the file name, which is why if you track the results, you will see a series of file names. This is useful if you want to use these results as input for another filter. However, if you need more information about the results, you can make find output with the same result as LS-l:
Find-user Lucy-INAME "*. html"-ls
This command returns the File Permission and time information.
Finally, you can run the "-exec" parameter to allow the "find" command to run any program on each search result. The following program will delete all files with the extension ". tmp" in your home directory.
Find-name ". tmp"-exec RM {};
The above two braces will be replaced by the corresponding file name, And the backslash is used to tell find when the command ends. Find is often used in combination with chmod to quickly change the file permissions of a large number of files, or
Grep and sed are used in combination to selectively search for text or modify text using regular expressions. This is only the tip of the tip related to the use of find, using it as a script input, you can make time-consuming
The task is automatically completed, such as clearing files not accessed within one year, or automatically backing up modified files. The power of find means that it will still be the best tool for Linux users to process tasks.
I.