-
-
Notifications
You must be signed in to change notification settings - Fork 2
Find Command Guide
- 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
-
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
find .What this does:
- Searches current directory (
.) and all subdirectories - Shows all files and directories
- Very long output (usually combined with other options)
find /path/to/directoryWhat this does:
- Searches specified directory and all subdirectories
- Recursive search (includes all subdirectories)
Example:
find /homefind /path -maxdepth 2What 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 1Shows only items directly in /home, not subdirectories.
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 /path -name "*.txt"What this does:
- Finds files matching pattern
-
*matches any characters -
?matches single character
Common patterns:
-
*.txt- All .txt files -
file*- Files starting with "file" -
*.log- All log files -
test?.txt- test1.txt, test2.txt, etc.
Example:
find . -name "*.md"Finds all Markdown files.
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.
find /path -type fWhat this does:
- Finds only files (not directories)
-
-type f= files only
find /path -type dWhat this does:
- Finds only directories
-
-type d= directories only
Example:
find /home -maxdepth 1 -type dfind /path -type l # Symbolic links
find /path -type s # Sockets
find /path -type p # Named pipesfind /path -size +100MWhat this does:
- Finds files larger than specified size
-
+means "larger than" - Size units:
c(bytes),k(KB),M(MB),G(GB)
Size units:
-
c- Bytes -
k- 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 1KBfind /path -size -100MWhat this does:
- Finds files smaller than specified size
-
-means "smaller than"
Example:
find . -size -1kFinds files smaller than 1KB.
find /path -size 100MWhat this does:
- Finds files of exactly specified size
- No
+or-means exact match (within rounding)
find /path -mtime -7What this does:
- Finds files modified in last 7 days
-
-mtime= modification time -
-7= within last 7 days -
+7= more than 7 days ago -
7= 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 agofind /path -atime -7What this does:
- Finds files accessed in last 7 days
-
-atime= access time - Same syntax as
-mtime
find /path -mmin -60What 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 agofind /path -perm 644What 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 /path -executableWhat this does:
- Finds files with execute permission
- Useful for finding scripts and programs
find /path -readableWhat this does:
- Finds files readable by current user
- Based on actual permissions, not just file mode
find /path -name "*.txt" -type f -size +1MWhat this does:
- Finds .txt files larger than 1MB
- Multiple criteria are ANDed together (all must match)
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.
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.
find /path -name "*.txt" -printWhat this does:
- Prints found files (this is the default action)
-
-printis usually optional
find /path -name "*.txt" -lsWhat this does:
- Lists files with detailed information
- Similar to
ls -loutput - Shows permissions, size, date, etc.
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.
find /path -name "*.tmp" -deleteWhat 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" -deleteProblem: Need to find files taking up space.
Solution:
find /home -type f -size +100MFinds files larger than 100MB.
Sort by size:
find /home -type f -size +100M -exec ls -lh {} \; | sort -k5 -hrProblem: Need to find files changed recently.
Solution:
find /path -type f -mtime -1Finds files modified in last 24 hours.
Problem: Need all files of a specific type.
Solution:
find /path -type f -name "*.extension"Example:
find . -type f -name "*.log"Problem: Need to find empty files or directories.
Solution:
find /path -type f -empty # Empty files
find /path -type d -empty # Empty directoriesProblem: Need files owned by specific user.
Solution:
find /path -user usernameExample:
find /home -user mattProblem: Need to count matching files.
Solution:
find /path -name "*.txt" | wc -lCounts number of .txt files.
Problem: Need to search but exclude certain directories.
Solution:
find /path -path /path/exclude -prune -o -name "*.txt" -printExample:
find /home -path /home/.cache -prune -o -name "*.txt" -printExcludes .cache directory from search.
find . # Current directory
find /path # Specific path
find /path -maxdepth 2 # Limit depthfind /path -name "file" # Exact name
find /path -name "*.txt" # Pattern
find /path -iname "file" # Case-insensitivefind /path -type f # Files only
find /path -type d # Directories onlyfind /path -size +100M # Larger than 100MB
find /path -size -1k # Smaller than 1KBfind /path -mtime -7 # Modified in last week
find /path -mmin -60 # Modified in last hour
find /path -atime -1 # Accessed todayfind /path -perm 644 # Specific permissions
find /path -executable # Executable files
find /path -readable # Readable filesfind /path -name "*.txt" -type f -size +1M
find /path \( -name "*.txt" -o -name "*.log" \)
find /path ! -name "*.tmp"find /path -name "*.txt" -print # Print (default)
find /path -name "*.txt" -ls # List with details
find /path -name "*.txt" -exec cmd {} \; # Execute command-
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
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.