Find and exec work together to do great things under Linux

Working under Linux, the find command is definitely a very high frequency command. We can use the find command to find files matching 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. Just a find command is not enough to do further operations.

For example, we want to find the intermediate files (*.o files) in the project folder, and then delete them all; another example, we want to transfer all the logs in the log folder for more than 3 days to the specified folder ; And so on, there are many other needs.

Like this, we want to use the find command to find the relevant files, and then do further operations, how should we proceed? At this time, the exec command comes in handy.

Let's first look at the basic usage of exec.

The -exec parameter is followed by the commands we want to operate further, such as rm, mv and so on. exec uses the semicolon ";" as the end identifier. Considering the different interpretations of the semicolon by various system platforms, we add a backslash before the semicolon to facilitate transplantation. Before the semicolon, there is usually a pair of curly braces {}, which represent the files found by the previous find command.

We directly explain the combined usage of find and exec through a few examples. For the convenience of demonstration, we 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 detailed information

We now want to find out all the .o files in the current directory and use the ls -l command to list them. The command to achieve 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 detailed information.

Example 2: Use the find command to find relevant files, and then use the rm command to delete them

We now want to find out all the .o files in the current directory and delete them with the rm command. The command to achieve 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 prompt after this action is completed, there is no need to take a screenshot.

Example 3: Using the security mode of the -exec option, the user will be prompted before operating on each matched file

In Example 2, we execute the rm command immediately after matching the file. This operation is somewhat dangerous, because if you misuse it, it may cause catastrophic results.

The safe mode of exec is to avoid this problem. After it matches a certain file, it will ask you before proceeding, and it will perform the corresponding operation only after your confirmation.

The same requirement of Example 2, if the safe mode is adopted, the command is as follows:

find . -name "*.o" -ok rm {} \;

The execution results are as follows:

img

Example 4: Search the key content in the matched file

If I currently have a very large project (such as the Linux kernel), I want to search for a file containing a certain keyword. We can use the grep command to retrieve all the files. This is certainly possible, but if the project is large, it will be too time-consuming and inefficient.

We can use the find command to find all relevant files, and then use the grep command to retrieve those files. Because the find filter has been used again, 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:

img

Example 5: Find the file and move it to the specified directory

This requirement is relatively simple. For example, I want to find out all the .o files and then mv them to the buil directory. Command like

under:

find . -name "*.o" -exec cp {} build \;

The results are as follows:

img

More exciting content, please pay attention to the public number Liang Xu Linux , a public reply to 1024 is available free 5T technical information, including: Linux, C / C ++, Python, raspberry pie, embedded, Java, artificial intelligence , and so on. Reply to the group in the official account and invite you to join the expert cloud technology exchange group.

img

Finally, recently many friends asked me for the Linux learning roadmap , so based on my experience, I spent a month staying up late in my spare time and compiled an e-book. Whether you are in an interview or self-improvement, I believe it will help you! The directory is as follows:

Give it to everyone for free, just ask you to give me a thumbs up!

Ebook | Linux development learning roadmap

I also hope that some friends can join me to make this e-book more perfect!

Gain? I hope the old irons will have a three-strike combo so that more people can read this article

Recommended reading:

Guess you like

Origin blog.csdn.net/yychuyu/article/details/108588803