UNIX Find And Execute

Zach tells me I need to post how to find files and execute a command on those files. While the man pages are always the definitive reference, these are the options I use frequently. As usual, anything in italics should be replaced with the details for your search.

Note: WordPress loves to convert quotes into fancy quotes (pointing in from each side) so these commands may not work too well if copied and pasted. On the bright side, there’s no time like the present to start commiting these to muscle memory.

Find a file and execute a specific command:

find path conditions -exec command {} \;

find ./ -name "*.php" -exec cat {} \;

The open/close curly bracket marks where the name of each file found will be substituted and the backslash-semicolon marks the end of the command to execute.

Here are a few other options I use frequently:

Search for a file by name:

find path -name filename

find ./ -name style.css

Search by name with wildcards:

find path -name "filename"

find ./ -name "*.html"

Search for files modified within the past n days:

find path -mtime -n

find ./ -mtime -7

Search for files modified before the past n days:

find path -mtime +n

find /tmp -mtime +3

These are mostly based on my experience in Solaris using the bash shell. They should work just about everywhere, but check the man pages as mileage will vary.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!


unix, solaris, linux, mac osx, osx, sysadmin, systems administration

5 thoughts on “UNIX Find And Execute”

  1. Just remeber that exec creates [fork(2)] one process per file. if the cmd in can accept multiple arguments, then xargs will provide better performance.

    Simple exmaple, lets say you have a tree of files with emacs backup files scattered thought out, lets say there are 100 of them in total.

    find . -name \*~ -exec rm {} \;

    The above will remove them, but 100 rm processes will be created in addition to the single find process. Process creation is a _relatively_ slow operation, process to create, new command to load, dymanic libraries to load and initialise, etc…

    find . -name \*~ -print | xargs rm

    This will do the same job but only create one (or maybe a few more) rm processes.
    xargs(1) as describe in the manual page contructs cmd lines and execute them, it knows the max cmd line length and will only create additional cmdlines to execute if the cmd line its creating gets too long. So the process count here is find + xargs + rm * N where N will be most likely be

  2. Good tip Peter!

    Usually when I’m doing stuff like this I’m not too worried about performance (kick it off and walk away), but your right. xargs could save big on a large find.

    Thanks for sharing.

  3. In general this is what I find myself needing to do without GnuGrep’s -r flag conveniently available on Solaris:

    find . -exec grep -l ‘a bit of text’ {} \;

    This post has allowed me to recreate this about 20 times now, when I used to always ask you the syntax…

Leave a Reply

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