RSS Feed

Executing a command with input from find output

Files on Linux can be found in quite a few different ways, two of the more common commands are find and locate. Personally I tend to use find more so over locate due to the higher search-ability and actions available through the command, one of which is executing another command based on the output of find. This is where this howto comes in.

As an example, let’s say you are trying to find all the .svn directories within the current directory and once found would like to remove them.

Finding all .svn directories within the current directory with find is pretty simple:

find . -type d -name ‘.svn’

Without specifying the -type option find will search files as well as directories. Specifying “d” as the options parameter limits the results to directories, “f” would limit the results to files only.

Using the -name option limits the results to the specified parameter.

The output of this command can then be passed to another command, in this example we’re going to remove the directories.

find . -type d -name ‘.svn’ -exec rm -rf {} \;

The -exec option takes a command as the parameter. The two curly braces “{}” represent the found file or directory name and the final part to the command “\;” escapes and ends the -exec parameter.

It’s extremely easy to use, once you know how to.

Posted in Linux on the 20th April 2010

One person has spoken their mind!

  1. IByDev says:

    Another useful post srcnix, will bookmark for later!

    Thanks!

SPEAK YOUR MIND...

Your email address will not be published. Required fields are marked *

*