May 27, 2021

Examples of using find in Unix like systems

FreeBSD text image

find is one of the Unix utilities that is most often used. As the name says it is used to find something i.e. files. It has many options you can use to specify which files to look for.

One of the simplest uses is find . -name somefile that will look for the file named somefile in the current directory. If you use find . -iname somefile a case insensitive search will be done for somefile.

Here are some more advanced examples. use man find on your system to find out all the available options for your system.

find / \! -name "*.c" -print
Print out a list of all the files whose names do not end in .c.

find / -newer somefile -user grumpy -print
Print out a list of all the files owned by user “grumpy” that are newer than the file somefile.

find / \! \( -newer somefile -user grumpy \) -print
Print out a list of all the files which are not both newer than somefile and owned by “grumpy”.

find / \( -newer somefile -or -user grumpy \) -print
Print out a list of all the files that are either owned by “grumpy” or that are newer than somefile.

find / -newerct '1 minute ago' -print
Print out a list of all the files whose inode change time is more recent than the current time minus one minute.

find / -type f -exec echo {} \;
Use the echo(1) command to print out a list of all the files.

find -L /usr/ports/packages -type l -exec rm -- {} +
Delete all broken symbolic links in /usr/ports/packages.

find /usr/src -name CVS -prune -o -depth +6 -print
Find files and directories that are at least seven levels deep in the working directory /usr/src.

find /usr/src -name CVS -prune -o -mindepth 7 -print
Is not equivalent to the previous example, since -prune is not evaluated below level seven.

There are many more examples at the Wikipedia find page.

One of the more interesting uses of the find command is to take some action on the files you find. There is of course more than one way to do this.

find /var/ftp/mp3 -name '*.mp3' -type f -exec chmod 644 {} \;
This command changes the permissions of all regular files whose names end with .mp3 in the directory tree /var/ftp/mp3. The action is carried out by specifying the statement -exec chmod 644 {} \; in the command. For every regular file whose name ends in .mp3, the command chmod 644 {} is executed replacing {} with the name of the file. The semicolon (backslashed to avoid the shell interpreting it as a command separator) indicates the end of the command.

find /var/ftp/mp3 -name '*.mp3' -type f -exec chmod 644 {} +
If you will be executing over many results, it is more efficient to use the variant above of the exec primary that collects filenames up to ARG_MAX and then executes COMMAND with a list of filenames.

If you are on a system that does not have the exec + variant in find you can pipe the output of find to xargs that will do the same as the above example. You will often see this use of find and xargs in examples on the web as it is more portable. man xargs on your system to find all the options to xargs available to you.
find /var/ftp/mp3 -name '*.mp3' -type f |xargs chmod 644

Another interesting use is combining find with cpio to create an archive or copy of a specific set of files

find . -iname \*jpg -print -o -iname \*jpeg -print -o -name websites -prune|cpio -p backupdir will copy all files ending in jpg or jpeg to the directory backupdir, but not files in the directory websites.

find . -iname \*jpg -print -o -iname \*jpeg -print -o -name websites -prune|cpio -o > backup.cpio will create n archive of all files ending in jpg or jpeg, but not files in the directory websites.

© Arnold Greyling 2023