Mastering Linux ‘find’: Essential Commands for File Search and Management
This guide explains how to use the Linux find command to locate files and directories by name, type, permissions, owners, timestamps, size, and more, while showing practical examples of depth control, exclusion, combined criteria, and advanced actions such as listing and deleting results.
Introduction
The find utility is a powerful built‑in tool on virtually every Linux distribution, allowing users to locate files and directories based on a wide range of criteria such as name, permissions, timestamps, size, and ownership. Mastering its options makes command‑line work far more efficient.
Basic Usage
The basic syntax is: find [path] [option] [expression] Running find without arguments lists all files and sub‑directories under the current directory.
Search by Name and Pattern
To list everything in the current directory and its sub‑folders: find . To search a specific directory: find ./test To find a file with an exact name: find ./test -name "abc.txt" Wildcards are supported:
find ./test -name "*.php"Depth Control
Limit recursion with -maxdepth (maximum depth) or -mindepth (minimum depth): find ./test -maxdepth 2 -name "*.php" Setting -maxdepth 1 restricts the search to the top‑level directory only.
Negation and Exclusion
Find files that do **not** match a pattern using -not or !: find ./test -not -name "*.php" or
find ./test ! -name "*.php"Combining Multiple Conditions
Use logical AND (default) or OR ( -o) to combine criteria. Example of AND: find ./test -name 'abc*' ! -name '*.php' Example of OR:
find -name '*.php' -o -name '*.txt'Permission and Attribute Search
Search by numeric permission: find . -type f -perm 0664 Search for files lacking a permission (e.g., not 0777): find . -type f ! -perm 0777 Search for SGID/SUID or sticky‑bit files using symbolic mode syntax:
find / -maxdepth 2 -perm /u=s 2>/dev/nullOwner and Group Search
Find files owned by a specific user: find . -user bob Find files belonging to a particular group:
find /var/www -group developerTime‑Based Search
Files modified N days ago: find / -mtime 50 Files accessed within the last N days: find / -atime -50 Files changed within a range: find / -mtime +50 -mtime -100 Files changed in the last N minutes:
find /home/bob -cmin -60Size‑Based Search
Find files of a specific size (e.g., 50 MiB): find / -size 50M Find files within a size range: find / -size +50M -size -100M Find the largest or smallest files by combining find with ls, sort, and head:
find . -type f -exec ls -s {} \; | sort -n -r | head -5Empty Files and Directories
Empty files: find /tmp -type f -empty Empty directories:
find ~/ -type d -emptyAdvanced Operations
Execute arbitrary commands on matched files. Example to list detailed info: find . -exec ls -ld {} \; Example to delete all .txt files in /tmp: find /tmp -type f -name "*.txt" -exec rm -f {} \; Delete directories by changing -type f to -type d and adding -r to rm:
find /tmp -type d -name "dirToRemove" -exec rm -r -f {} \;Conclusion
By mastering the various options of find, users can perform precise file searches, filter results by numerous attributes, and chain further commands for processing or cleanup, making it an indispensable tool for system administration and scripting.
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
