Lab 13: find — Locating Files

Objective

Use find to locate files by name, type, size, permission, age, and owner. Combine find with -exec and -delete for powerful one-liners used in security auditing and system maintenance.

Time: 30 minutes | Level: Foundations | Docker: docker run -it --rm ubuntu:22.04 bash


Step 1: Find by Name

find /etc -name 'passwd' 2>/dev/null

📸 Verified Output:

/etc/pam.d/passwd
/etc/passwd
# Wildcard: all .conf files in /etc
find /etc -name '*.conf' 2>/dev/null | head -5

📸 Verified Output:

/etc/mke2fs.conf
/etc/sysctl.conf
/etc/ld.so.conf.d/x86_64-linux-gnu.conf
/etc/ld.so.conf.d/libc.conf
/etc/nsswitch.conf

💡 find searches recursively by default. Always redirect 2>/dev/null to suppress "Permission denied" errors when searching as non-root.


Step 2: Limit Search Depth

📸 Verified Output:


Step 3: Find by Type

📸 Verified Output:

📸 Verified Output:


Step 4: Find by Size

📸 Verified Output:

📸 Verified Output:

💡 -xdev = don't cross filesystem boundaries. Without it, find / might also search /proc and /sys virtual filesystems, causing infinite loops or enormous output.


Step 5: Find by Modification Time

📸 Verified Output:

📸 Verified Output:

💡 -mtime N: +N = older than N days, -N = newer than N days, N = exactly N days. -mmin does the same in minutes — useful for finding files changed in the last 10 minutes.


Step 6: Find by Permissions (Security Audit)

📸 Verified Output:

📸 Verified Output:


Step 7: find with -exec (Run Commands on Results)

📸 Verified Output:

📸 Verified Output:

💡 -exec command {} \;{} is replaced by the filename, \; ends the exec clause. For better performance with many files use -exec command {} + which passes all files at once.


Step 8: Capstone — Security Filesystem Audit

📸 Verified Output:


Summary

find Option
Meaning

-name '*.txt'

Match filename with glob

-type f

Files only

-type d

Directories only

-size +10k

Larger than 10 KB

-mtime +7

Modified more than 7 days ago

-mtime -1

Modified in last 24 hours

-perm -4000

Has SUID bit

-perm -002

World-writable

-maxdepth N

Limit recursion depth

-exec cmd {} \;

Run command on each result

-delete

Delete matching files

Last updated