UNIX Find And Execute
Categories: Information Technology, System Administration, UNIXZach 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.
For more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.Buy it now!

May 9th, 2006 at 10:55 pm
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
May 10th, 2006 at 7:51 am
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.
October 19th, 2006 at 10:46 am
Good tips, thank you.
Amazon have this book for better price.
http://www.amazon.com/Easy-Linux-Commands-Examples-Command/dp/0975913506/sr=8-1/qid=1161269074/ref=pd_bbs_sr_1/002-2287464-9167267?ie=UTF8
April 13th, 2007 at 9:22 am
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…
June 27th, 2007 at 11:08 am
Gnu find and xargs have a nice feature which helps when file names contain spaces: find . -name \*~ -print0 | xargs -0 rm