~/linux-searching-files

Searching Files

CPTS Notes13 min read

A find command takes start paths, tests, and actions:

find <paths> <tests> <actions> 2>/dev/null # general form

Tests 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 trees

By name

Match a file name exactly (case-sensitive):

find / -name "config.php" 2>/dev/null # exact name

Match a file name in any case:

find / -iname "*.conf" 2>/dev/null # name in any case

Match 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 *.conf against the current directory first. If a.conf exists there, find gets -name a.conf and 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 inside

List the contents of a directory without the directory itself:

find /opt -mindepth 1 -maxdepth 1 # contents of /opt only

Skip a tree with -prune:

find / -path /proc -prune -o -name "*.conf" -print 2>/dev/null # skip /proc

Skip several trees:

# skip /proc, /sys, and /run
find / \( -path /proc -o -path /sys -o -path /run \) -prune -o -type f -name "*.conf" -print 2>/dev/null

Hide 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 -prune command needs -print at the end. Without it, find also prints the pruned directories. -not -path only hides results: find still walks the tree. -prune stops 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-data

Files a group owns:

find / -group adm 2>/dev/null # owned by group adm

Files 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 # SUID

SGID files:

find / -perm -2000 -type f 2>/dev/null # SGID

SUID or SGID files:

find / -perm /6000 -type f 2>/dev/null # SUID or SGID

World-writable files:

find / -perm -0002 -type f 2>/dev/null # world-writable, octal
find / -perm -o+w -type f 2>/dev/null # world-writable, symbolic

Files the current user can write to or read:

find / -writable -type f 2>/dev/null # files you can write to
find /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.

-perm reads only the mode bits. -writable and -readable ask 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 minutes

Files modified in the last 24 hours:

find / -type f -mtime -1 2>/dev/null # last 24 hours

Files modified more recently than a reference file:

find /var/www -type f -newer /var/www/html/index.php 2>/dev/null # newer than index.php

Files modified after a date:

find / -type f -newermt "2026-10-01" 2>/dev/null # after 1 October 2026

Files 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.

-mtime counts whole 24-hour periods and drops the fraction. -mtime +1 matches files at least 2 days old.

By size

Files larger than 100 MiB:

find / -type f -size +100M 2>/dev/null # over 100 MiB

Files smaller than 1024 bytes:

find /etc -type f -size -1024c 2>/dev/null # under 1024 bytes

Empty 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 -1k matches only empty files. Use c for 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 .old

Exclude 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 -a before -o. In -name "*.bak" -o -name "*.old" -exec ..., the -exec binds to the .old test only, and .bak files never reach it. Put the -o tests in parentheses.

Actions

Run a command once per file:

find / -name "*.conf" -exec grep -l "password" {} \; 2>/dev/null # one grep per file

Run a command once with many files:

find / -name "*.conf" -exec grep -l "password" {} + 2>/dev/null # one grep for many files

List matches with details, like ls -dils:

find / -perm -4000 -type f -ls 2>/dev/null # SUID files with details

Print chosen fields:

find /home -type f -printf "%M %u:%g %s %TY-%Tm-%Td %p\n" 2>/dev/null # mode, owner, size, date, path

Delete matches:

find /tmp/work -name "*.tmp" -delete # delete .tmp files

Send 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 -delete last. find evaluates from left to right, so find . -delete -name "*.tmp" deletes everything under .. Run the command with -print first and check the list.

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 numbers

Search only some file types:

grep -rn --include="*.php" "mysqli_connect" /var/www 2>/dev/null # .php files only

List only the names of the files that match:

grep -rli "DB_PASS" /var/www 2>/dev/null # file names only

Print only the matching part, here IPv4 addresses:

grep -oP '\d+\.\d+\.\d+\.\d+' file # IPv4 addresses

Match any of several words:

grep -iE "pass|user|key|token" config.php # any of four words

Strip comments and blank lines from a config:

grep -vE '^\s*(#|$)' /etc/ssh/sshd_config # drop comments and blank lines

Pull IP addresses out of any output:

grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' scan.txt | sort -u # unique IPv4 addresses

Count the matching lines:

grep -c "Failed password" /var/log/auth.log # count failed logins

Show the lines around each match:

grep -A2 -B2 "BEGIN OPENSSH PRIVATE KEY" notes.txt # two lines of context each side

Match 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/null hides permission errors.

Pentest searches

SUID and SGID binaries, with details:

find / -type f -perm /6000 -ls 2>/dev/null # SUID and SGID with details

What 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 /proc

World-writable files:

# world-writable files, skip /proc and /sys
find / \( -path /proc -o -path /sys \) -prune -o -type f -perm -0002 -print 2>/dev/null

Files the current user owns outside /proc and /home:

# files you own outside /proc and /home
find / -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 owns

Config files that contain “password”:

# config files that mention password
find / -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/null

The 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 number

grep -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 minutes

Backup files, SSH keys, and KeePass databases:

# backups, SSH keys, and KeePass databases
find / -type f \( -name "*.bak" -o -name "*.old" -o -name "id_rsa*" -o -name "*.kdbx" \) 2>/dev/null

Writable files in /etc:

find /etc -type f -writable 2>/dev/null # writable files in /etc

More checks after landing a shell: Local Enumeration.