next up previous contents
Next: A last word Up: findthe file Previous: Operators

Examples

Yes, I know that find has too many options. But there are a lot of instances which are worth remembering, because they are used often. Let's see some of them.

% find . -name foo\* -print
finds all file names starting with foo. If the string is embedded in the name, probably it is more sensitive to write something like "*foo*", rather than *.

% find /usr/include -xtype f -exec grep foobar \
           /dev/null {} \;
is a grep executed recursively starting from directory /usr/include. In this case, we are interested both in regular file and in symbolic links which point to regular files, hence the -xtype test. Many times it is simpler to avoid specifing it, especially if we are rather sure no binary file contains the wanted string. And why the /dev/null in the command? It's a trick to force grep to write the file name where a match has been found. The command grep is applied to each file in a different invocation, and so it doesn't think it is necessary to output the file name. But now there are two files, i.e. the current one and /dev/null! Another possibility should be to pipe the command to xargs and let it perform the grep. I just tried it, and completely smashed my filesystem (together with these notes which I am trying to recover by hand :-( ).

% find /  -atime +1 -fstype ext2 -name core \
              -exec rm {} \;
is a classical job for crontab. It deletes all file named core in filesystems of type ext2 which have not been accessed in the last 24 hours. It is possible that someone wants to use the core file to perform a post mortem dump, but nobody could remember what he was doing after 24 hours...

% find /home -xdev -size +500k -ls > piggies
is useful to see who has those files who clog the filesystem. Note the use of -xdev; as we are interested in just one filesystem, it is not necessary to descend other filesystems mounted under /home.



next up previous contents
Next: A last word Up: findthe file Previous: Operators



Ross Biro
Thu May 25 10:45:54 PDT 1995