Fundamentals 8 min read

Master the Unix find Command: Powerful File Search Techniques

This guide explains how to use the Unix find command to efficiently locate and manipulate files and directories based on name, type, size, timestamps, and more, providing syntax, common options, actions, and practical examples for everyday shell tasks.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master the Unix find Command: Powerful File Search Techniques

The find command is a powerful tool in Unix and Unix‑like systems (such as Linux) for searching files and directories within a directory tree. It provides many options for complex searches, allowing filtering by name, type, modification time, size, and more.

Basic Syntax

find [path] [options] [test] [action]

Path : The directory or directories to search. Use . for the current directory.

Options : Control the behavior of find, such as ignoring errors or showing help.

Test conditions : Criteria that files must match, e.g., name, type, modification time.

Actions : What to do with matched files, such as printing the path or deleting the file.

Common Options

-name : Match files by name.

Example: find /path -name "*.txt" finds all .txt files under /path.

-iname : Case‑insensitive version of -name.

Example: find /path -iname "*.jpg" finds all .jpg files regardless of case.

-type : Match by file type. f: regular file, d: directory, l: symbolic link.

Example: find /path -type d lists all directories under /path.

-size : Match by file size.

Example: find /path -size +1M finds files larger than 1 MB.

Units: c (bytes), k (KB), M (MB), etc.

-mtime : Match by modification time (in days). -mtime n: exactly n days ago. -mtime +n: more than n days ago. -mtime -n: within the last n days.

Example: find /path -mtime -7 finds files modified in the past week.

-ctime : Match by inode change time.

Example: find /path -ctime +30 finds files whose metadata changed over 30 days ago.

-atime : Match by last access time.

Example: find /path -atime -1 finds files accessed in the last 24 hours.

-mmin : Match by modification time in minutes.

Example: find /path -mmin +60 finds files not modified in the last hour.

-cmin : Match by inode change time in minutes.

Example: find /path -cmin -30 finds files whose metadata changed within the last 30 minutes.

-amin : Match by access time in minutes.

Example: find /path -amin +10 finds files not accessed in the last 10 minutes.

Actions

-print : Default action; prints the matched file path.

Example: find /path -name "*.log" -print -exec : Executes a command on each matched file.

Example: find /path -name "*.tmp" -exec rm -f {} \; deletes all .tmp files.

-delete : Deletes matched files (use with caution).

Example: find /path -name "*.bak" -delete -print0 : Prints paths separated by a null character, useful for filenames with spaces.

Example: find /path -type f -print0 | xargs -0 rm -prune : Excludes a directory and its sub‑directories from the search.

Example:

find /path -type d -name "ignore" -prune -o -type f -print

Examples

Find all .log files in the current directory: find . -name "*.log" Find files larger than 100 MB in /var/log: find /var/log -size +100M Find files modified more than 7 days ago and delete them: find /path -mtime +7 -exec rm -f {} \; Print paths of files accessed in the last 30 minutes: find /path -amin -30 -print Find and delete all .tmp files (use carefully):

find /path -name "*.tmp" -delete

Summary

The find command is a versatile utility for locating and operating on files within a directory tree. By mastering its syntax, common options, and actions, you can efficiently manage and process files on Unix‑like systems.

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.

Shellcommand-lineUnixFile Searchfind command
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.