How to Find a File by Name in Ubuntu Terminal

When you are managing a Linux server through a command-line interface, you do not have the luxury of a visual file explorer or a graphical search bar. If you know you downloaded a crucial configuration file called database-backup.sql last month, but you cannot remember if you saved it in your home directory, the /tmp folder, or the /var directory, searching for it manually by typing ls in every single folder could take hours.

Ubuntu provides an incredibly powerful, native search utility designed to scour the entire hard drive in seconds. It is called the find command.

The Basic ‘find’ Command Syntax

The find command requires two essential pieces of information: where to start looking, and what to look for.

  1. Open your terminal or SSH into your server.
  2. To search the entire server for a specific file, type the following command and press Enter:
find / -name "database-backup.sql"

Breaking Down the Command:

  • find: The application itself.
  • /: This tells the command where to start searching. The forward slash represents the “root” directory. By starting at the root, you are instructing the tool to search every single folder and sub-folder on the entire hard drive. If you only wanted to search your personal documents, you would change this to /home/user/ to save time.
  • -name: This flag tells the tool that you are searching for an exact, case-sensitive filename.
  • "database-backup.sql": The exact name of the file you are looking for.

The terminal will pause for a few moments as it scans the hard drive, and will output the absolute path to the file once it finds it (e.g., /var/backups/database-backup.sql).

How to Ignore Capital Letters

Linux is strictly case-sensitive. If you search for “Backup.sql” using the -name flag, it will completely ignore a file called “backup.sql” because the ‘b’ is lowercase.

If you cannot remember exactly how you capitalized the filename, you should use the case-insensitive flag (-iname) instead:

find / -iname "backup.sql"

This will successfully find “Backup.sql”, “backup.sql”, and even “BaCkUp.SqL”.

How to Use Wildcards (The Asterisk)

The most powerful feature of the find command is the ability to use wildcards (represented by an asterisk *) to search for partial matches. The asterisk acts as a placeholder for “any character.”

For example, if you know the file is a picture, but you cannot remember the name, you can tell Ubuntu to find every JPEG file on the server:

find /home/user/ -name "*.jpg"

If you remember that a file started with the word “report” but you cannot remember the date attached to the end of it, you would use the wildcard at the end:

find / -name "report*"

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.