Mastering Linux ‘find’: Powerful File Search Techniques and Examples
This guide explains the essential syntax of the Linux find command and demonstrates how to locate files by name, pattern, path, type, depth, size, timestamps, permissions, ownership, and how to execute actions on matched files, with clear examples for each use case.
Basic Syntax
The find utility searches the file hierarchy based on specified criteria. Its general form is:
find [options] [path] [expression]Search by File Name
List all files in the current directory and sub‑directories: find . Find a file named 11.png: find . -name "11.png" Find all .jpg files: find . -name "*.jpg" Find both .jpg and .png files: find . -name "*.jpg" -o -name "*.png" Find files that are not .png:
find . ! -name "*.png"Search Using Regular Expressions
Locate files whose names consist solely of digits and end with .png:
find . -regex "\./*[0-9]+\.png"Search by Path
Find files or directories whose path contains the string wysiwyg:
find . -path "*wysiwyg*"Search by File Type
Use -type to filter by file type. Common type codes are:
f – regular file
l – symbolic link
d – directory
c – character device
b – block device
s – socket
p – FIFO (named pipe)
Example – find regular files whose path contains wysiwyg:
find . -type f -path "*wysiwyg*"Limit Search Depth
Find .png files only in the current directory (no recursion): find . -maxdepth 1 -name "*.png" Find .png files exactly two levels deep:
find . -mindepth 2 -maxdepth 2 -name "*.png"Search by File Size
The -size option accepts units: b (512‑byte blocks), c (bytes), w (2‑byte words), k (kilobytes), M (megabytes), G (gigabytes).
Example – find files larger than 100 MiB:
find . -type f -size +100MSearch by Access/Modification/Change Time
Time‑related tests: -atime / -amin: last access (days/minutes) -mtime / -mmin: last modification (days/minutes) -ctime / -cmin: last status change (days/minutes)
Find files modified within the last day: find . -type f -mtime -1 Find files accessed in the last week: find . -type f -atime -7 Move log files older than one week to /tmp/old_logs:
find . -type f -mtime +7 -name "*.log" -exec mv {} /tmp/old_logs \;Search by Permissions
Find files with exact permission 777: find . -type f -perm 777 Find PHP files whose permissions are not 644:
find . -type f -name "*.php" ! -perm 644Search by Owner or Group
Find files owned by user root: find . -type f -user root Find files belonging to group root:
find . -type f -group rootExecute Commands on Found Files
Use -ok for interactive confirmation, or -exec for direct execution.
Interactive deletion of all .js files (asks before each removal): find . -type f -name "*.js" -ok rm {} \; Immediate deletion without confirmation:
find . -type f -name "*.js" -exec rm {} \;Find Empty Files
Example to create empty files and then locate them:
touch 1.txt 2.txt 3.txt
find . -emptySigned-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.
