Searching For Files And Folders With The find Command

Want to support HowtoForge? Become a subscriber!
 
Submitted by DanMilliken (Contact Author) (Forums) on Mon, 2013-09-02 19:25. :: Linux

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

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

 

OPTIONS

-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.

 

TESTS

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).

 

ACTIONS

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.

 

Examples

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


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Ryan (not registered) on Fri, 2013-11-15 08:21.

If you would like to understand paths better you can find a quite good introduction to them at the following tutorial:

http://linuxtutorial.todolistme.net