How to execute commands on the files you find with ‘find’

How to execute commands on the files you find with ‘find’

If you want to run a command on every file found with the ‘find’
command, you can use the ‘-exec’ command line option:

# find . -mtime +10 -exec rm -r {} \;

The find command is normal, and we won’t be explaining it here. Suffice
it to say that this command will find all files under the current
working directory that are 10 days old or older, and delete them.

    * The ‘-exec’ option tells find to execute the command that follows on
      every file found.
    * The ‘rm -r’ is the command that will be executed.
    * The ‘{}’ (curly braces) in the find command are special. They are
      replaced, at run-time, with the currently “found” file.
    * The ‘\;’ is just the end-of-line marker for the find command. This is
      necessary because you could easily tell find to execute a new find
      within the ‘-exec’ part of the command line. When you nest commands
      like this, you need some way to identify the end of a particular
      command. The ‘;’ does this. The ‘\’ is to escape the ‘;’, because
      the ‘;’ has special meaning to the shell, as well as to the find
      command.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s