You may already know how to search for files using the find command. However, the basic ways to find a file by name, size, user, or type may not be enough. Moreover, it is sometimes necessary not only to find files but also to perform some actions on them. In this topic, we will discuss the -exec option of the find command.
What is exec
This argument allows you to execute other commands of your choice on the search results. We may choose to use it when we need to perform actions with several files at once and we want to do it quickly and automatically. For example, if you need to delete multiple .pdf files from ./documents, then it is easier to do this with one command, rather than manually deleting each file separately.
The syntax of it is as follows:-exec command {} \;, where
commanddenotes the command you wish to execute on the search results. For example,rm,mv,cpand so onthe
{}refers to the search resultsthe
\;denotes that the command ends.
The \; symbol may be substituted by \+. The result will be the same. However, the difference between them is that with \;, the command is executed once per file or directory found by find. With \+ , the command is executed once for multiple files or directories at a time, passing as many as possible as arguments to the command in a single execution. Using \+ is more efficient when dealing with many items, reducing the overhead of repeatedly launching the command.
Below we will illustrate some of the commands one may need to execute.
Change files
First of all, you may need to copy files from one directory to another:
$ find ./ -name "*.pdf" -exec cp {} ./documents \;Here we searched for all files with the .pdf extension and copied them to the /documents directory. In the same way, you can move the files using the corresponding mv command.
We use mv command to rename files, we can use this with find and -exec to rename files:
$ find ./ -type f -name 'test*' -exec mv {} {}.renamed \;With this command we use find -exec to rename files. The found files are stored in {} and are renamed by adding the .renamed extension.
The other things you might want to change are file mode and file owner. Let's look at how to do this.
You can recursively change file permissions without affecting directories:
$ find ./ -type f -exec chmod 755 {} \;The directories permissions may also be changed this way if needed. To do this, use the d type parameter instead of f with the -type argument. In addition to this, you can also recursively change the user, for example:
$ find ./ -user olduser -type d -exec chown newuser {} \;Below we will look what we can do with file content using -exec option.
File content
If you need to find files not by name, but by their content, then you can combine find and -exec with grep. Let's say you want a list of files with the word cat, then you can do the following:
$ find ./tmp -type f -exec grep -i cat {} \;
This is a Unix cat
The cat sits on the map However, the names of the files are currently not visible. To display file names you can replace ; with + :
$ find ./tmp -type f -exec grep -i cat {} \+
/tmp/dir/text2:This is a Unix cat
/tmp/dir/text1:The cat sits on the mapIn case of using \+, it shows the filename where each match occurs, which is helpful for identifying the source of the content.
The next action you might need to perform is deleting files.
Remove files
Using –exec, you can delete files. Here all text files are removed from the ./test directory:
$ find ./test -type f -name "*.txt" -exec rm {} \;You can complicate the command and delete only certain files, for example, those that are older than 7 days:
$ find ./tmp -type f -mtime +7 -exec rm {} \;The -mtime parameter means that the file's data was last modified n*24 hours ago, i.e. 7*24 in our case.
There is also the -delete option that performs the same action. You can read more about it using the man find command.
Conclusion
The
-execoption helps to execute the command of one's choice on the search results.The main reason to use the
-execoption is that it automates and speeds up working with several files at once.With this option, one can perform actions like moving files, deleting files, renaming, viewing files content, and so on.