Find Command Guide - ryzendew/Linux-Tips-and-Tricks GitHub Wiki
Find Command Troubleshooting for Beginners
Table of Contents
- Find Files Modified in Last N Days
- Find Files Accessed in Last N Days
- Find Files Changed in Last N Minutes
- Print Found Files (Default)
- List Files with Details
- Execute Command on Found Files
- Delete Found Files (DANGEROUS!)
- Scenario 1: Find Large Files
- Scenario 2: Find Recently Modified Files
- Scenario 3: Find Files by Extension
- Scenario 4: Find Empty Files/Directories
- Scenario 5: Find Files by Owner
- Scenario 6: Find and Count Files
- Scenario 7: Find Files and Exclude Directories
- Basic Commands
- Finding by Name
- Finding by Type
- Finding by Size
- Finding by Time
- Finding by Permissions
- Combining Criteria
- Actions
:pencil: What is find?
findsearches for files and directories in the filesystem- One of the most powerful and commonly used Linux commands
- Can search by name, size, type, permissions, date, and more
- Essential for locating files and troubleshooting
What find can do:
- Find files by name (exact or pattern)
- Search by file size
- Find files by type (file, directory, etc.)
- Search by modification/access time
- Find files by permissions
- Execute commands on found files
- Combine multiple search criteria
:zap: Basic Commands
Basic Find (Current Directory)
find .
What this does:
- Searches current directory (
.) and all subdirectories - Shows all files and directories
- Very long output (usually combined with other options)
Find in Specific Directory
find /path/to/directory
What this does:
- Searches specified directory and all subdirectories
- Recursive search (includes all subdirectories)
Example:
find /home
Limit Search Depth
find /path -maxdepth 2
What this does:
- Limits search to specified depth levels
-maxdepth 1= only the specified directory-maxdepth 2= directory and one level of subdirectories
Example:
find /home -maxdepth 1
Shows only items directly in /home, not subdirectories.
:mag: Finding by Name
Find Files by Exact Name
find /path -name "filename"
What this does:
- Finds files/directories with exact name
- Case-sensitive
- Use quotes around the name
Example:
find . -name "Home.md"
Find Files by Pattern (Wildcards)
find /path -name "*.txt"
What this does:
- Finds files matching pattern
*matches any characters?matches single character
Common patterns:
*.txt- All .txt filesfile*- Files starting with "file"*.log- All log filestest?.txt- test1.txt, test2.txt, etc.
Example:
find . -name "*.md"
Finds all Markdown files.
Case-Insensitive Name Search
find /path -iname "filename"
What this does:
- Case-insensitive name search
-inameinstead of-name- Useful when you're not sure of exact case
Example:
find . -iname "home.md"
Finds "Home.md", "home.md", "HOME.md", etc.
:file_folder: Finding by Type
Find Only Files
find /path -type f
What this does:
- Finds only files (not directories)
-type f= files only
Find Only Directories
find /path -type d
What this does:
- Finds only directories
-type d= directories only
Example:
find /home -maxdepth 1 -type d
Other File Types
find /path -type l # Symbolic links
find /path -type s # Sockets
find /path -type p # Named pipes
:chart_with_upwards_trend: Finding by Size
Find Files Larger Than Size
find /path -size +100M
What this does:
- Finds files larger than specified size
+means "larger than"- Size units:
c(bytes),k(KB),M(MB),G(GB)
Size units:
c- Bytesk- Kilobytes (1024 bytes)M- Megabytes (1024 KB)G- Gigabytes (1024 MB)
Examples:
find /home -size +1G # Files larger than 1GB
find /home -size +100M # Files larger than 100MB
find /home -size +1k # Files larger than 1KB
Find Files Smaller Than Size
find /path -size -100M
What this does:
- Finds files smaller than specified size
-means "smaller than"
Example:
find . -size -1k
Finds files smaller than 1KB.
Find Files of Exact Size
find /path -size 100M
What this does:
- Finds files of exactly specified size
- No
+or-means exact match (within rounding)
:calendar: Finding by Time
Find Files Modified in Last N Days
find /path -mtime -7
What this does:
- Finds files modified in last 7 days
-mtime= modification time-7= within last 7 days+7= more than 7 days ago7= exactly 7 days ago
Examples:
find /home -mtime -1 # Modified in last 24 hours
find /home -mtime -7 # Modified in last week
find /home -mtime -30 # Modified in last month
find /home -mtime +30 # Modified more than 30 days ago
Find Files Accessed in Last N Days
find /path -atime -7
What this does:
- Finds files accessed in last 7 days
-atime= access time- Same syntax as
-mtime
Find Files Changed in Last N Minutes
find /path -mmin -60
What this does:
- Finds files modified in last 60 minutes
-mmin= modification time in minutes- More precise than
-mtime
Examples:
find . -mmin -30 # Modified in last 30 minutes
find . -mmin -60 # Modified in last hour
find . -mmin +1440 # Modified more than 24 hours ago
:lock: Finding by Permissions
Find Files with Specific Permissions
find /path -perm 644
What this does:
- Finds files with exact permissions
644= rw-r--r-- (read/write for owner, read for others)
Common permissions:
644- Regular files (rw-r--r--)755- Executable files/directories (rwxr-xr-x)600- Private files (rw-------)
Find Executable Files
find /path -executable
What this does:
- Finds files with execute permission
- Useful for finding scripts and programs
Find Readable Files
find /path -readable
What this does:
- Finds files readable by current user
- Based on actual permissions, not just file mode
:mag_right: Combining Criteria
Find Files Matching Multiple Criteria
find /path -name "*.txt" -type f -size +1M
What this does:
- Finds .txt files larger than 1MB
- Multiple criteria are ANDed together (all must match)
Find Files Matching Any Criteria (OR)
find /path \( -name "*.txt" -o -name "*.log" \)
What this does:
- Finds files matching either pattern
-o= OR operator- Parentheses and backslashes required
Example:
find . \( -name "*.md" -o -name "*.txt" \)
Finds both .md and .txt files.
Negate Criteria (NOT)
find /path ! -name "*.tmp"
What this does:
- Finds files NOT matching the pattern
!= NOT operator
Example:
find . -type f ! -name "*.md"
Finds all files except .md files.
:wrench: Actions on Found Files
Print Found Files (Default)
find /path -name "*.txt" -print
What this does:
- Prints found files (this is the default action)
-printis usually optional
List Files with Details
find /path -name "*.txt" -ls
What this does:
- Lists files with detailed information
- Similar to
ls -loutput - Shows permissions, size, date, etc.
Execute Command on Found Files
find /path -name "*.txt" -exec command {} \;
What this does:
- Executes command on each found file
{}is replaced with filename\;ends the command
Example:
find . -name "*.txt" -exec wc -l {} \;
Counts lines in each .txt file.
Important: Be very careful with -exec! Test with -print first.
Delete Found Files (DANGEROUS!)
find /path -name "*.tmp" -delete
What this does:
- DELETES found files
- VERY DANGEROUS - test first with
-print! - No confirmation, files are permanently deleted
Always test first:
# First, see what would be deleted:
find /path -name "*.tmp" -print
# Then, if correct, delete:
find /path -name "*.tmp" -delete
:bulb: Common Troubleshooting Scenarios
Scenario 1: Find Large Files
Problem: Need to find files taking up space.
Solution:
find /home -type f -size +100M
Finds files larger than 100MB.
Sort by size:
find /home -type f -size +100M -exec ls -lh {} \; | sort -k5 -hr
Scenario 2: Find Recently Modified Files
Problem: Need to find files changed recently.
Solution:
find /path -type f -mtime -1
Finds files modified in last 24 hours.
Scenario 3: Find Files by Extension
Problem: Need all files of a specific type.
Solution:
find /path -type f -name "*.extension"
Example:
find . -type f -name "*.log"
Scenario 4: Find Empty Files/Directories
Problem: Need to find empty files or directories.
Solution:
find /path -type f -empty # Empty files
find /path -type d -empty # Empty directories
Scenario 5: Find Files by Owner
Problem: Need files owned by specific user.
Solution:
find /path -user username
Example:
find /home -user matt
Scenario 6: Find and Count Files
Problem: Need to count matching files.
Solution:
find /path -name "*.txt" | wc -l
Counts number of .txt files.
Scenario 7: Find Files and Exclude Directories
Problem: Need to search but exclude certain directories.
Solution:
find /path -path /path/exclude -prune -o -name "*.txt" -print
Example:
find /home -path /home/.cache -prune -o -name "*.txt" -print
Excludes .cache directory from search.
:keyboard: Quick Reference
Basic Commands
find . # Current directory
find /path # Specific path
find /path -maxdepth 2 # Limit depth
Finding by Name
find /path -name "file" # Exact name
find /path -name "*.txt" # Pattern
find /path -iname "file" # Case-insensitive
Finding by Type
find /path -type f # Files only
find /path -type d # Directories only
Finding by Size
find /path -size +100M # Larger than 100MB
find /path -size -1k # Smaller than 1KB
Finding by Time
find /path -mtime -7 # Modified in last week
find /path -mmin -60 # Modified in last hour
find /path -atime -1 # Accessed today
Finding by Permissions
find /path -perm 644 # Specific permissions
find /path -executable # Executable files
find /path -readable # Readable files
Combining Criteria
find /path -name "*.txt" -type f -size +1M
find /path \( -name "*.txt" -o -name "*.log" \)
find /path ! -name "*.tmp"
Actions
find /path -name "*.txt" -print # Print (default)
find /path -name "*.txt" -ls # List with details
find /path -name "*.txt" -exec cmd {} \; # Execute command
:warning: Important Safety Tips
- Always test with
-printfirst before using-deleteor-exec - Use quotes around patterns with wildcards
- Limit search scope with
-maxdepthto avoid long searches - Be careful with
-delete- it permanently deletes files! - Test
-execcommands on a small set first - Use
2>/dev/nullto suppress permission denied errors:find /path -name "*.txt" 2>/dev/null
Summary
This guide covered:
- Basic Usage:
- Searching directories
- Limiting depth
- Finding by Name:
- Exact names and patterns
- Case-insensitive search
- Finding by Type:
- Files, directories, links
- Finding by Size:
- Larger/smaller than, exact size
- Finding by Time:
- Modification and access time
- Days and minutes
- Finding by Permissions:
- Specific permissions
- Executable/readable files
- Combining Criteria:
- AND, OR, NOT operators
- Actions:
- Print, list, execute, delete
Next Steps:
- Practice with safe searches first
- Combine with other commands (grep, ls, etc.)
- Learn to use
-execcarefully - Master pattern matching with wildcards
For text searching in files, see the Grep Command Guide. For file permissions, see the File Permissions Guide.