Master the Linux ‘find’ Command: Powerful File Search Techniques
Learn how to use the versatile Unix ‘find’ command to search and manipulate files by name, type, size, timestamps, and more, with detailed syntax, common options, and practical examples for printing, deleting, and executing actions on matched files.
The
findcommand is a powerful tool in Unix and Unix‑like systems (e.g., Linux) for searching files and directories within a directory tree. It offers many options to perform complex searches based on name, type, time, size, etc.
Basic Syntax
<code>find [path] [options] [test] [action]</code>path : directories to search; can be one or more paths or
.for the current directory.
options : control
findbehavior such as ignoring errors or showing help.
test : criteria to match files, e.g., name, type, modification time.
action : what to do with matched files, e.g., print path, delete.
Common Options
-name: match files by name. Example:
find /path -name "*.txt" -iname: case‑insensitive name match. Example:
find /path -iname "*.jpg" -type: match by file type.
ffor regular file,
dfor directory,
lfor symlink. Example:
find /path -type d -size: match by file size. Units:
c(bytes),
k(KB),
M(MB). Example:
find /path -size +1M -mtime: match by modification time in days.
-mtime -7finds files modified within the last 7 days.
-ctime: match by change time of metadata. Example:
find /path -ctime +30 -atime: match by last access time. Example:
find /path -atime -1 -mmin: match by modification time in minutes. Example:
find /path -mmin +60 -cmin: match by metadata change time in minutes. Example:
find /path -cmin -30 -amin: match by access time in minutes. Example:
find /path -amin +10Actions
-print: default action, prints the path of matched files.
-exec: execute a command on each matched file. Example:
find /path -name "*.tmp" -exec rm -f {} \; -delete: delete matched files (use with caution).
-print0: prints paths separated by a null character, useful with
xargs -0.
-prune: exclude specified directories from the search.
Examples
Find all
.logfiles in the current directory:
<code>find . -name "*.log"</code>Find files larger than 100 MB in
/var/log:
<code>find /var/log -size +100M</code>Delete files modified more than 7 days ago:
<code>find /path -mtime +7 -exec rm -f {} \;</code>Print files accessed in the last 30 minutes:
<code>find /path -amin -30 -print</code>Find and delete all
.tmpfiles (use carefully):
<code>find /path -name "*.tmp" -delete</code>Summary
The
findcommand is a versatile utility for locating and operating on files in a directory tree; mastering its options and actions enables efficient file management on Unix/Linux systems.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
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.