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

Examples

Yes, find has just too many options, I know. But there are a lot of cooked instances which are worth to remember, because they are usen very 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 \*foo\*.

% 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 specyfing 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 tring 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 index
Next: A last word Up: findthe file searcher Previous: Operators

Converted on:
Mon Apr 1 08:59:56 EST 1996