Master Linux File Search: which, whereis, locate & find Commands Explained
This guide walks through essential Linux commands—which, whereis, locate, and find—detailing how to discover executable paths, locate specific files, query the file database, and perform advanced searches with options for name, size, type, time, permissions, and execution of actions.
Finding the Absolute Path of a Command
The which command displays the absolute path of a given executable, using the PATH environment variable.
[root@localhost ~]# which bash
/usr/bin/bash
[root@localhost ~]# which ls
alias ls='ls --color=auto'
/usr/bin/lsLocating Specific Files with whereis
whereislocates the binary, source, and manual pages of a program name.
[root@localhost ~]# whereis --help
# syntax: whereis [options] filename
-b # search only binaries
-m # search only man pages
-s # search only source filesExample:
[root@localhost ~]# whereis -b ifconfig
ifconfig: /usr/sbin/ifconfig
[root@localhost ~]# whereis -m ifconfig
ifconfig: /usr/share/man/man8/ifconfig.8.gzUsing locate and Updating Its Database
The locate command searches a pre‑built database ( /var/lib/mlocatedb) that is updated daily; use updatedb to refresh it manually.
[root@localhost ~]# yum install -y mlocate
[root@localhost ~]# updatedb
[root@localhost ~]# locate /etc/passwd
/etc/passwd
/etc/passwd-Comprehensive Searches with find
The find command offers extensive options for locating files by name, size, type, permissions, timestamps, inode, and more.
# Basic syntax: find [path] [expression]
-name "*.log" # by filename pattern
-size -10k # files smaller than 10 KB
-size +1M # files larger than 1 MB
-type f # regular files
-type d # directories
-type l # symbolic links
-perm 0644 # exact permission bits
-user wang # owned by user "wang"
-ctime +7 # changed more than 7 days ago
-mmin -120 # modified within last 120 minutes
-inum 1024 -exec rm {} \; # delete by inode number
-a -o # logical AND / OR
-exec ls -l {} \; # execute a command on each matchExamples:
# Find all .log files under /var
find /var/ -name "*.log"
# Find .txt files named 1‑3 in /root
find /root/ -name "[1-3].txt"
# Find files in /etc smaller than 10 KB
find /etc/ -size -10k
# Find files larger than 1 MB in /etc
find /etc/ -size +1M
# Find files owned by user "wang" in /root
find /root/ -user wang
# Find files with permission 0644 in /boot
find /boot/ -perm 0644
# Find binary files in /usr/bin
find /usr/bin/ -type f
# Find files modified in the last 120 minutes in /etc
find /etc/ -mmin -120
# Find files changed more than 7 days ago in /etc
find /etc/ -ctime +7
# Delete files with inode 1024
find ./ -inum 1024 -exec rm {} \;
# Find files between 1 KB and 10 KB in /etc
find /etc/ -size +1k -a -size -10k
# List .log files in /var/log and show details
find /var/log/ -name "*.log" -exec ls -l {} \;
# List files starting with "init" in /etc, exclude directories
find /etc/ -name "init*" -a -type f -exec ls -l {} \;
# Delete yum.log in /tmp
find /tmp/ -name yum.log -exec rm {} \;
# Delete all files owned by user lyshark (prompt before each)
find / -user lyshark -ok rm -r {} \;Logical Operators
Use -a (AND) and -o (OR) to combine multiple criteria.
# Example: files larger than 1 KB AND smaller than 10 KB
find /etc/ -size +1k -a -size -10kThese commands together provide a powerful toolkit for locating and managing files on Linux systems.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
