Using the find -exec Command Option on Linux


Introduction

The find command in Linux is a versatile and powerful tool for finding files and directories on a file system. The “-exec” option is a useful addition to the find command that allows you to execute a command on any file or directory it finds. This can be useful for tasks like finding specific files and then performing an action on them, like deleting, moving, or editing. In this article, we will discuss the syntax and usage of the find -exec command option and provide examples of how it can be used.

Syntax and Usage

The basic syntax of the search command with the -exec option is as follows −

$ find [path] [options] -exec [command] {} \;
  • path is the location in the file system that you want to find. It can be a specific directory or the root directory (/).

  • options are any additional options you want to use with the search command. These can include things like finding files with specific permissions or ownership, last modified time, and more.

  • command is the command you want to run on each file or directory it finds.

  • {} is a placeholder for the file or directory it found.

  • ;\ is used to terminate the “-exec” option.

It is important to note that the -exec option must end with a semicolon (;) and this semicolon must be preceded by a backslash (\) to ensure that the shell interprets the command correctly.

Examples

To illustrate the use of the find -exec command option, let's look at some examples.

Example 1 

Find all “.txt” files in the home directory and display their names

$ find ~ -name "*.txt" -exec ls -l {} \;

This command will search for all ".txt" files in the home directory and, for each file found, execute the “ls -l” command, with the path to the file as an argument. The “ls -l” command will display the file name, permissions, owner, size and other information.

Example 2 

Find all files in the home directory that have execute permission for the owner and delete them

$ find ~ -perm -u+x -exec rm {} \;

This command will search for all files in the home directory that have execute permission for the owner, and for each file found, execute the rm command, with the path to the file as an argument. The rm command will remove the file.

Example 3 

Find all files in the home directory that were last modified more than 30 days ago and move them to a backup directory

$ find ~ -mtime +30 -exec mv {} /backup/ \;

This command will search for all files in the home directory that were last modified more than 30 days ago and, for each file found, execute the mv command, with the path to the file as an argument. The mv command will move the file to the “/backup/” directory.

Note − The output of the above commands is specific to files and directories that exist in the home directory of the user running the commands. The outputs will be different for different users and different systems.

Advanced Use

The “-exec” option can also be used in combination with other options to perform more complex actions.

  • Using -ok instead of -exec will prompt the user for confirmation before executing the command for each file. This can be useful for commands that have destructive effects, like the rm command in the second example.

  • The -exec option can also be used in combination with an “-or” option to allow various commands to be executed on found files. For example, you can use the find command to locate all “.txt” files and then use the “-exec” option to run the ls “-l” and cat commands on the files.

    $ find ~ -name "*.txt" -exec ls -l {} \; -or -exec cat {} \;
    
  • The “-print” option can be used to print the path to each file found before running the “-exec” option.

    $ find ~ -name "*.txt" -print -exec ls -l {} \;
    

Conclusion

The find -exec command option is a powerful tool for searching for files and directories in a file system and performing actions on them. It can be used for tasks such as searching for specific files and then deleting, moving, or modifying them. The examples provided in this article should give you a good understanding of how to use the “find -exec” command option and the different options that are available. With practice and experimentation, you can use this command option to automate repetitive tasks and streamline your workflow.

Updated on: 20-Jan-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements