When operating a Linux server without a graphical desktop environment, you cannot rely on a visual search bar to find misplaced files. While the `grep` command is excellent for searching the contents of a file, it is useless if you simply want to locate a file based on its name, size, or creation date. For this, you need the find command.
The find command is arguably the most powerful search utility in the Linux terminal, allowing you to traverse complex directory trees and execute actions on the files you discover. In this guide, you will learn the essential syntax for locating exactly what you need.
The Basic Syntax
The fundamental structure of the command is:
find [where_to_search] [search_criteria] [what_to_find]
- where_to_search: The directory where the search should begin (e.g.,
/var/www/or.for the current directory). - search_criteria: The flag dictating how to search (e.g., by name, size, or type).
- what_to_find: The actual string, number, or pattern you are looking for.
1. Searching by File Name
The most common use case is searching for a file when you know its exact name.
find /etc -name "nginx.conf"
This command searches the entire /etc directory (and all its subdirectories) for a file named exactly “nginx.conf”.
Ignoring Case Sensitivity
If you are not sure if the file is capitalized, use the -iname flag instead, which ignores case distinctions:
find / -iname "MyDocument.txt"
(Note: Searching the root directory / will search your entire hard drive and may take a few minutes).
2. Using Wildcards (Partial Matches)
If you only know part of a filename, or want to find all files of a specific type, you must use the asterisk (*) wildcard.
To find every single PDF file inside your home directory:
find ~ -name "*.pdf"
To find any log file that begins with the word “error”:
find /var/log -name "error*"
3. Searching by File Size
If your server is running out of disk space, you can use find to hunt down massive, forgotten files using the -size flag.
Size modifiers include k (Kilobytes), M (Megabytes), and G (Gigabytes). You use + to search for files larger than a number, and - for files smaller.
To find all files larger than 500 Megabytes in the current directory:
find . -size +500M
To find files exactly equal to 10 Kilobytes:
find / -size 10k
4. Searching by File Type
Sometimes you only want to search for directories, not files (or vice versa). You can restrict the search using the -type flag, followed by f (file) or d (directory).
To find a folder named “backups” anywhere on the system, ignoring any files named “backups”:
find / -type d -name "backups"
5. Searching by Modification Time
If a web application was hacked yesterday, you can search for any PHP files that were modified in the last 24 hours using the -mtime (modification time in days) flag.
To find files modified exactly 1 day ago:
find /var/www -name "*.php" -mtime 1
To find files modified less than 3 days ago:
find /var/www -name "*.php" -mtime -3
By mastering the find command, you eliminate the need to manually rummage through deep directory structures, allowing you to administer Linux servers with speed and precision.