Skip to main content

Creating User-Defined Actions with `-exec` in `find`

The -exec action in the find command is one of the most powerful and versatile features of Linux, enabling users to define custom actions to be performed on files that match their search criteria. This allows for a great deal of flexibility and automation in managing files.

Syntax of -exec

The syntax for the -exec action is as follows:

find [path...] [expression] -exec [command] '{}' [;|+]
  • [path...] is where you want find to search.
  • [expression] is the testing criteria (find uses this to decide which files to process).
  • [command] is the command find will execute on the matched files.
  • {} is a placeholder for the current file find is processing.
  • ; indicates the end of the exec command. Each file matched is processed by a separate invocation of [command].
  • + at the end of the exec command will make find try to run the [command] on all files at once, similar to xargs.

To ensure that the shell does not interpret the semicolon (;) as a special character, it's escaped with a backslash (\;), or it's quoted.

Creating a Delete Action

Let's illustrate creating a user-defined delete action using the rm command within -exec. For safety, we will create temporary files to work with.

Step 1: Creating Sample Files

Open the terminal and create a directory with sample files:

mkdir -p ~/find-exec-example
cd ~/find-exec-example
touch file-to-delete{1..3}.txt

This will create a directory named find-exec-example in your home directory and within it, three files: file-to-delete1.txt, file-to-delete2.txt, and file-to-delete3.txt.

Step 2: Defining the Delete Action

Now, you'll define a user-defined action to delete files that match your criteria.

find ~/find-exec-example -type f -name 'file-to-delete*.txt' -exec rm {} \;

This command searches for files in ~/find-exec-example with names that match the pattern file-to-delete*.txt and uses the rm command to delete them. The {} is replaced with the name of each found file, and \; signifies the end of the exec command.

When you execute this command, you will not see output unless there is an error. To verify that the files have been deleted, you can run:

ls ~/find-exec-example

There should be no file-to-delete*.txt files listed.

Safety Considerations

Using -exec rm with find can be dangerous because it can potentially delete more files than intended if used incorrectly. Always test your find commands with -print before replacing it with -exec rm to ensure it only matches the files you intend to delete.

For example, run the following first to see which files would be deleted:

find ~/find-exec-example -type f -name 'file-to-delete*.txt' -print

Once you're sure it's safe, you can switch -print to -exec rm.

Conclusion

The -exec action of the find command in Linux is an exceptionally powerful tool for performing batch operations on a set of files that match certain criteria. By understanding and using -exec, users can automate a variety of tasks, such as deleting files, changing permissions, or even applying complex scripts to data sets. It's essential, however, to handle this tool with care, especially when performing destructive operations like deletion, to avoid unintended loss of data.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle