Kiet Nguyen logo
NotesNotesResumeResume
© 2026 Kiet Nguyen
← All categories

05

Find & Locate

  • Find large or recent files on full disk
  • Locate binary/config by name
  • Explain find vs locate
findlocatewhichrealpath

Must-know cold

  • find /path -name '*.log' · find /path -type f -mtime +7
  • find /path -type f -size +100M
  • find ... -print0 | xargs -0 -r ... (spaces-safe; GNU -r skips empty)
  • which cmd / command -v cmd · type cmd
  • locate is a DB cache (fast, may be stale); find walks the live tree

Commands

find

Definition: Walk a filesystem tree and select paths by name, type, time, size, and more.

Option / testArgumentMeaningExample
(none)PATHStart path (always pass PATH)find /var/log
-namePATTERNBasename match (shell globs; quote it)find . -name '*.conf'
-inamePATTERNCase-insensitive -namefind . -iname 'readme*'
-pathPATTERNFull path matchfind / -path '*/nginx/*.conf'
-ipathPATTERNCase-insensitive path
-regexREGEXWhole path vs regex (GNU default = emacs, not ERE; -regextype)find . -regex '.*\.py'
-typef / d / l / b / c / p / sFile, dir, symlink, block, char, pipe, socketfind . -type f
-size±N[cwbkMG]Size in rounded-up units (+100M = more than 100 MiB). -1k is only empty files, not “under 1k” — use -size -1024cfind / -type f -size +100M
-mtime±NAge in whole 24h periods, fraction dropped. +7 = at least 8 days ago; -1 = last 24hfind . -mtime +7
-mmin±NModified ±N minutesfind . -mmin -30
-atime / -amin±NAccess time (often stale: relatime / noatime mounts)find . -atime +30
-ctime / -cmin±NMetadata change time
-newermtDATEModified more recently than DATEfind . -newermt '2026-08-01'
-userUSEROwned by USERfind /home -user alice
-groupGROUPOwned by GROUPfind /shared -group deploy
-uid / -gidNNumeric idsfind / -uid 1000
-permMODEExact modefind . -perm 644
-perm-MODEAll listed bits set. -111 = u+g+o execute, not “any +x”find . -perm -0002 (world-writable)
-perm/MODEAny listed bit (GNU). Any +x is /111find . -perm /u=s setuid hunt
-empty—Empty files/dirsfind . -type d -empty
-maxdepthNLimit directory depthfind /var -maxdepth 2 -type d
-mindepthNStart at depth Nfind . -mindepth 1
-xdev—Don’t cross mount pointsfind / -xdev -name ...
-mount—Same as -xdev (GNU)
-L—Follow symlinksfind -L . -type f
-P—Never follow (default)
-writable—Writable by current user (GNU)
-executable—Executable/searchable (GNU)
-readable—Readable (GNU)
-inumNInode numberHardlink groups
-linksNLink count
-not / !testNegatefind . ! -name '*.o'
-a / -o—And (default) / Or. -o binds looser than -a — group with \(\)find . \( -name a -o -name b \) -type f
\( \)—Group testsfind . \( -name '*.c' -o -name '*.h' \)
-print—Print path (default action)
-print0—NUL-delimited printSafe with xargs -0
-ls—ls -dils style listingfind . -name '*.log' -ls
-printfFORMATCustom print (GNU)find . -printf '%p %s\n'
-execCMD {} \;Run CMD per file; {} = pathfind . -name '*.tmp' -exec rm {} \;
-execCMD {} +Batch many paths into one CMDFaster than \;
-execdirCMD {} \;Run in file’s directory
-okCMD {} \;Like -exec but prompt eachSafer delete
-delete—Delete match (GNU). Implies -depth; cannot usefully pair with -prunefind . -name '*.tmp' -delete
-quit—Stop after first match (GNU)find / -name nginx -quit
-prune—Don’t descend into matchSkip dirs

Size units (-size)

SuffixMeaning
cbytes
w2-byte words
b512-byte blocks (default)
kKiB
MMiB
GGiB

Flag combos

ComboMeaningExample
find /var -xdev -type f -size +100MLarge files on one FSDisk full
find /var/log -type f -mtime +14Logs older than 14 daysCleanup candidates
find . -name '*.log' -print0 | xargs -0 -r grep -l ERRORGrep safely (-r = don’t run grep on empty GNU xargs)Spaces in names
find /home -type f -perm -0002World-writable filesSecurity audit
find / -name 'nginx.conf' 2>/dev/nullHide permission noise
find /path -mindepth 1 -maxdepth 1 -type dImmediate subdirs only (-maxdepth 1 includes /path)
find . -type f -exec grep -l PAT {} +Content searchvs recursive grep

