Find files by name (case-insensitive)
find /home -iname “*.conf” -type f
> [!info]+ Command Breakdown
> 1. **-iname "*.conf"**: Case-insensitive pattern matching for files ending in `.conf`
> 2. **-type f**: Restricts results to regular files only
> 3. *Wildcard `*` matches any characters before `.conf` extension*
> 4. *Useful for locating configuration files across user directories*
```bash
# Find files larger than 100MB
find / -type f -size +100M 2>/dev/null
[!info]+ Command Breakdown
- -size +100M: Files greater than 100 megabytes
- 2>/dev/null: Redirects permission-denied errors to avoid output clutter
- Starting from root
/requires elevated privileges for complete results- Useful for identifying large files consuming disk space or potential data exfiltration
# Find files modified between two dates
find /data -newermt "2025-12-01" ! -newermt "2026-01-01"
[!info]+ Command Breakdown
- -newermt “2025-12-01”: Files modified after (newer than) December 1, 2025
- ! -newermt “2026-01-01”:
!negates the test; files NOT newer than January 1, 2026- Logical combination creates a date range: December 1-31, 2025
- Critical for incident response timeline analysis
# Find and delete empty directories
find /tmp -type d -empty -delete
[!warning]+ Command Breakdown
- -type d: Targets directories only
- -empty: Matches directories with no contents
- -delete: Deletes matched items (implies
-depthtraversal)- Use with extreme caution—deletion is immediate and irreversible
- Test with
-deleteto verify targets
SUID/SGID and World-Writable File Discovery
Files with SUID/SGID bits or world-writable permissions are high-value targets for privilege escalation:
- SUID (Set User ID): Executes with file owner’s privileges (typically root)
- SGID (Set Group ID): Executes with file group’s privileges
- World-writable: Any user can modify the file
- Cross-reference SUID binaries with GTFOBins for exploitation paths
- World-writable config files in
/etcare critical escalation vectors
SUID/SGID Discovery Commands
# Find SUID files
find / -type f -perm -4000 2>/dev/null
[!info]+ Command Breakdown
- -perm -4000: Files with at least the SUID bit (octal 4000) set
- -type f: Restricts to regular files (not directories)
- The
-prefix means “at least these permission bits”—file may have additional permissions- Common legitimate SUID binaries:
/usr/bin/passwd,/usr/bin/sudo,/bin/ping
# Find SGID files
find / -type f -perm -2000 2>/dev/null
SGID characteristics:
- SGID on executables runs with group privileges
- SGID on directories causes new files to inherit directory’s group
- Less common for privilege escalation than SUID but still valuable
- Check output against system baseline for anomalies
# Find SUID or SGID files
find / -type f \( -perm -4000 -o -perm -2000 \) 2>/dev/null
[!info]+ Command Breakdown
- ( … ): Parentheses create logical grouping (escaped for shell)
- -o: Logical OR operator—matches either condition
- Comprehensive search for all elevated permission binaries
- Output should be compared against baseline for anomaly detection
World-Writable Discovery Commands
# Find world-writable files
find / -type f -perm -0002 2>/dev/null
Security implications:
- Extremely dangerous if file is executed or sourced by privileged processes
- Check ownership—writable files owned by root are highest priority
- Common in web directories due to misconfigurations
- Potential for code injection or configuration tampering
# Find world-writable directories (sticky bit often expected)
find / -type d -perm -0002 2>/dev/null
Expected vs. dangerous:
- World-writable directories like
/tmptypically have sticky bit (1000) set - Sticky bit prevents users from deleting others’ files
- Missing sticky bit on writable directory is a misconfiguration
- Check
/var/www,/var/tmp,/dev/shmfor anomalies
Advanced Privilege Escalation Enumeration
# SUID binaries owned by root (common priv-esc targets)
find / -type f -perm -4000 -user root 2>/dev/null
Analysis approach:
- Root-owned SUID binaries execute with root privileges
- Focus on non-standard binaries not in
/usr/binor/bin - Test discovered binaries against GTFOBins for known exploits
- Document custom SUID binaries for deeper analysis
# SUID/SGID with detailed output
find / -type f \( -perm -4000 -o -perm -2000 \) -exec ls -la {} \; 2>/dev/null
[!info]+ Command Breakdown
- -exec ls -la {} ;: Executes
ls -laon each matched file- {}: Placeholder replaced with found filename
- ;: Required terminator for
-exec(escaped for shell)- Provides full permission string, owner, group, size, and modification date
# World-writable files excluding /proc and /sys
find / -path /proc -prune -o -path /sys -prune -o -type f -perm -0002 -print 2>/dev/null
[!info]+ Command Breakdown
- -path /proc -prune: Excludes
/procdirectory from traversal- -o: OR operator—chains pruning and search logic
- -prune: Prevents descending into matched directory
/procand/sysare pseudo-filesystems with world-writable entries by design- Excluding them reduces noise and improves performance
# World-writable directories without sticky bit (dangerous)
find / -type d \( -perm -0002 -a ! -perm -1000 \) 2>/dev/null
[!danger]+ Command Breakdown
- -a: Logical AND operator—both conditions must be true
- ! -perm -1000: Negates sticky bit check (octal 1000)
- World-writable directory without sticky bit allows any user to delete any file
- Severe misconfiguration—often found in poorly configured web directories
OPSEC considerations:
- Full system scans generate high I/O and may trigger file integrity monitoring (AIDE, OSSEC)
- Redirect stderr (
2>/dev/null) to avoid logging permission-denied paths in shell history - Consider running during high-activity periods to blend with baseline noise
- Use
-maxdepthto limit scope and reduce detection surface - Combine with
-xdevto avoid traversing network mounts (reduces latency and external logs)
Command Execution with -exec and xargs
Three primary methods exist for executing commands on found files, each with distinct performance and safety characteristics:
- -exec cmd {} ;: Forks command once per file (slower, more visible)
- -exec cmd {} +: Batches files into single command invocation (faster, less visible)
- find | xargs: Batches via pipe, respects ARG_MAX, supports parallelism
Execution Method Comparison
| Method | Behaviour | Performance | Use Case | OPSEC Impact |
|---|---|---|---|---|
-exec cmd {} \; | Forks cmd once per file | Slow | Small sets, complex per-file logic | High (many processes) |
-exec cmd {} + | Batches files into one cmd | Fast | Large sets, simple commands | Low (few processes) |
find | xargs | Batches via pipe | Fast | Very large sets, custom batching | Low (few processes) |
find -print0 | xargs -0 | Null-delimited batching | Fast | Filenames with spaces/newlines | Low (safe handling) |
xargs -P N | Parallel execution | Fastest | CPU-bound operations | Medium (multiple concurrent processes) |
-exec Examples
# -exec with \; (one command per file – slower)
find . -type f -name "*.log" -exec rm {} \;
[!info]+ Command Breakdown
- -exec rm {}: Executes
rmcommand with{}replaced by filename- ;: Terminator indicating end of command (backslash escapes semicolon from shell)
- Spawns one
rmprocess per file—thousands of files = thousands of processes- High overhead but allows per-file command customization
# -exec with + (batched arguments – faster)
find . -type f -name "*.log" -exec rm {} +
Batching behaviour:
- Combines multiple filenames into single command invocation
- Example:
rm file1.log file2.log file3.loginstead of three separatermcalls - Respects system ARG_MAX limit—automatically splits into multiple batches if needed
- Preferred method for large-scale operations
# Grep for pattern in PHP files (batched)
find /var/www -type f -name "*.php" -exec grep -l "eval(" {} +
[!info]+ Command Breakdown
- grep -l “eval(”: Lists filenames containing the string
eval((potential web shell indicator)- -exec … +: Batches PHP files into single
grepinvocation for efficiency- Useful for web application security audits and malware hunting
- Consider escaping parentheses in grep pattern depending on shell context
# Change ownership in batches
find /data -type f -exec chown appuser:appgroup {} +
Performance notes:
- Changes file owner to
appuserand group toappgroup - Batching significantly reduces execution time on large directory trees
- Common post-deployment task or privilege management operation
- Requires appropriate permissions (typically root/sudo)
xargs Examples
# Pipe to xargs (batched, handles large sets)
find . -type f -name "*.log" -print0 | xargs -0 rm
[!info]+ Command Breakdown
- -print0: Outputs null-delimited filenames (handles spaces, newlines, special characters)
- xargs -0: Reads null-delimited input from stdin
- The
-0pairing is critical for safe handling of unusual filenames- xargs automatically batches arguments respecting ARG_MAX
# xargs with parallelism
find . -type f -name "*.log" -print0 | xargs -0 -P 4 rm
Parallelism considerations:
- -P 4: Runs up to 4 parallel
rmprocesses simultaneously - Significantly faster for CPU-bound operations (compression, checksumming)
- Use
-P 1to force serial execution if parallelism causes detection - Monitor system load—excessive parallelism can overwhelm resources
# Safe delete with confirmation (interactive)
find . -name "*.tmp" -print0 | xargs -0 -p rm
Interactive mode:
- -p: Prompts user before executing each command
- Safety mechanism for destructive operations
- User must type
yto confirm each deletion - Not suitable for automated scripts—use only for manual operations
# Compress logs older than 30 days (parallel)
find /var/log -type f -mtime +30 -name "*.log" -print0 | xargs -0 -P 4 gzip
[!info]+ Command Breakdown
- -mtime +30: Files modified more than 30 days ago
- gzip: Compresses each file (replaces original with
.gzversion)- -P 4: Compresses 4 files simultaneously
- Common log rotation cleanup task
- Parallelism ideal for CPU-intensive compression workloads
Additional xargs Options
| Flag | Description | Example Use |
|---|---|---|
-n N | Max N arguments per invocation | xargs -n 1 processes one file at a time |
-r | Don’t run if input is empty | Prevents errors when find returns nothing |
-I {} | Replace string placeholder | xargs -I {} mv {} /backup/ |
-t | Print command before executing | Debugging and logging |
--show-limits | Display ARG_MAX and buffer sizes | System capability check |
[!warning]+ Common Errors
- Missing
-0with xargs when filenames contain spaces: Command breaks or acts on wrong files—always use-print0 | xargs -0pairing- Forgetting
\;or+at end of-exec: Syntax error—required terminator- Using
-deletebefore other predicates: Evaluation order matters;-deleteimplies-depthtraversal- Forgetting
-rwith xargs when find returns nothing: Unexpected command execution with no arguments- Exceeding ARG_MAX with
-exec {} +: Rare on modern systems—xargs auto-splits, but find may fail on ancient systems
Time-Based File Searches
find supports three timestamp types for file matching:
- mtime: File modification time (content changed)
- atime: File access time (content read)
- ctime: Inode change time (metadata changed—permissions, ownership, name)
- Each has day-based (
-mtime) and minute-based (-mmin) variants - Critical for incident response, forensic analysis, and log management
Time Predicate Syntax
| Predicate | Meaning | Measurement Unit |
|---|---|---|
-mtime n | Modified exactly n days ago | 24-hour periods |
-mtime +n | Modified more than n days ago | 24-hour periods |
-mtime -n | Modified within last n days | 24-hour periods |
-atime n/+n/-n | Access time variants | 24-hour periods |
-ctime n/+n/-n | Inode change time variants | 24-hour periods |
-mmin n/+n/-n | Modification time | Minutes |
-amin n/+n/-n | Access time | Minutes |
-cmin n/+n/-n | Inode change time | Minutes |
-newermt "date" | Modified after specified date | ISO 8601 format |
-newer reference | Modified more recently than file | File comparison |
-daystart | Measure from start of today | Changes reference point |
Time-Based Search Examples
# Files modified in the last 24 hours
find /var/log -type f -mtime 0
Interpretation:
- -mtime 0: Files modified between now and 24 hours ago
0represents the current 24-hour period from now- Useful for identifying recently changed logs during incident investigation
- Does not mean “modified today”—use
-daystart -mtime 0for that
# Files modified more than 30 days ago
find /tmp -type f -mtime +30
Interpretation:
- -mtime +30: Files with modification time older than 30 days
- + prefix means “more than”—excludes files at exactly 30 days
- Common cleanup pattern for temporary directories
- Combine with
-deleteor-exec rmfor automated maintenance
# Files modified in the last 60 minutes
find /home -type f -mmin -60
Interpretation:
- -mmin -60: Files modified within the last 60 minutes
- - prefix means “less than”—within the specified timeframe
- Higher resolution than day-based predicates
- Essential for real-time security monitoring and breach detection
# Files modified yesterday (using -daystart)
find /data -daystart -mtime 1 -type f
[!info]+ Command Breakdown
- -daystart: Changes reference point to midnight today (00:00) instead of current time
- -mtime 1: Exactly 1 day ago from reference point
- Without
-daystart,1means “24-48 hours ago from now”- Order matters:
-daystartmust appear before-mtimein expression
# Files modified between two dates
find /logs -newermt "2025-12-01" ! -newermt "2025-12-31"
[!info]+ Command Breakdown
- -newermt “2025-12-01”: Modified after (newer than) December 1, 2025 00:00:00
- ! -newermt “2025-12-31”:
!negates—NOT newer than December 31, 2025 00:00:00- Creates inclusive date range: December 1-30, 2025
- Requires quotes around dates; supports ISO 8601 format with time:
"2025-12-01 14:30:00"
# Files accessed more recently than a reference file
find /app -newer /app/deploy.timestamp
Use cases:
- -newer /app/deploy.timestamp: Files modified more recently than reference file’s mtime
- Useful for identifying files changed since last deployment
- Create timestamp files with
touchto mark events - Variant:
-anewerfor atime comparison,-cnewerfor ctime
Advanced Time-Based Queries
# Files modified today (calendar day, not 24 hours)
find /var/log -type f -daystart -mtime 0 -printf "%T+ %p\n"
[!info]+ Command Breakdown
- -daystart -mtime 0: Files modified since midnight today
- -printf “%T+ %p\n”: Custom format—
%T+is ISO timestamp,%pis path- Output format:
2026-01-18+09:30:15.0000000000 /var/log/auth.log- Pipe to
sortfor chronological ordering
# Files NOT accessed in the last 90 days (candidates for archival)
find /archive -type f -atime +90 -ls
Archival workflow:
- -atime +90: Access time older than 90 days
- -ls: Long listing output with timestamps
- Identifies stale files for archival or deletion
- Warning: atime may be unreliable on filesystems with
noatimeorrelatimemount options
OPSEC and forensic considerations:
- Access time queries may update atime on some filesystems: Recursive find can modify the evidence you’re searching for
noatimeorrelatimemount options: Access time may be stale or not updated—verify mount options withmount | grep atime- Timestomping: Adversaries can modify file timestamps—time-based queries less reliable if attacker has touched files
- Timezone considerations: Timestamps in UTC vs local time—use
%T+printf format for ISO 8601 with timezone - Inode change time (ctime) cannot be modified by standard tools: More forensically reliable than mtime/atime
[!tip]+ Performance Optimization
- Combine time predicates with
-typeearly in expression for faster evaluation- Use
-maxdepthto limit search scope when possible- Redirect stderr (
2>/dev/null) to avoid permission-denied overhead- Consider
locatedatabase for name-based searches if time constraints allow- Use
-xdevto avoid crossing mount points and network filesystems
References
- GNU findutils Manual
- Linux find Man Page
- Red Hat: Linux find Command
- Cyberciti: Finding Files by Date
- Red Hat: Audit Permissions with find
- Baeldung: Find Modified Date
- Endpoint Dev: Efficiency of find -exec vs xargs
- CaveOps: find -exec vs find | xargs
- GTFOBins
- MITRE ATT&CK: File and Directory Discovery
- HackTricks: Linux Privilege Escalation
- SANS: Incident Response Process
#Linux #FileSystemEnumeration #find #xargs #SUID #SGID #PrivilegeEscalation #IncidentResponse #Forensics #SystemAdministration #PenetrationTesting #Reconnaissance #GTFOBins