Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Search for Files in Linux from the Command Line

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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, the plocate or mlocate package may need to be installed (check with which locate).

Step-by-Step

  1. Open a terminal. Press Ctrl+Alt+T (on most desktop environments) or search for "Terminal" in your app menu.
  2. Search by name using find. The basic syntax is find [starting directory] -name [filename]. For example, to find a file called notes.txt in your home directory: find ~ -name "notes.txt". The ~ is shorthand for your home directory.
  3. 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 .log under /var/log. Quote the pattern to prevent the shell from expanding it.
  4. Search case-insensitively. Add -iname instead of -name: find /etc -iname "*.conf" finds files like CONF or Conf.
  5. Search by type. Use -type f for regular files, -type d for directories, -type l for symlinks. Example: find /home -type d -name "backup" finds directories named "backup".
  6. Search by size. Use -size with a suffix: c for bytes, k for kilobytes, M for megabytes, G for gigabytes. Example: find / -size +100M finds files larger than 100 MB. Use - for smaller than, e.g., -size -1k for files under 1 KB.
  7. Search by modification time. Use -mtime (days) or -mmin (minutes). Example: find ~ -mtime -7 finds files modified in the last 7 days. A plus sign (+7) means more than 7 days ago.
  8. Search by content with grep. To find files containing a specific string, use grep -r "search term" /path. For example, grep -r "error" /var/log/ scans all files in that directory. Add -l to list only filenames: grep -rl "error" /var/log/.
  9. Use locate for instant results. If locate is installed, run locate filename. It searches a pre-built database, so it's much faster than find, but the database may be outdated. Update it with sudo updatedb (on most systems) before using if you need current results.
  10. Combine find with other commands. Use -exec to run a command on each result. For example, to delete all .tmp files in your home: find ~ -name "*.tmp" -exec rm {} \;. The {} is replaced by the filename, and the \; ends the command. Be careful with deletion.
  11. Limit search depth. Use -maxdepth to 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 *.log without quotes, the shell may expand *.log to matching files in the current directory, breaking the command. Always quote patterns: "*.log".
  • Using -name when you need -iname. Linux filenames are case-sensitive, so find / -name "README" won't find readme. Use -iname for case-insensitive.
  • Searching from / without limiting depth. find / -name "foo" can take a long time and return many permission errors. Use -maxdepth or 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 sudo only if you need to search system files, but be careful.
  • Using locate without updating. If you just created a file, locate won't find it until the database is updated. Run sudo updatedb first.

Tips

  • Use find / -name "filename" 2>/dev/null to 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 find with ls -l to see details: find ~ -name "*.jpg" -exec ls -l {} \;.
  • For content search, grep -ri "term" /path makes it case-insensitive and recursive. Add -n to 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".