-exec punctuation:

  • {} is the path placeholder.
  • \; ends “one invocation per file”.
  • + ends “batch paths” (like xargs).

locate

Definition: Fast name search via a prebuilt database. updatedb rebuilds it.

CommandArgumentMeaningExample
locatePATTERNSearch prebuilt DB for pathnameslocate nginx.conf
locate -iPATTERNCase-insensitivelocate -i readme
-n / -lN PATTERNLimit results (mlocate -n, plocate -l)locate -n 20 nginx.conf
locate -rREGEXRegex matchlocate -r '/nginx\.conf$'
locate -cPATTERNCount matcheslocate -c '*.log'
locate -ePATTERNOnly existing files (recheck)locate -e bin/ls
updatedb—Rebuild database (usually root/cron)sudo updatedb
updatedb --prunepathspathsSkip paths when buildingConfig in /etc/updatedb.conf

Flag combos

ComboMeaningExample
locate bin/nginxFast path guessThen verify with ls
sudo updatedb && locate fooFresh DB then searchAfter new installs

Caveats: DB often ignores some paths; results can be stale; may not be installed (package mlocate / plocate).

which

Definition: Locate how the shell resolves a command name (PATH, alias, binary). Pair with type, command -v, and whereis.

CommandArgumentMeaningExample
whichCMDFirst PATH match (external)which python3
which -aCMDAll PATH matcheswhich -a java
typeCMDShell resolution: alias/builtin/filetype ls
type -aCMDAll interpretationstype -a python
command -vCMDPortable: how the shell would invoke it (path, alias, or function)command -v nginx
command -VCMDVerbose (bash)command -V cd
whereisCMDBinary, source, man guesseswhereis ls
whereis -bCMDBinaries onlywhereis -b ssh

Flag combos

ComboMeaningExample
type cmd; command -v cmdAlias vs real binaryDebug “wrong version”
which -a javaMultiple JDKs on PATHToolchain hosts

realpath

Definition: Resolve symlinks and print the canonical absolute path. readlink shows one hop.

CommandArgumentMeaningExample
realpathPATHAbsolute canonical pathrealpath ./link
readlinkLINKSymlink target (one level)readlink /usr/bin/python
readlink -fPATHCanonicalize (GNU)readlink -f ./x

Common recipes

GoalCommand
Config by namefind /etc -name '*.conf' 2>/dev/null | head
Large filessudo find / -xdev -type f -size +200M 2>/dev/null
Recent deploysfind /opt/app -type f -mtime -1
Empty dirsfind /tmp -type d -empty
Delete old tempsfind /tmp -type f -mtime +7 -print then review → -delete
Safe xargsfind . -name '*.log' -print0 | xargs -0 -r ls -lh
Fast name searchlocate nginx.conf
Where is binarycommand -v nginx; type nginx
World-writablefind /opt -type f -perm -0002 2>/dev/null

Pitfalls

  • Always quote -name '*.log' or the shell expands the glob too early.
  • find / is slow and noisy — prefer a start path + -xdev. 2>/dev/null hides permission errors you may need.
  • -type f does not include symlinks to files (-P default). -L follows links and can loop; -type l is then almost never true.
  • -perm -111 is every execute bit, not “any +x” (/111).
  • -size rounds up to the unit. -size -1M / -1k match only empty files. Prefer -size -1048576c when you mean bytes.
  • -mtime +7 is more than 7 whole days (fraction dropped) ≈ ≥ 8×24h. Use -mmin or -newermt for precision. -atime lies on noatime/relatime disks.
  • -maxdepth 1 includes the start path. Immediate children: -mindepth 1 -maxdepth 1.
  • -o is looser than implicit -a. Write \( -name a -o -name b \) -type f.
  • -delete implies -depth (so -prune does nothing useful) and deletes everything the expression matches — put it last; preview with -print.
  • GNU xargs without -r still runs the command once on empty input (grep then reads stdin). Use xargs -0 -r or -exec … {} +.
  • locate ≠ live truth; new files wait for updatedb. Flags differ (mlocate -n vs plocate -l).
  • find + -exec \; is slow; prefer {} +.

For more details, try man <command> in your terminal.

Previous04 Permissions & OwnershipNext06 Text Processing