Searching For Files And Folders With The find Command

In this tutorial we'll look at the find command and how you can quickly use it to locate files in your filesystem. Find is a powerful utility capable of locating files anywhere on your system including mounted drives and removable storage, processing regular expressions, and even running other commands on those files. Fortunately only a few simple options are needed to provide most users with all the capability they need.

The format of the find command is:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

The H, L and P options specify whether to follow symbolic links. debugopts exists to provide debugging information, and -0level provides query optimization. For this tutorial and most of your use of find, we'll only need to specify the path and the expression.


The Path

The path specifies where to look on the filesystem. Find will search this path and all subdirectories. To search the entire filesystem, specify '/' for the path.



expression consists of three parts: OPTIONS, TESTS, and ACTIONS.



-help This will print out a short summary of the find command usage

find -help

-mount This option tells find not to search directories on other filesystems, such as mounted USB drives and other volumes that can be slower to search.



The tests section tells find what to look for.

-executable This tell find to only return files that are executables and directories. It's useful for finding a directory that you know the name of (combine with the -file d flag) or an executable (combine with the -file f flag).

-name/-iname This gives the string of the file (or directory) name you're searching for. It should be enclosed in double quotes ("). Metacharacters (`*',`?', and `[]') can be used for parts of the name. -name makes the search case sensitive and -iname makes it case insensitive.

-regex use this instead of -name to search for a name using an Emacs style regular expression.

-type Use this to specify if you're looking for a file (-type f) or directory (-type d).



This section is used to perform actions on the results of the find command.

-exec Execute a command on each file found with the filename passed to the command.


Suppressing Errors

Normally when searching in all directories with your non-root user account, you'll get lots of errros like: "find: `/var/spool/cron/crontabs`: Permission denied. We can remove these from the output by sending them to /dev/null by appending 2>/dev/null to the find command.



Search the entire filesystem for an executable

find / -name "grep" -executable -type f 2>/dev/null

Search the entire filesystem (but not mounted drives) for a directory

find / -mount -executable -iname "music" -type d 2>/dev/null


Further Reading

This tutorial only covers a portion of the capabilities of find. I hope it's enough to do most of what you need, but there are many more options in the find manual.

man find

For information on regular expressions to give you more flexibility with your search terms, see the man page for POSIX regular expressions

man 7 regex
Share this page:

2 Comment(s)