Searching for files efficiently is an essential skill for Linux users, developers, and system administrators. Linux provides multiple tools and commands to locate files, directories, and even search inside their contents.

The “find” command is one of the most versatile tools for locating files based on names, types, sizes, modification times, and even permissions.

Basic Syntax

find [path] [options] [expression]

a) Search by Filename

find /home -name "report.pdf"
  • /home → Search path.

  • -name → Match exact filename (case-sensitive).

🔹 Case-insensitive search:

find /home -iname "report.pdf"

b) Search by Extension

find /var/log -type f -name "*.log"
  • -type f → Search files only.
  • Use -type d for directories.

c) Search by Size

find / -size +500M
  • +500M → Files larger than 500 MB.
  • -100k → Files smaller than 100 KB.

d) Search by Modification Time

find /etc -type f -mtime -7
  • -mtime -7 → Files modified within the last 7 days.
  • Use +7 to find files older than 7 days.

e) Execute Commands on Found Files

find /var/log -name "*.log" -exec gzip {} \;
  • Compresses all .log files inside /var/log.

f) Combine Multiple Conditions

find /home -type f \( -name "*.pdf" -o -name "*.docx" \) -size +1M
  • Searches for PDF or DOCX files larger than 1 MB.

Faster Searching with the “locate” Command(High Performance)

Unlike “find“, “locate” uses a prebuilt database for near-instant searches.

Install and Update Database

sudo apt install mlocate # Debian/Ubuntu
sudo yum install mlocate # CentOS/RHEL
sudo updatedb # Update locate database

Usage

locate report.pdf

🔹 Case-insensitive search:

locate -i report.pdf

🔹 Limit the number of results:

locate -n 20 nginx

⚠️ Tip: Always run sudo updatedb to refresh the database before searching.

Searching Inside Files with grep

If you need to find text patterns inside files, use grep.

Basic Search

grep "error" /var/log/syslog
  • Finds the word “error” inside /var/log/syslog.

Recursive Search

grep -rnw '/etc' -e "root"
  • -r → Recursive search.
  • -n → Show line numbers.
  • -w → Match the whole word.

Search with Regex

grep -E "([0-9]{3})-[0-9]{3}-[0-9]{4}" *.txt
  • Finds phone numbers in .txt files.

Using “whereis” and “which”

a) Locate Executable Binaries

whereis python
  • Shows binary, man page, and source locations.

b) Find the Exact Executable Path

which python3
  • Outputs the full path to the executable.

 Searching with fd — A Modern Alternative to find 🚀 (Recommended)

fd is a faster, user-friendly alternative to find.

Install fd

sudo apt install fd-find # Ubuntu/Debian
sudo dnf install fd-find # Fedora

Examples

fd report.pdf
  • Default search path: current directory.

fd -e log error
  • Searches for files named error with .log extension.

fd -t d backup
  • Finds directories named “backup”.

 Searching with fzf (Interactive Fuzzy Finder) 🔎

fzf provides a real-time search interface.

Install fzf

sudo apt install fzf

Usage

fzf
  • Opens an interactive search UI.
  • Start typing to filter results instantly.

 Performance Tips for Large File Systems

  • Exclude unnecessary directories:

find / -path "/proc" -prune -o -name "*.conf" -print
  • Limit depth to speed up search:

find /var -maxdepth 2 -name "*.log"
  • Use locate instead of find when possible — it’s much faster.
  • Combine with grep for live content search:
grep -r "DATABASE_URL" $(locate .env)

 Security Considerations

  • Avoid using sudo unless required — can expose sensitive files.
  • Restrict grep and locate searches in multi-user environments.
  • Keep permissions correct on sensitive directories.

Conclusion

Linux provides powerful and flexible tools to locate files and content efficiently.

  • Use find for advanced conditional searches.
  • Use locate for instant lookups.
  • Use grep when searching inside files.
  • Try modern tools like fd and fzf for better usability and performance.

For large-scale environments, combining find, grep, and indexed searches like locate offers the best balance between speed and precision.