![]() |
| Example of find command. |
Let's begin...
Basic Syntax
The basic syntax of find command is:
find [starting_path] [expression] [action]
For example:
find /home/myportfolioreview13 -type f -name "*.raw"
The command searches recursively through the directory and its subdirectories.
| Component | Meaning |
|---|---|
find |
Execute the find utility |
/home/myportfolioreview13 |
Starting directory |
-type f |
Search only regular files |
-name "*.raw" |
Match filenames ending in .raw |
Search Path
| Path | Meaning |
|---|---|
/ |
Entire filesystem |
/home |
All users' home directories |
~ |
Your home directory |
. |
Current directory |
.. |
Parent directory |
/home/myportfolioreview13/Pictures |
Specific directory |
/mnt |
Mounted filesystems |
Those are examples of starting paths we can begin from any path, now see the examples:
find . -name "*.jpg"
find ~/Pictures -name "*.raw"
find /var/log -name "*.log"
find / -name "filename.txt"
Searching from root path "/" may produce permission errors. You can suppress those messages with "2>/dev/null" like example below:
find / -name "*.raw" 2>/dev/null
Adding "2>/dev/null" sends any errors to the void so they won't clutter your terminal.
File Types
| Option | Meaning |
|---|---|
-type f |
Regular file |
-type d |
Directory |
-type l |
Symbolic link |
-type b |
Block device |
-type c |
Character device |
-type p |
Named pipe (FIFO) |
-type s |
Unix socket |
Examples:
# Find directories
find /home -type d# Find symbolic links
find . -type l# Find regular files
find ~/Downloads -type f
Searching By File Name
| Predicate | Meaning |
|---|---|
-name |
Match filename, case-sensitive |
-iname |
Match filename, case-insensitive |
-path |
Match the entire path |
-ipath |
Case-insensitive path matching |
-regex |
Match path against a regular expression |
-iregex |
Case-insensitive regular expression |
Examples:
Note:find . -name "*.jpg"find . -path "*/Pictures/*.raw"
find . -iname "*.JPG"
find . -name "config*"
find . -name "*.py"
Quotation marks around "*.raw", they prevent shell from expanding the wildcard before find receives it.
Searching By File Size
Syntax:
find . -size [operator][number][unit]
Units:
| Unit | Meaning |
|---|---|
c |
Bytes |
w |
2-byte words |
k |
Kibibytes (1024 bytes) |
M |
Mebibytes |
G |
Gibibytes |
| No suffix | 512-byte blocks |
Operators:
| Operator | Meaning |
|---|---|
+ |
Greater than |
- |
Less than |
| No operator | Exact size range |
Examples:
# Larger than 50 MB
find . -type f -size +50M
# Smaller than 10 MB
find . -type f -size -10M
# Exactly 100 MB (size rounded to unit)
find . -type f -size 100M
# Larger than 50 bytes
find . -type f -size +50c
# Larger than 1 GB
find . -type f -size +1G
Searching By Modification Time
| Predicate | Meaning |
|---|---|
-mtime |
Modification time in 24-hour periods |
-mmin |
Modification time in minutes |
-atime |
Last access time in 24-hour periods |
-amin |
Last access time in minutes |
-ctime |
Last metadata change in 24-hour periods |
-cmin |
Last metadata change in minutes |
Examples:
# Modified within the last 24 hours
find . -type f -mtime -1
# Modified more than 7 days ago
find . -type f -mtime +7
# Modified within the last 30 minutes
find . -type f -mmin -30
# Accessed more than 30 days ago
find . -type f -atime +30
# Metadata changed within the last hour
find . -type f -cmin -60
Searching By Permissions
| Predicate | Meaning |
|---|---|
-perm 644 |
Exact permission match |
-perm -644 |
All specified permission bits must be present |
-perm /644 |
At least one specified permission bit must be present |
-readable |
Accessible for reading |
-writable |
Accessible for writing |
-executable |
Accessible for execution |
Examples:
# Find executable files
find . -type f -executable
# Find files with exactly 777 permissions
find . -type f -perm 777
# Find files with SUID permission
find /usr -type f -perm -4000
# Find files with SGID permission
find /usr -type f -perm -2000
# Find world-writable files
find . -type f -perm -0002
Combining Multiple Conditions
| Operator | Meaning |
|---|---|
-a |
AND |
-o |
OR |
! or -not |
NOT |
( ... ) |
Group conditions |
Examples:
Find regular files that satisfy both conditions "AND":
find . -type f -name "*.raw" -size +50M
Find JPG or PNG files "OR":
find . -type f \( -name "*.jpg" -o -name "*.png" \)
Find regular files that are not TXT files "NOT":
find . -type f ! -name "*.txt"
Find JPG or RAW files larger than 10 MB, escaped parentheses so shell passes them to find:
find . -type f \( -name "*.jpg" -o -name "*.raw" \) -size +10M
Executing Commands
| Component | Explanation |
|---|---|
-exec |
Execute an external command |
COMMAND |
Program to execute |
{} |
Replaced by the matched pathname |
\; |
Marks the end of the command |
Syntax:
find PATH CONDITIONS -exec COMMAND {} \;
Display filenames:
find . -type f -name "*.raw" -exec ls -lh {} \;Copy files:
find . -type f -name "*.raw" -exec cp -t ~/Pictures/ {} \;Move files:
find . -type f -name "*.raw" -exec mv -t ~/Pictures/ {} \;
Delete files:
find . -type f -name "*.tmp" -exec rm -- {} \;Change permissions:
find . -type f -name "*.sh" -exec chmod 755 {} \;Execute commands in batches using +:
find . -type f -name "*.raw" -exec ls -lh {} +
Excluding Directories
Suppose if I want to search .py file but skip .git folder:
find . -path "./.git" -prune -o -type f -name "*.py" -print
Additional Options
| Action | Purpose |
|---|---|
-print |
Print matched paths |
-print0 |
Print paths separated by null characters |
-delete |
Delete matching files |
-exec |
Execute a command |
-execdir |
Execute a command from the matched file's directory |
-ok |
Ask for confirmation before executing |
-okdir |
Confirmation with execution from the matched directory |
-prune |
Prevent descending into a directory |
-quit |
Stop searching immediately |
More Practical Find Commands
| Goal | Command |
|---|---|
| Find large files | find / -type f -size +500M 2>/dev/null |
| Find Python files | find . -type f -name "*.py" |
| Find hidden files | find . -type f -name ".*" |
| Find empty files | find . -type f -empty |
| Find empty directories | find . -type d -empty |
| Find recently modified files | find . -type f -mmin -60 |
| Find executable files | find . -type f -executable |
| Find files by owner | find /home -user myportfolioreview13 |
| Find files by group | find /home -group myportfolioreview13 |
| Find multiple extensions | find . -type f \( -name "*.jpg" -o -name "*.png" \) |
| Count matching files | find . -type f -name "*.py" \| wc -l |
Well that's all I can share in this post, and see you again in the next post! Bye..
