find is a Linux/Unix command used to search for files and directories in a specified location based on certain criteria such as name, size, modified time, and ownership, among others.
The exec option in find is used to execute a command on the files that are found by the find command. The command can be any shell command, including other Linux commands or scripts.
In this article, we will dive into this command.
Table of Contents
Syntax of find command with exec option
The find command with the -exec option allows you to perform a command on the files found by find. The syntax is as follows:
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!find [path] [expression] -exec [command] {} \;
Parameter | Description |
---|---|
path | The starting directory for the find command |
expression | A set of conditions that must be satisfied for a file to be considered a match |
-exec | Specifies that a command should be executed on each file found by find |
command | The command to execute on each file found by find |
{} | A placeholder for the file name that find found |
\; | Signals the end of the exec command |
Note that the {} and \; parts of the command are required and must be included as shown.
For example, the following command searches for all files with the extension .log in the current directory and its subdirectories and deletes them:
find . -name "*.log" -exec rm {} \;
Here, . specifies the current directory, -name “*.log” specifies the search criteria for files with the extension .log,
rm is the command that is executed on the found files, {} is the placeholder that is replaced with the names of the found files, and \; is used to terminate the command.
Find and remove multiple files in Linux
$ find . -type f -name "*.mp3" -exec rm -f {} \;
This command will find and remove all files with a “.mp3” extension in the current directory and its subdirectories. Here’s what each part of the command does:
- -type f: only consider regular files (i.e., not directories or special files).
- -name “*.mp3”: the filename pattern to match. In this case, it’s any file with a “.mp3” extension.
- rm -f: the command to execute on each file found by find. In this case, it’s the rm command with the -f (force) option to delete the file without prompting for confirmation.
delete files with find and exec command in Linux
$ find / -type f -size +100m -exec rm -f {} \;
This command will delete all the files that are larger than 100 megabytes from the root directory and its subdirectories.
It is important to use this command with caution, as it can potentially delete important files.
It is recommended to use the -print option with the find command first to verify the files that will be deleted before using the -exec option to delete them.
rename files with find and exec command in Linux
$ find . -type f -name 'file*' -exec mv {} {}_renamed \;
- mv: the command to move or rename files
- {}: a placeholder for the file name that find found
- {}_renamed: the new name for the file, which is the original name plus “_renamed”
- \;: signals the end of the exec command
When the command is executed, find will search for all files starting with “file” in the current directory and its subdirectories.
For each file found, the command mv {} {}_renamed will be executed, where {} is replaced by the file name. This will rename each file by appending “_renamed” to its original name.
change file permissions with find and exec command
$ find / -type f -perm 0777 -print -exec chmod 644 {} \;
This command finds all files on the system with the permission 0777 (read, write, execute permissions for all users) and prints their names to the console.
Then it changes their permissions to 644 (read and write permissions for the owner, and read-only permissions for group and others) using the chmod command.
More examples with find exec command
find . -name '*.mp3' -exec mv {} /tmp/music \;
This command finds all files in the current directory and its subdirectories that end with “.mp3” and moves them to the “/tmp/music” directory.
Copy all XML files containing “2020” in their filename to a backup directory using the find command.
$ find . -name '*2020*.xml' -exec cp -r "{}" /tmp/backup \;
Related: