More Linux and UNIX find command options
Categories: Information Technology, System Administration, UNIXLike many Linux and UNIX commands, the find command supports a long list of options. I covered several of the options in a previous article but there are even more useful options. Here are some of them:
Doing things with what we find
The –exec option gives find the powerful ability to execute commands on the files found. The syntax is a little tricky but an example is usually all it takes to get it right.
Before using the -exec option, especially with a powerful command like rm I recommend performing the same find without the –exec. By doing this you will see exactly which files you will be affecting when you run the final command.
The following is a practical example that finds files less than three days old with the .txt extension and deletes them.
Finding .txt Files < 3 Days Old and Delete Them
$ find . -name '*.txt' -mtime -3 -exec rm {} \;
$ ls –lt
total 8
-rw-r--r-- 1 tclark authors 2229 Jan 13 21:35 declaration.txt
-rw-rw-r-- 1 tclark presidents 1310 Jan 13 17:48 gettysburg.txt
The –exec option allows you to put any command after it. Here we have used rm but it is often useful to use this option with cp or chmod. Within the command to be run there must be two curly brackets {}. find will execute the command for each file it finds substituting the file name (and path) where the curly brackets are. Finally the end of the –exec option is signaled by an escaped semicolon (\;). The –exec option should always be the last option given in a find command.
The find command is great for finding files and directories but next we’ll look at some options for finding other things on the system.
Dealing with “Permission denied” in find
If you use find a lot (and you probably will) you will sometimes run into the problem where you get just pages and pages of output like this:
$ find / -name '*.txt'
find: /var/lib/dav: Permission denied
find: /var/lib/nfs/statd: Permission denied
find: /var/lib/dhcpv6: Permission denied
find: /var/lib/slocate: Permission denied
find: /var/lib/xdm/authdir: Permission denied
find: /var/lib/php/session: Permission denied
find: /var/log/samba: Permission denied
find: /var/log/ppp: Permission denied
find: /var/log/audit: Permission denied
find: /var/log/squid: Permission denied
...
This is find telling you there are certain directories you don’t have permissions to search. This can make it very difficult to find the useful output of the find as it can be mixed in with the permissions errors.
To ignore these (and any other) errors and just get the results of what you can find we can use a special redirect at the end of the command. Redirecting output will be covered in more detail in the chapter on shell scripting, but suffice it to say that in this command 2>/dev/null is redirecting the error output to nowhere.
$ find / -name '*.txt' 2>/dev/null
/var/www/icons/small/README.txt
/usr/X11R6/lib/X11/rgb.txt
/usr/X11R6/lib/X11/doc/Xprint_FAQ.txt
/usr/lib/4Suite/tests/Xml/Core/count.txt
...
While it would not be a good idea to redirect the error output all the time (usually you want to know when something has gone wrong) in this case of the find command it can be very useful.
For more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.Buy it now!
9 Responses to “More Linux and UNIX find command options”
-
Peter Says:
May 4th, 2007 at 10:41 amAn easy way to get rid of the error messages is to have find test for permissions before trying to go into the directory:
For example to look for files named “help” use find like this:
find . \( -type d ! -exec test -x {} -a -r {} \; -prune \) -o \( -name help ) -exec ls -ld {} \;The crucial part is
-type d ! -exec test -x {} -a -r {} \; -prunewhich test for the readability of the directory and does not traverse it if it is not readable or executable. -
Tom Says:
May 4th, 2007 at 11:27 am“Dealing with “Permission denied†in find”
– thanks Jon, that was exactly the command I was just looking for
-
Robert Says:
November 14th, 2007 at 10:34 amThanks! Useful!
-
gian Says:
December 18th, 2007 at 4:56 amhello Jon,
thanks for the help.
just to make you notice that:
$ find . -name ‘*.txt’ -mtime -3 -exec rm {} \;
will delete all txt files, since “-3″ means “with a date that is 3 days ahead”. If you wish to delete files which are 3 days older you should type:
$ find . -name ‘*.txt’ -mtime 3 -exec rm {} \;
ciao -
Jon Emmons Says:
December 18th, 2007 at 9:00 pmGain,
From my experience
-mtime 3would return (and in this example remove) the files which were modified exactly 3 days ago.mtime -3finds files modified less than 3 days ago and-mtime +3would find files modified more than three days ago.I’ve used this syntax on Linux and several varieties of UNIX but these things do vary by platform. What platform have you used this on? If there is a platform which has the results you mention I’d like to add a warning to this article!
Thanks.
-
Shan Says:
May 13th, 2008 at 12:24 pmDealing with “Permission denied†in find which is really useful to me. Especially When I try to find the largest files in solaris, I get this error. Now I am getting my desired output with out this error. Thanks for your tips.
With best regards
Shan -
Jim Says:
March 19th, 2010 at 6:25 amGreat tips and very useful.
Many thanks
Jim -
J_R Says:
August 4th, 2010 at 7:26 amUsing “2>/dev/null” to prevent “Permission denied” does not work if trying to find which file(s) a particular piece of text occurs in e.g.
find . -type f -print | xargs grep “dctm.gif” 2>/dev/null
Only works with if you are trying to find an actual file(s)
-
tim Says:
June 6th, 2011 at 3:51 pmthis works great for avoiding paths to be printed where the said object is not present
find / name “text” 2>/dev/null | grep -r text
You can skip 2>/dev/null altogether if you don’t want to look at paths.