Title: find Command
Course: CIS 121: Linux/UNIX I: Fundamentals ~ For IGNITE Review > Section: Learning Modules

The find command can be very useful at the start of a pipe to search for files. Find will search for those files in the location where you specify. The find command is recursive - meaning it will search in all the subdirectories also.

Below are some examples.

Find all files in and below /etc and put the list in etcfiles.txt.
find /etc > etcfiles.txt

Find all files of the entire system and put the list in allfiles.txt.
find / > allfiles.txt

Find files in and below the current directory that end in .conf.
find . -name "*.conf"

Find files in and below the current directory, of type file (not directory, pipe or etc.), and that end in .conf.
find . -type f -name "*.conf"

Find files in and below the /data directory, of type directory, and that end in .bak.
find /data -type d -name "*.bak"

Find files in and below the current directory that are newer than file42.txt.
find . -newer file42.txt

Find can also execute another command on every file found. This example will look for *.odf files in and below the current directory and copy them to /backup/.
find /data -name "*.odf" -exec cp {} /backup/ \;

When using the -exec or -ok command:

- {} will be replaced by the current file name being processed.
- \; is needed at the end and specifies the end of the find command.

Find can also execute, after your confirmation, another command on every file found. This example will remove *.odf files if you approve of it for every file found.
find /data -name "*.odf" -ok rm {} \;

(Cobbaut, p.137)