Introduction

The find command is a useful command-line tool in Linux.

It allows you to use the terminal to search for a specific string of characters in a specified location.  The find command in Linux offers a method for control over search parameters, options, and locations. As it can be used from the terminal, it’s an incredibly handy and versatile tool to understand.

This tutorial will teach you how to find files in Linux Using the Command Line.

  • A Linux system
  • Access to a terminal or command line (Ctrl-Alt-T in Ubuntu, Alt-F2 in CentOS)
  • A user account with sudo privileges

Prerequisites

  • A Linux system
  • Access to a terminal or command line (Ctrl-Alt-T in Ubuntu, Alt-F2 in CentOS)
  • A user account with sudo privileges

NOTE

The sudo command is not normally needed to use the find command. However, some search locations are restricted to a regular user, and may require elevated privileges.

Section/Step

About the FIND Command in Linux

The find command is a tool that lists out all instances of a particular string of characters. Typically, you’ll type a filename that you want to locate, but find is not limited just to a simple filename. You can use it to search for a specific location, provide options to control its behavior, and use other commands to tell it what to do with the things it finds.

The basic format or syntax of the find command looks like this:

find <options> <location> <expression> <action> <name>

The <options> section is optional, and there are a few specific options you can choose from.  Most of the time, you’ll go straight to the <location>, which tells find where to start looking for your search term.  The <expression> tells find what category of information it’s supposed to look for.  The <action> field is optional, but gives you the ability to run a command on the search results.  Finally, the <name> is the string of text (or other data) that you want it to display.

Briefly:

  • Options control the behavior of the search
  • Location tells the system where to start searching
  • Expression tells the system what kind of data you’re searching for
  • Action is optional, and it tells the system to perform an action on the results
  • Name is the actual thing that you’re searching for

Location

The location string can be as simple as:

find /home/user filename

This would search the user account in the home directory.  If you have a good idea where you want to look, you can specify the location to search.  For example, if you have two user accounts and you don’t remember which one you saved a file to, you can enter:

/home

The find command will search everything under the /home directory (including all user directories) for your search term.

There are a few helpful shortcuts to know with location.  First, if you use a period (.), find will search the directory you’re currently in.  So whatever directory is listed in your command prompt, entering a period will search that directory.

Second, if you enter a simple / (slash), the system will search the whole hard drive.

Third, you can use the ~ (tilde) character to search the home directory of the current user.

Expression

The expression field can be both tricky and powerful.  There are many different options that you can use in this field, and each tells find what kind of file you’re looking for.

The most common expression is –name.  This searches the location you’ve specified for a file with the name you type in the name field.

If we enter the following:

find /home –name somefile

The system will search the home directory for any file with the name somefile.  This only displays filenames with an exact match to your search terms.  This might cause problems if a filename has Capital Letters.  To tell find to ignore upper/lower case, use the –iname expression, as follows:

find /home –iname somefile

The system will search the /home directory, and return anything named somefile, SomeFile, and even sOmEfIlE.

By default, find will look for files.  But you can use the –type expression to specify searching for a file or a directory:

find . –type d –name pictures

This command would search the current directory (with the period) for a directory with the name of pictures.  By default, find will search for files, but you can specify files by using -type f.

Some of the common expressions you might use include:

  • -name filename: search by a string of characters
  • -iname filename: a string of characters that ignores upper/lower case
  • -not filename: show all files that don’t include the name
  • -type f or -type d: search for files or directories (respectively)
  • -perm 0000: search for files with a given permission level
  • -user username: return all files owned by username
  • -mtime 00: show the last 00 files modified
  • -atime 00: show the last 00 files accessed
  • -size +00M -11G:  show all files greater than 00 megabytes and less than 11 gigabytes (these commands can be used individually to show all files larger than a certain size, smaller than a certain size, or between a range of sizes)

Name

The name section is where you tell find what you’re looking for.  The find tool will search the specified location for whatever string of characters you enter.  Normally, the name of the file you’re looking for comes at the end of the command, after the location, the options, and the expressions.

For normal letters and numbers, you can simply enter the string of characters at the end of the command.  However, if you’re using special characters – a wildcard, a period, a slash, etc. – use single quotes around the name for clarity.

For example:

find /home –name filename

This command would look in the /home directory for all files called filename.

To search for files with a specific extension, like .php or .jpg:

find / –type f –name ‘*.php’

The system will search the whole hard drive (because of the slash) for regular files (type –f) that have an extension of .php.  In this case, the asterisk (the * sign) is used as a wildcard, meaning that it can stand in for any number of characters of any type.  Wildcards can be useful if you only remember part of a filename, or are searching for files with a particular extension.

Options and Actions

Options are special functions added to the beginning of a find command, before the search path.  Some options tell find how to handle symbolic links (shortcuts), such as –L –H –P.  Another option is to change the search optimization level with –O.  By default, find runs at –O1, which is optimization level 1.  –O2 and –O3 will adjust the order of operations in order to perform quicker options first.  Most users are unlikely to need these options.

Actions are additional things that find can do.  Normally, find simply displays the search results in your terminal window.  An action term can enhance that functionality to perform a task when a file is found.

For example, this command will search the contents of files (instead of just the names of files):

find ~ –type f –exec grep “contents” ‘{}’ \; –print

This might look really complicated, but most of the additional characters are part of the grep function.  Working from the beginning, we’re asking the system to find, in our user’s home directory (~),  a file (-type f).  Then, we’re telling the system to take action (-exec) by running the grep command to search through the contents of all the files for the word “contents.”  The {} brackets indicate the match results of the find command, and they’re in single quotes so that the grep command doesn’t misinterpret them.  We’re closing out the grep command with a semicolon (;), and we’re using a backslash (\) so that the system doesn’t misinterpret the semicolon as an instruction.  Finally, the –print option tells the system to display the results.

More simply, you can just copy and paste this command into your terminal window, and substitute your search path for the ~ character, and your search term for the word contents.

Another way you can use the find –exec combination is to change file permissions:

find /path –name “filename.ext” –exec chmod 777 ‘{}’ \;

This uses the –exec command to run chmod to change the file permissions of the file filename.ext so that it can be read, written, and executed by everyone.

A third example might be to find and delete a file.  BE CAREFUL USING THIS FUNCTION – deleted files may be unrecoverable, and deleting system files can make your system unstable or unusuable.

find . –name “*.bmp” –delete

This command will search the current directory (displayed in your command prompt) for all files with the .bmp (bitmap) extension, and delete them.  This can be useful for deleting old backup files, if you substituted “*.bak”  instead of “*.bmp”..

Conclusion

The find tool in Linux can be an extremely powerful utility.  It can help keep track of files on a shared system.  It could help you locate a file that’s gone missing, or that has been accidentally moved.  You could use it to search for a file that you created or saved, but don’t recall which directory it was saved to.

More powerfully though, the find tool can help administrators with system management.  The ability to use grep to search the contents of files can be especially useful looking for keywords within files.  Tools like chmod can be a handy way to grant user access to a file (or directory, with –type d).  And with the –delete command, you can quickly find and delete one or multiple files in one command (for example, find / -iname "minecraft.*" -delete.)

For more information about specific options, you can type the following in your terminal:

man find

The man command is short for “manual,” and will display options for the find command.