Operations 12 min read

Master Linux File Search: Powerful find Command Techniques

This comprehensive guide explains how to use the Linux find command for locating directories, hidden files, size‑specific files, recent modifications, permission‑set files, and more, while also covering performance tuning, error handling, and sorting results.

ITPUB
ITPUB
ITPUB
Master Linux File Search: Powerful find Command Techniques

The Linux find command is an unmatched tool for searching files and directories, offering a simple syntax with many options to fine‑tune searches.

Find Directories

Search only directories with the -type d option:

$ find /path/to/search -type d -name "name-of-dir"

Find Hidden Files

Hidden files start with a dot; use a pattern that matches a leading period:

$ find /path/to/search -name ".*"

Find Files by Size

Use -size to locate files of exact size, larger than, or smaller than a given threshold:

Greater than 10 MB: $ find /path/to/search -size +10M Less than 10 MB: $ find /path/to/search -size -10M Exactly 10 MB: $ find /path/to/search -size 10M Between 100 MB and 1 GB:

$ find /path/to/search -size +100M -size -1G

Search From a File List

Combine find with grep -f to filter results using patterns stored in a file: $ find /path/to/search | grep -f filelist.txt Use grep -v -f to exclude matches:

$ find /path/to/search | grep -v -f filelist.txt

Limit Recursion Depth

Control how deep find recurses with -maxdepth:

Only current directory: $ find . -maxdepth 0 -name "myfile.txt" Current directory and one subdirectory:

$ find . -maxdepth 1 -name "myfile.txt"

Find Empty Files or Directories

Use -empty with -type f or -type d:

$ find /path/to/search -type f -empty
$ find /path/to/search -type d -empty

Combine with -delete to remove them automatically:

$ find /path/to/search -type f -empty -delete

Find Files with setuid/setgid Permissions

Search for files with specific permission bits using -perm (or -user for a specific owner):

$ find /path/to/search -user root -perm /4000
$ find /path/to/search -perm /4000
$ find /path/to/search -perm /2000
$ find /path/to/search -perm /6000

Search by Modification Time

Use -mtime to find files modified within or beyond a number of days:

Modified in the last 30 days: $ find /path/to/search -type f -mtime -30 Modified more than 30 days ago: $ find /path/to/search -type f -mtime +30 Exactly 30 days ago: $ find /path/to/search -type f -mtime 30 Show detailed info with -exec ls -l {}:

$ find /path/to/search -type f -mtime -30 -exec ls -l {} \;

Sort Results by Time

Print timestamps with -printf "%T+\t%p\n" and pipe to sort (add -r for newest first):

$ find /path/to/search -printf "%T+\t%p
" | sort
$ find /path/to/search -printf "%T+\t%p
" | sort -r

Locate vs. Find

The locate command searches a pre‑built database (updated with updatedb), making it faster but less flexible than find. Use locate for quick searches on the whole disk, but prefer find for precise, option‑rich queries.

Control CPU and I/O Load

When searching large directory trees, lower the command’s priority with nice, ionice, or both:

$ ionice -c3 -n7 find /path/to/search -name "myfile.txt"
$ nice -n 19 find /path/to/search -name "myfile.txt"
$ nice -n 19 ionice -c2 -n7 find /path/to/search -name "myfile.txt"

Monitor resource usage with top if needed.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SysadminFile Searchcommand-line
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.