If you misplace a file in a modern desktop environment, you simply type its name into the Windows search bar or Mac Spotlight, and it appears instantly. When you are managing a Linux server through a text-based terminal, you do not have that luxury. If you know a file named “database_backup.sql” exists somewhere on the hard drive but you don’t know which folder it is in, you must use the incredibly powerful find command.
Why use “find” instead of “locate”?
Linux actually has another search command called locate, which is incredibly fast. However, locate relies on a pre-built index of the hard drive that only updates once a day. If you created a file five minutes ago, locate will not know it exists. The find command, on the other hand, performs a real-time, live crawl of your directories, ensuring 100% accuracy every time.
The Basic Syntax of the find Command
The find command requires three pieces of information to work correctly: where to start searching, what criteria to use, and what exactly to look for.
The syntax looks like this:
find [where to start] -name [filename]
Example 1: Searching a Specific Directory
If you know a file is somewhere inside the massive /var/www/ directory, but you don’t know which subfolder it is buried in, you can tell find to start its search there.
find /var/www/ -name "config.php"
Linux will start at /var/www/, crawl through every single folder inside it, and print the exact, absolute path to any file named exactly “config.php”.
Example 2: Searching the Entire Hard Drive
If you have absolutely no idea where the file is located on the system, you can tell find to start at the root directory (represented by a single forward slash /). This will search the entire hard drive.
find / -name "database_backup.sql"
Note: Searching the root directory often results in dozens of “Permission denied” errors as the command tries to look inside protected system folders. To hide those errors, you can run the command as a superuser using sudo find / -name....
Example 3: Case-Insensitive Searching
Linux is strictly case-sensitive. If you use -name "Invoice.pdf", it will completely ignore a file named “invoice.pdf”. If you are unsure how the file was capitalized, replace -name with -iname (the “i” stands for insensitive).
find /home/user/ -iname "invoice.pdf"
Example 4: Using Wildcards (The Asterisk)
The true power of the find command is its ability to use wildcards (asterisks). An asterisk acts as a placeholder for “any character.”
If you want to find every single JPEG image inside a folder, regardless of its name, you can search for the extension.
find /home/user/Downloads/ -name "*.jpg"
If you are looking for a log file but can’t remember the exact date in the filename, you can search for a partial match.
find /var/log/ -name "*error_log*"
This will find “apache_error_log.txt”, “error_log_2023”, and any other variation containing that phrase.