Master Linux File Search: Powerful find Command Tricks and Examples
This guide walks through the versatile Linux find command, showing how to locate files and directories by name, type, size, timestamps, permissions, and more, while demonstrating practical examples such as pattern matching, case‑insensitive searches, depth limiting, and executing actions on results.
Viewing the Manual
Before using find, you can read its full documentation with man find to understand all available options.
Viewing Directory Structure
Use the tree command to display the current directory hierarchy, which helps you see where files are located before searching.
$ tree
.
├── demoSub
│ └── testhappy.txt
├── testgood.txt
└── testnice.txtFinding All Files
To list every file and directory under the current path:
$ find .Finding by Filename
Specify a filename directly to check its existence in the current directory:
$ find testnice.txtFinding by Type
Use -type d to list only directories or -type f for regular files:
$ find . -type d
$ find . -type fFinding by Name Pattern
Combine -name with a pattern. The pattern is case‑sensitive:
$ find . -type f -name "testhappy.txt"
$ find . -type f -name "test*"
$ find . -type f -name "*.txt"For case‑insensitive matching, replace -name with -iname:
$ find . -type f -iname "test*"Finding by Modification Time
Use -mmin for minute‑based modification time. -mmin -10 finds files changed within the last 10 minutes; -mmin +10 finds files changed more than 10 minutes ago. A range can be expressed with two -mmin options: $ find . -type f -mmin +5 -mmin -10 For day‑based searches, use -mtime (e.g., -mtime -2 for files modified in the last two days).
Finding by Access and Change Time
Other time attributes include -atime / -amin (last access) and -ctime / -cmin (last status change).
Finding by Size
The -size option filters files by size. Prefix + means greater than, - means less than. Units: c (bytes), k (KB), M (MB), G (GB).
$ find . -size +2M # files larger than 2 MB
$ find . -size -2M # files smaller than 2 MBFinding Empty Files
Use -empty to locate files or directories with no content:
$ find . -emptyFinding by Permissions
Search for files with specific permission bits using -perm:
$ find . -perm 664Executing Actions on Found Items
The -exec option runs a command on each match. The placeholder {} represents the current file, and the command must end with \; or + (the latter groups multiple files).
# Change permissions to 777 for all files with 664
find . -perm 664 -exec chmod 777 {} +
# Change owner and group
find . -exec chown qin:tester {} +
# Delete all .txt files (use a dry‑run first)
find . -type f -name "*.txt" -exec rm {} +Limiting Search Depth
Restrict how deep find descends with -maxdepth. For example, -maxdepth 1 searches only the current directory:
$ find . -maxdepth 1 -name "*.txt" -exec rm {} +Summary
The find command is a powerful tool for locating files and directories based on a wide range of criteria—name, type, size, timestamps, permissions, and more. Combined with -exec and depth‑limiting options, it enables complex batch operations directly from the shell, turning simple command‑line usage into an efficient workflow.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
