Common Linux Commands for Finding Files: which, whereis, type, locate, and find
This article introduces several essential Linux commands—which, whereis, type, locate, and find—explaining how they search for binaries, manuals, source files, or any files on disk, along with practical examples and usage tips for effective file location.
Linux provides a set of useful commands for locating binaries, manuals, source files, or any files on the filesystem. This guide explains how to use these commands with examples.
which
The which command searches the directories listed in the PATH environment variable for the location of a given executable.
which -a which # show all occurrences of the command
/usr/bin/which
/bin/whichTypical PATH contents (may vary by system) can be displayed with:
echo $PATH
/home/hyb/bin:/home/hyb/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/binIf a command is installed but not found, checking PATH ensures its directory is included.
whereis
The whereis command locates a program's binary, source, and manual pages.
whereis ls # show binary, source, and man page if available
ls: /bin/ls /usr/share/man/man1/ls.1.gz
whereis -m ls # show only manual page
ls: /usr/share/man/man1/ls.1.gz
whereis -b ls # show only binary
ls: /bin/ls
whereis stdio.h # locate header file and its man page
stdio: /usr/include/stdio.h /usr/share/man/man3/stdio.3.gzNote: whereis cannot locate built‑in shell commands.
type
The type builtin reports how the shell would interpret a given name.
alias: # alias
keyword: # keyword
builtin: # built‑in command
file: # external commandCommon options:
-t # output only the type name (e.g., file)
-p # if external, show its path
-a # show all information for external commandsExamples:
type ls # ls is aliased to `ls --color=auto`
type cd # cd is a shell builtin
type find # find is /usr/bin/find
type function # function is a shell keyword
type -a which # show all paths for which
which is /usr/bin/which
which is /bin/whichlocate
locate searches a pre‑built database for any file name, offering very fast results but lacking real‑time accuracy.
-e # show only existing files
-q # quiet mode, suppress errors
-n # limit output to n entries
-r # use regular expression
-i # case‑insensitive search
-c # print count of matchesExample usage:
locate locate.txt # find locate.txt
/home/hyb/workspaces/shell/locate/locate.txt
locate -e locate.txt # show only existing files (none if deleted)
locate -c locate.log # count matches (outputs 1)
locate -i locate.zip # case‑insensitive search
/home/hyb/workspaces/shell/locate/LOCATE.zip
locate -r /locate\.log$ # regex: files ending with /locate.logBecause the database is updated periodically (usually daily), recently added or removed files may not be reflected until updatedb is run (often requires root).
find
The find command walks the filesystem tree, providing real‑time, highly configurable file searches.
Search by name
find ./ -name "sort*"
./sort4.txt
./sort2.txt
./sort3.txt
./sort.txt
find ./ -iname "SORT.txt" # case‑insensitive
./sort.txtSearch by permissions
find ./ -perm 777
./test
./sort.txtSearch by file type
find ./ -type l
./testCommon type letters: f (regular file), d (directory), b (block device), c (character device), l (symlink), s (socket), p (FIFO).
Search by size
find ./ -size 1k # files smaller than 1 KB
./test
./sort4.txt
...
find ./ -size +1M # files larger than 1 MB
./test.zipSize units: k (kilobytes), M (megabytes), G (gigabytes), c (bytes), b (blocks, 512 B), w (two‑byte words).
Search by ownership
find ./ -user root # files owned by root
find ./ -nouser # files with no existing ownerSearch by time
find ./ -mtime 3 # modified more than 3 days ago
find ./ -mtime -3 # modified within the last 3 days
find ./ -mtime 0 # modified today
find ./ -newer sort.txt # newer than sort.txt
find ./ -anewer sort.txt # accessed more recently than sort.txt
find ./ -amin 5 # accessed within the last 5 minutesKey timestamps: atime (last access), mtime (last modification), ctime (last status change).
Summary
The which command finds the location of executables in PATH .
whereis locates binaries, manuals, and source files but not built‑ins.
type shows how the shell interprets a name (alias, builtin, etc.).
locate offers fast, database‑based searches with limited real‑time accuracy.
find provides accurate, real‑time, and highly flexible file searches.
Advanced find usage will be covered in future articles.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.