How to Find a File in Linux Using the Command Line

The 'find' command offers powerful options to hone your search

What to Know

  • The find command offers the most powerful and precise features to surface whatever you're looking for in Linux .
  • All modern Linux distributions support the find command from the shell.
  • To access the shell (sometimes called the terminal window) in most distributions, click the relevant icon or press Ctrl+Alt+T.

This article explains what the find command is, offers search location shortcuts, common expressions, example usages, patterns, how to send outputs from the find command to a file, and how to find and execute a command against a file.

The 'find' Command

The command used to search for files is called find.

The basic syntax of the find command is:

find filename

The currently active path marks the search location, by default. To search the entire drive, type the following:

find / filename

If, however, you want to search the folder you are currently in, use the following syntax:

find . filename
Find lorem.odt command in Terminal window on Ubuntu Linux

When you search by name across the entire drive, use the following syntax:

find / -name filename
  • The first part of the find command is the find command.
  • The second part is where to start searching from.
  • The next part is an expression that determines what to find.
  • The last part is the name of the file to find.

Search Location Shortcuts

The first argument after the find command is the location you wish to search. Although you may specify a specific directory, you can use a metacharacter to serve as a substitute. The three metacharacters that work with this command include:

  • Period (.): Specifies the current and all nested folders.
  • Forward Slash (/): Specifies the entire filesystem.
  • Tilde (~): Specifies the active user's home directory.

Searching the entire filesystem may generate access-denied errors. Run the command with elevated privileges (by using the sudo command) if you need to search in places your standard account normally cannot access.

Expressions

The most common expression you will use is -name, which searches for the name of a file or folder.

There are, however, other expressions you can use:

  • -amin n: The file was last accessed +/- n minutes ago, depending on how you enter the time.
  • -anewer: Takes another file as reference to find any files that were accessed more recently and the reference file.
  • -atime n: The file was last accessed more/fewer than n days ago, depending on the how you enter the target time (n).
  • -cmin n: The file was last changed n minutes ago, depending on how you enter the target time (n).
  • -cnewer: Takes another file as reference to find any files that were accessed more recently and the reference file.
  • -ctime n: The file was last accessed more/fewer than n days ago, depending on the how you enter the target time (n).
  • -empty: The file is empty.
  • -executable: The file is executable.
  • -false: Always false.
  • -fstype type: The file is on the specified file system.
  • -gid n: The file belongs to group with the ID n.
  • -group groupname: The file belongs to the named group.
  • -ilname pattern: Search for a symbolic link but ignore the case.
  • -iname pattern: Search for a file but ignore the case.
  • -inum n: Search for a file with the specified inode.
  • -ipath path: Search for a path but ignore the case.
  • -iregex expression: Search for an expression but ignore the case.
  • -links n: Search for a file with the specified number of links.
  • -lname name: Search for a symbolic link.
  • -mmin n: The file was last accessed +/- n minutes ago, depending on how you enter the time.
  • -mtime n: The file was last accessed more/fewer than n days ago, depending on the how you enter the target time (n).
  • -name name: Search for a file with the specified name.
  • -newer name: Search for a file edited more recently than the reference file given.
  • -nogroup: Search for a file with no group id.
  • -nouser: Search for a file with no user attached to it.
  • -path path: Search for a path.
  • -readable: Find files that are readable.
  • -regex pattern: Search for files matching a regular expression.
  • -type type: Search for a particular type. Type options include:
    • -type d: Directoris
    • -type f: Files
    • -type l: Symlinks
  • -uid uid: The file numeric user id is the same as the uid.
  • -user name: The file is owned by the user that is specified.
  • -writable: Search for files that can be written to.

Example Usage of the Find Command

Here are some of the ways you can use the find command.

How to Find Files Accessed More Than a Certain Number of Days Ago

To find all the files within your home folder accessed more than 100 days ago:

find ~ -atime +100

How to Find Empty Files and Folders

To find all the empty files and folders in your system:

find / -empty

How to Find All of the Executable Files

To find all the executable files on your computer:

find / -executable

How to Find All of the Readable Files

To find all the files that are readable:

find / -readable

Patterns

When you search for a file, you can use a pattern. For example, search for all files with the extension mp3:

find / -name *.mp3

Depending on the shell you're using, you may need to escape the asterisk. If you run the command and don't get the results you're expecting, try quoting the entire pattern to escape the asterisk, like so: find / -name '*.mp3'

Terminal in Ubuntu Linux showing command "find / -name *.mp3"

How to Send Output from the Find Command to a File

The main problem with the find command is that it can sometimes return too many results to look at in one go. Pipe the output to the tail command, or output the lines to a file as follows:

find / -name *.mp3 -fprint nameoffiletoprintto

How to Find and Execute a Command Against a File

To search for and edit a file at the same time, type:

find / -name filename -exec nano '{}' \;

The above command searches for a file called filename and then runs the nano editor for the file that it finds.

Nano is the name of a command, not an exact part of this syntax.