Searching Files
CPTS Notes
- Foundations
- Enumeration with Nmap
- Passwords
- Wordlists
- Cracking
- Protected Files
- Spraying and Defaults
- Hunting (Windows)
- Hunting (Linux)
- Hunting (Shares)
- Hunting (Traffic)
- Looting (Windows)
- Looting (Linux)
- File Services
- FTP
- SMB
- NFS
- Rsync
- Network
- DNS
- SNMP
- SMTP
- Mail Retrieval
- Cloud Mail
- Databases
- MySQL
- MSSQL
- Remote Access
- SSH
- R-Services
- RDP
- WinRM
- WMI
- Linux
- Reading Files
- Editing Files
- Searching Files
- Requests and Transfer
- Local Enumeration
- Privesc Primer
- File Transfer
- Choosing a Method
- Serving Files
- Windows Transfers
- Linux Transfers
- Code One-Liners
- Session Transfers
- Protected Transfers
- Detection & Evasion
- Windows
- Reading Files
- Editing Files
- Searching Files
- Requests and Transfer
- Local Enumeration
- Privesc Primer
- Reference
- Port References
- DNS Resolution
- The Mail Pipeline
- SMB & NetBIOS
A find command takes start paths, tests, and actions:
find <paths> <tests> <actions> 2>/dev/null # general formTests filter files by name, type, owner, permissions, time, and size. Actions print, list, or run a
command on each match. grep searches inside files. On a target, the main searches are SUID binaries,
writable files, and files that hold credentials.
How a find command reads
find walks each start path, checks every file against the tests from left to right, and runs the action on each file that passes all of them.
find /etc -type f -name "*.conf" -ls 2>/dev/null # .conf files under /etc, with details| Part | Example | Meaning |
|---|---|---|
| Path | /etc |
Where the search starts. Give several paths, or / for the whole box. Default: . |
| Test | -type f -name "*.conf" |
Conditions a file must meet. |
| Action | -ls |
What find does with each match. Default: -print (print the path). |
2>/dev/null |
Sends error output, such as “Permission denied”, to /dev/null. |
Search several start paths in one command:
find /home /tmp /var/www -name "*.txt" 2>/dev/null # .txt files in three treesBy name
Match a file name exactly (case-sensitive):
find / -name "config.php" 2>/dev/null # exact nameMatch a file name in any case:
find / -iname "*.conf" 2>/dev/null # name in any caseMatch the full path:
find / -path "*/.ssh/*" 2>/dev/null # files inside any .ssh folder| Flag | Meaning |
|---|---|
-name PATTERN |
The file name (the last part of the path) matches PATTERN. Case-sensitive. |
-iname PATTERN |
Same as -name, case-insensitive. |
-path PATTERN |
The full path matches PATTERN. * also matches /. |
* |
Any characters, or none. It also matches a leading dot: "*.env" matches .env. |
? |
One character. |
[0-9] |
One character from the set or range. |
Quote every pattern. Without quotes, the shell expands
*.confagainst the current directory first. Ifa.confexists there, find gets-name a.confand only finds files with that name.
By type
Find directories by name:
find / -type d -name "backup*" 2>/dev/null # folders named backup*| Flag | Meaning |
|---|---|
-type f |
Regular file. |
-type d |
Directory. |
-type l |
Symbolic link. |
Depth and skipping trees
List each home directory and the directories one level inside it:
find /home -maxdepth 2 -type d 2>/dev/null # home folders and one level insideList the contents of a directory without the directory itself:
find /opt -mindepth 1 -maxdepth 1 # contents of /opt onlySkip a tree with -prune:
find / -path /proc -prune -o -name "*.conf" -print 2>/dev/null # skip /procSkip several trees:
# skip /proc, /sys, and /runfind / \( -path /proc -o -path /sys -o -path /run \) -prune -o -type f -name "*.conf" -print 2>/dev/nullHide results under a path:
find / -name "*.conf" -not -path "/proc/*" 2>/dev/null # hide results under /proc| Flag | Meaning |
|---|---|
-maxdepth N |
Go at most N levels below the start paths. 0 checks only the start paths. |
-mindepth N |
Ignore files less than N levels deep. 1 drops the start paths. |
-prune |
Do not go into the current directory. |
-not -path PATTERN |
Drop results whose path matches PATTERN. |
A
-prunecommand needs-not -pathonly hides results: find still walks the tree.-prunestops find from entering it, which is faster on large trees like/proc.
By owner and group
Files a user owns:
find / -user www-data -type f 2>/dev/null # owned by www-dataFiles a group owns:
find / -group adm 2>/dev/null # owned by group admFiles whose owner has no account:
find / -nouser 2>/dev/null # owner has no account| Flag | Meaning |
|---|---|
-user NAME |
Owned by user NAME. A numeric UID also works. |
-group NAME |
Owned by group NAME. A numeric GID also works. |
-nouser |
The owner UID has no entry in /etc/passwd. |
-nogroup |
The group GID has no entry in /etc/group. |
By permissions
-perm takes a mode in three forms:
| Form | Meaning |
|---|---|
-perm 644 |
The mode is exactly 644. |
-perm -644 |
The mode has all bits in 644. It can have other bits too. |
-perm /644 |
The mode has at least one bit in 644. |
The bits a pentester searches for:
| Bits | Meaning |
|---|---|
4000 (u=s) |
SUID: the file runs as its owner. |
2000 (g=s) |
SGID: the file runs as its group. |
0002 (o+w) |
World-writable: any user can write to it. |
SUID files:
find / -perm -4000 -type f 2>/dev/null # SUIDSGID files:
find / -perm -2000 -type f 2>/dev/null # SGIDSUID or SGID files:
find / -perm /6000 -type f 2>/dev/null # SUID or SGIDWorld-writable files:
find / -perm -0002 -type f 2>/dev/null # world-writable, octalfind / -perm -o+w -type f 2>/dev/null # world-writable, symbolicFiles the current user can write to or read:
find / -writable -type f 2>/dev/null # files you can write tofind /root -readable 2>/dev/null # files you can read in /root| Flag | Meaning |
|---|---|
-perm MODE |
Mode test in one of the three forms above. Takes octal (-4000) or symbolic (-u=s) modes. |
-writable |
The current user can write to the file. Counts group membership and ACLs. |
-readable |
The current user can read the file. Counts group membership and ACLs. |
-permreads only the mode bits.-writableand-readableask the system whether the current user has access, so they also catch access through a group or an ACL.
For what to do with a SUID or SGID hit, see Privesc Primer.
By time
Files modified in the last 10 minutes:
find / -type f -mmin -10 2>/dev/null # last 10 minutesFiles modified in the last 24 hours:
find / -type f -mtime -1 2>/dev/null # last 24 hoursFiles modified more recently than a reference file:
find /var/www -type f -newer /var/www/html/index.php 2>/dev/null # newer than index.phpFiles modified after a date:
find / -type f -newermt "2026-10-01" 2>/dev/null # after 1 October 2026Files modified between two dates:
find / -type f -newermt "2026-10-01" ! -newermt "2026-10-08" 2>/dev/null # between two dates| Flag | Meaning |
|---|---|
-mmin N |
Content modified N minutes ago. |
-mtime N |
Content modified N days (24-hour periods) ago. |
-amin N, -atime N |
Same, for the last access. |
-cmin N, -ctime N |
Same, for the last status change (owner, mode, or content). |
-newer FILE |
Modified more recently than FILE. |
-newermt DATE |
Modified after DATE, for example "2026-10-01" or "2026-10-01 14:00". |
-N, +N, N |
Less than N, more than N, exactly N. |
-mtimecounts whole 24-hour periods and drops the fraction.-mtime +1matches files at least 2 days old.
By size
Files larger than 100 MiB:
find / -type f -size +100M 2>/dev/null # over 100 MiBFiles smaller than 1024 bytes:
find /etc -type f -size -1024c 2>/dev/null # under 1024 bytesEmpty files and directories:
find /var/www -empty 2>/dev/null # empty files and folders| Flag | Meaning |
|---|---|
-size N[unit] |
File size. +N larger, -N smaller, N exactly. |
c |
Bytes. |
k, M, G |
KiB, MiB, GiB. |
| (no unit) | 512-byte blocks. |
-empty |
Empty file, or directory with no entries. |
find rounds the file size up to the unit. A 4-byte file counts as
1k, so-size -1kmatches only empty files. Usecfor small limits.
Combining tests
Tests next to each other must all match. find adds an implicit -a between them.
Match either of two names:
find / -type f \( -name "*.bak" -o -name "*.old" \) 2>/dev/null # .bak or .oldExclude a name:
find /home -type f ! -name "*.jpg" 2>/dev/null # everything but .jpg| Operator | Meaning |
|---|---|
-a, -and |
Both tests match. Implicit between tests. |
-o, -or |
Either test matches. |
!, -not |
The next test does not match. |
\( ... \) |
Group tests. Escape the parentheses and put spaces around them. |
find evaluates
-abefore-o. In-name "*.bak" -o -name "*.old" -exec ..., the-execbinds to the.oldtest only, and.bakfiles never reach it. Put the-otests in parentheses.
Actions
Run a command once per file:
find / -name "*.conf" -exec grep -l "password" {} \; 2>/dev/null # one grep per fileRun a command once with many files:
find / -name "*.conf" -exec grep -l "password" {} + 2>/dev/null # one grep for many filesList matches with details, like ls -dils:
find / -perm -4000 -type f -ls 2>/dev/null # SUID files with detailsPrint chosen fields:
find /home -type f -printf "%M %u:%g %s %TY-%Tm-%Td %p\n" 2>/dev/null # mode, owner, size, date, pathDelete matches:
find /tmp/work -name "*.tmp" -delete # delete .tmp filesSend paths to xargs, safe for spaces and newlines in names:
find /var/www -name "*.php" -print0 | xargs -0 grep -l "password" # null-separated paths to grep| Flag | Meaning |
|---|---|
-exec CMD {} \; |
Run CMD once per file. {} is the file path. \; ends the command. |
-exec CMD {} + |
Run CMD with many paths per call. Faster. {} must come just before +. |
-ls |
Print inode, size, permissions, links, owner, group, time, and path. |
-printf FORMAT |
Print each match in FORMAT. |
-delete |
Delete each match. |
-print0 |
Print paths separated by a null byte. |
xargs -0 |
Read null-separated input and run the command on it. |
grep -l |
Print only the names of files that contain a match. |
Common -printf directives:
| Directive | Meaning |
|---|---|
%p |
Full path. |
%f |
File name. |
%u, %g |
Owner, group. |
%m, %M |
Mode in octal (644), mode in symbols (-rw-r--r--). |
%s |
Size in bytes. |
%TY-%Tm-%Td |
Modification date. |
\n |
New line. -printf adds none by itself. |
Put
-deletelast. find evaluates from left to right, sofind . -delete -name "*.tmp"deletes everything under.. Run the command with
grep
Prints the lines that match a pattern.
Search a folder recursively, any case, with line numbers:
grep -rni "password" /var/www 2>/dev/null # recursive, any case, line numbersSearch only some file types:
grep -rn --include="*.php" "mysqli_connect" /var/www 2>/dev/null # .php files onlyList only the names of the files that match:
grep -rli "DB_PASS" /var/www 2>/dev/null # file names onlyPrint only the matching part, here IPv4 addresses:
grep -oP '\d+\.\d+\.\d+\.\d+' file # IPv4 addressesMatch any of several words:
grep -iE "pass|user|key|token" config.php # any of four wordsStrip comments and blank lines from a config:
grep -vE '^\s*(#|$)' /etc/ssh/sshd_config # drop comments and blank linesPull IP addresses out of any output:
grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' scan.txt | sort -u # unique IPv4 addressesCount the matching lines:
grep -c "Failed password" /var/log/auth.log # count failed loginsShow the lines around each match:
grep -A2 -B2 "BEGIN OPENSSH PRIVATE KEY" notes.txt # two lines of context each sideMatch a whole word only (root and not chroot):
grep -w "root" /etc/group # whole word only| Flag | Meaning |
|---|---|
-r |
Search folders recursively |
-n |
Print the line number |
-i |
Ignore case |
-l |
Print only the names of files that match |
-c |
Print a count of matching lines |
-v |
Print the lines that do not match |
-E |
Extended regex: |, +, ?, {n} and () work without backslashes |
-o |
Print only the matched part |
-P |
Perl regex: \d for a digit, lookarounds |
-w |
Match whole words only |
-A n / -B n |
Print n lines after or before each match |
--include="*.ext" |
Search only files that match the glob |
2>/dev/nullhides permission errors.
Pentest searches
SUID and SGID binaries, with details:
find / -type f -perm /6000 -ls 2>/dev/null # SUID and SGID with detailsWhat to do with them: Privesc Primer.
World-writable directories:
find / -path /proc -prune -o -type d -perm -0002 -print 2>/dev/null # world-writable folders, skip /procWorld-writable files:
# world-writable files, skip /proc and /sysfind / \( -path /proc -o -path /sys \) -prune -o -type f -perm -0002 -print 2>/dev/nullFiles the current user owns outside /proc and /home:
# files you own outside /proc and /homefind / -user "$(whoami)" -type f -not -path "/proc/*" -not -path "/home/*" 2>/dev/null$(whoami) puts the current user name into the command.
Files a group owns, for each group from id:
find / -group adm -type f 2>/dev/null # files group adm ownsConfig files that contain “password”:
# config files that mention passwordfind / -type f \( -name "*.conf" -o -name "*.config" -o -name "*.cnf" -o -name "*.ini" -o -name "*.php" -o -name "*.env" \) -not -path "/proc/*" -exec grep -il "password" {} + 2>/dev/nullThe matching lines, with file name and line number:
find /var/www /etc /opt -type f -exec grep -Hni "password" {} + 2>/dev/null # matching lines with file and line numbergrep -i ignores case, -H prints the file name, and -n prints the line number. More on grep in
grep.
Files modified in the last 30 minutes:
find / -type f -mmin -30 -not -path "/proc/*" -not -path "/sys/*" 2>/dev/null # last 30 minutesBackup files, SSH keys, and KeePass databases:
# backups, SSH keys, and KeePass databasesfind / -type f \( -name "*.bak" -o -name "*.old" -o -name "id_rsa*" -o -name "*.kdbx" \) 2>/dev/nullWritable files in /etc:
find /etc -type f -writable 2>/dev/null # writable files in /etcMore checks after landing a shell: Local Enumeration.