How to Search for Files in Linux from the Command Line
In this article
To search for files in Linux from the command line, you use the find command for most tasks, with locate as a faster alternative for pre-indexed searches, and grep for searching inside file contents. The most basic search is find /path -name "filename", which prints the full path of every matching file. This guide covers the essential commands, options, and practical examples so you can locate anything on your system quickly.
What You'll Need
- A Linux system with a terminal (any distribution works; the commands are standard).
- Basic familiarity with navigating the command line (cd, ls).
- Read permissions on the directories you search; otherwise, you'll get permission denied errors.
- For
locate, theplocateormlocatepackage may need to be installed (check withwhich locate).
Step-by-Step
- Open a terminal. Press
Ctrl+Alt+T(on most desktop environments) or search for "Terminal" in your app menu. - Search by name using
find. The basic syntax isfind [starting directory] -name [filename]. For example, to find a file callednotes.txtin your home directory:find ~ -name "notes.txt". The~is shorthand for your home directory. - Use wildcards for partial names. If you don't know the exact name, use
*(any characters) or?(one character). For example,find /var/log -name "*.log"finds all files ending in.logunder/var/log. Quote the pattern to prevent the shell from expanding it. - Search case-insensitively. Add
-inameinstead of-name:find /etc -iname "*.conf"finds files likeCONForConf. - Search by type. Use
-type ffor regular files,-type dfor directories,-type lfor symlinks. Example:find /home -type d -name "backup"finds directories named "backup". - Search by size. Use
-sizewith a suffix:cfor bytes,kfor kilobytes,Mfor megabytes,Gfor gigabytes. Example:find / -size +100Mfinds files larger than 100 MB. Use-for smaller than, e.g.,-size -1kfor files under 1 KB. - Search by modification time. Use
-mtime(days) or-mmin(minutes). Example:find ~ -mtime -7finds files modified in the last 7 days. A plus sign (+7) means more than 7 days ago. - Search by content with
grep. To find files containing a specific string, usegrep -r "search term" /path. For example,grep -r "error" /var/log/scans all files in that directory. Add-lto list only filenames:grep -rl "error" /var/log/. - Use
locatefor instant results. Iflocateis installed, runlocate filename. It searches a pre-built database, so it's much faster thanfind, but the database may be outdated. Update it withsudo updatedb(on most systems) before using if you need current results. - Combine
findwith other commands. Use-execto run a command on each result. For example, to delete all.tmpfiles in your home:find ~ -name "*.tmp" -exec rm {} \;. The{}is replaced by the filename, and the\;ends the command. Be careful with deletion. - Limit search depth. Use
-maxdepthto avoid scanning huge trees. Example:find /usr -maxdepth 2 -name "python*"only searches two levels deep.
Common Mistakes
- Forgetting to quote wildcards. If you run
find / -name *.logwithout quotes, the shell may expand*.logto matching files in the current directory, breaking the command. Always quote patterns:"*.log". - Using
-namewhen you need-iname. Linux filenames are case-sensitive, sofind / -name "README"won't findreadme. Use-inamefor case-insensitive. - Searching from
/without limiting depth.find / -name "foo"can take a long time and return many permission errors. Use-maxdepthor start from a more specific directory. - Ignoring permission errors. If you see "Permission denied", it means you don't have read access to that directory. Use
sudoonly if you need to search system files, but be careful. - Using
locatewithout updating. If you just created a file,locatewon't find it until the database is updated. Runsudo updatedbfirst.
Tips
- Use
find / -name "filename" 2>/dev/nullto suppress permission errors and see only results. - To search for multiple names, use
-o(OR):find ~ \( -name "*.txt" -o -name "*.md" \). Remember to escape the parentheses or quote them. - For a quick search of your current directory, use
find . -name "pattern"(the dot means current directory). - If you know part of the path but not the filename, use
-path:find / -path "*/project/*.conf". - Combine
findwithls -lto see details:find ~ -name "*.jpg" -exec ls -l {} \;. - For content search,
grep -ri "term" /pathmakes it case-insensitive and recursive. Add-nto show line numbers.
Troubleshooting
If find returns nothing: Check your spelling, case, and whether the file actually exists in the location you're searching. Try a broader search: find / -name "*partofname*".
If find is too slow: Limit the search with -maxdepth, or use locate if you have it. Also, avoid searching from / unless necessary.
If you get "Permission denied" errors: Run the command with sudo if you need to search system directories, but be aware that sudo gives you full access, so use it only when you trust the search. Alternatively, use 2>/dev/null to hide the errors.
If locate is not found: Install it with your package manager. On Debian/Ubuntu: sudo apt install plocate. On Fedora: sudo dnf install plocate. On Arch: sudo pacman -S plocate.
If grep -r says "Is a directory": That's normal; grep prints that for directories when you use -r without -d skip. Add -d skip to ignore directories: grep -r -d skip "term" /path.
If you accidentally delete files with -exec rm: There's no undo in the command line. Always test with -exec ls first to see what would be affected, and consider using -delete only after verifying.
Frequently Asked Questions
What's the difference between find and locate?
find searches the filesystem in real time, so results are always current, but it can be slow. locate uses a pre-built database (updated usually daily), so it's instant but may not reflect recent changes.
How do I search for a file by its content?
Use grep -r "text" /path . For example, Add -i for case-insensitive.
Can I search for files modified in the last hour?
Yes, use find /path -mmin -60. The number is minutes; use -mtime -1 for the last 24 hours.
How do I search for files with a specific extension?
Use find /path -name "*.ext". For example, find ~ -name "*.pdf". Remember to quote the pattern.
Why does find show "Permission denied" so often?
Because many system directories (like /root or /proc) are restricted. Use 2>/dev/null to hide those messages, or run with sudo if you need to access them.
How do I search for directories, not files?
Add -type d to your find command. Example: find / -type d -name "config".