How to Find the Path of a File in Linux

Unlike Windows or macOS, Linux does not have a user-friendly “Search Bar” integrated into the corner of a graphical file explorer. If you need to edit a configuration file (like nginx.conf) but you cannot remember exactly which sub-folder it is buried in, you must command the terminal to scan the hard drive and print the absolute path.

There are two primary commands you can use to accomplish this: find (which is incredibly thorough but slow) and locate (which is lightning-fast but requires a database update). You must understand the difference to choose the right tool for the job.

Method 1: The “find” Command (The Slow, Thorough Search)

The find command is the absolute gold standard in Linux. It does not rely on cached data. The exact second you press Enter, the command physically crawls through every single folder on the hard drive, byte by byte, looking for a match. Because it does this in real-time, it is 100% accurate, but it can take several minutes to finish scanning a massive server.

  1. Open your terminal or SSH into your server.
  2. The basic syntax is: sudo find [starting_directory] -name "[filename]".

For example, if you want to find a file named error.log, and you want to scan the entire hard drive, type this command:

sudo find / -name "error.log"
  • sudo: Run as an administrator (otherwise Linux will throw “Permission Denied” errors when it tries to search restricted folders).
  • /: This tells the tool to start the search at the absolute root of the hard drive.
  • -name: This flag tells the tool to look for a specific string of text.

The terminal will pause for a moment, and then print the exact absolute path (e.g., /var/log/nginx/error.log) directly onto the screen.

Using Wildcards with “find”

If you don’t know the exact name of the file, you can use the asterisk (*) wildcard. For example, if you know the file is a configuration file (ends in .conf) and has the word “apache” somewhere in the name, you would type:

sudo find / -name "*apache*.conf"

This will return everything from apache2.conf to my_apache_backup.conf.

Method 2: The “locate” Command (The Lightning-Fast Search)

If you are frustrated by how slow the find command is, you should use locate instead. locate does not physically scan the hard drive. Instead, it instantly checks a master index database that the operating system builds every night at midnight. It returns results in milliseconds.

  1. First, ensure the command is actually installed (it often is not installed by default on minimalist server builds). Type:

sudo apt install mlocate (for Debian/Ubuntu) or sudo dnf install mlocate (for RedHat/CentOS).

  1. Once installed, the syntax is incredibly simple. Just type the command followed by the word you are looking for:

locate error.log

The terminal will instantly spit out every single file path on the system that contains that word.

The Crucial “updatedb” Step

The fatal flaw of the locate command is that it relies on an index database. If you created a brand new file ten minutes ago, the locate command will not be able to find it, because the database has not been updated yet (it only updates once every 24 hours).

If you want to use the locate command to find a recently created or moved file, you must manually force the database to refresh before you search.

  1. Type this command and press Enter:
sudo updatedb

The terminal will pause for a few seconds as it rebuilds the index. Once it is finished, you can run your locate command, and it will be 100% accurate.

Get the best tech tips delivered straight to your inbox.

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