In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use find and exec under Linux". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn "how to use find and exec under Linux".
Working under Linux, the find command is definitely a very high frequency command. We can use the find command to find files that match certain keywords, find files with certain dates, or set some regular expressions to find a series of files that meet this condition.
However, if there is only one find command, we can only find the file and list it in the terminal. A single find command is not enough to go any further. For example, we want to find the intermediate files (* .o files) in the project folder and delete them all; for example, we want to transfer all the logs under the log folder for more than 3 days to the designated folder; and so on. There are a lot of requirements.
Like this, we want to use the find command to find the relevant files, and then do further operations, how to do this? That's when the exec order comes in handy.
Let's first look at the basic usage of exec.
The-exec parameter is followed by commands that we want to do further, such as rm,mv, and so on. Exec uses the semicolon ";" as the closing identifier. Considering the different interpretations of the semicolon on each system platform, we add a backslash before the semicolon for portability. Before the semicolon, there is usually a pair of curly braces {}, representing the files found by the previous find command.
We directly explain the joint use of find and exec through several examples. For demonstration purposes, let's assume that there is a project folder as follows:
Example 1: use the find command to find the relevant files, and then use the ls command to list their details
We now want to find all the .o files in the current directory and list them with the ls-l command. The command to implement this requirement is as follows:
Find. -name "* .o"-type f-exec ls-l {}\
The results are as follows:
Here, we use the find command to match all the .o files in the current directory, and use the ls-l command in the-exec option to list their details.
Example 2: use the find command to find related files, and then use the rm command to delete them
We now want to find all the .o files in the current directory and delete them with the rm command. The command to implement this requirement is as follows:
Find. -name "* .o"-exec rm {}\
After executing this command, all .o files in this directory are deleted. Since there will be no hint after this action is completed, there is no need for screenshots.
Example 3: safe mode with the-exec option that prompts the user before operating on each matched file
In example 2, we execute the rm command as soon as we match the file, which is dangerous because misoperation can lead to catastrophic consequences.
The security mode of exec is created to avoid this problem. After matching to a file, it will ask you before performing the operation, and only after your confirmation will it take the corresponding action.
For the same requirement in example 2, if the security mode is adopted, the command goes like this:
Find. -name "* .o"-ok rm {}\
The implementation results are as follows:
Example 4: search for key contents in matched files
If I now have a very large project (such as the Linux kernel), I want to search for a file that contains a keyword. We can use the grep command to retrieve all the files. This is certainly possible, but if the project is large, it is too time-consuming and inefficient.
We can first use the find command to find all the relevant files, and then use the grep command to retrieve those files. Because you have already used find filtering, this operation will save a lot of time and improve efficiency.
The command is as follows:
Find. -name "* .h"-exec grep-rns "hello" {}\
The results are as follows:
Example 5: find the file and move to the specified directory
This requirement is relatively simple. For example, I now want to find all the .o files and mv them to the buil directory. Commands such as
Below:
Find. -name "* .o"-exec cp {} build\
The results are as follows:
The above is all the contents of the article "how to combine find and exec under Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.