Linux › Module 5 › Lesson 3
find, which, and locate
Find files and binaries with find, which, and locate
Opening
Where did that file go?
grep searches inside file contents. Sometimes you do not have a path yet — you have a name, a type, or an age. Attackers drop tools in /tmp. Admins lose configs. You need a map of the filesystem, not a text search. find walks directories live. which and type tell you which program your shell will run. locate searches a pre-built index. Knowing the difference is how you avoid running a fake ssh from a hijacked PATH. For Cyberlium, start every find under $HOME (especially $HOME/cyberlium-lab) unless you administer the box and have a reason.
1. find: walk the tree with tests
find starts at a path and evaluates each file. find "$HOME/cyberlium-lab" -name "*.log" matches names (quote the glob so the shell does not expand it first). -type f means regular files; -type d means directories. -mtime -1 means modified in the last 24 hours; -mtime +7 means older than a week. Combine tests: find "$HOME" -type f -name "*.txt" -mtime -1 lists recent text files you likely created yourself.
Size and permission tests matter in defense labs: -size +1M, -empty, -perm -0002 (world-writable — recognize, do not create on shared systems). Actions: default prints paths; -exec cmd {} \; runs a command per match (powerful; easy to abuse). Prefer printing first. Only -delete or -exec rm when you are sure the match set is tiny and owned by you.
find / -name "id_rsa" 2>/dev/null is a classic lab hunt for SSH keys — on a system you own or have written permission to audit. 2>/dev/null hides Permission denied from directories you cannot read; it does not bypass security. Prefer starting under $HOME or a lab folder until you have a reason and authorization to search further. On a shared school host, do not wander /home/* looking for other students' keys — that is not a lab, that is snooping.
2. which vs type vs locate, and PATH
which name prints the path of the executable the shell would run (implementation varies slightly by shell/OS). type name (bash) is often more informative: alias, function, or hashed path. command -v name is a portable cousin. Use these when you suspect PATH order tricks — a malicious ~/bin/ls before /usr/bin/ls.
PATH is a colon-separated list of directories searched for bare command names. echo "$PATH" and read left to right. Putting . (current directory) early in PATH is a classic footgun on multi-user systems. After installing tools, re-check which python3 or which ssh so you know what you invoke.
locate name searches a database built by updatedb (often daily via cron). It is fast but can be stale — newly created files may be missing until the DB refreshes. locate may also be absent on minimal installs. find is live and precise; locate is a quick index when available. Neither tool authorizes reading file contents you should not see.
3. Choosing the right locator under pressure
Need contents? grep. Need a name under your tree? find $HOME …. Need which binary runs? which/type/command -v. Need a fuzzy system-wide name hint on a machine you own? locate, then verify with ls and find. Mixing them up wastes time: people locate a config, then forget to check it is the one the service actually reads.
Safety defaults for this course: find "$HOME/cyberlium-lab" … first; expand to "$HOME" if needed; only then consider system paths on a VM you own. Never -delete from / . Never find other users' homes on shared hosts. World-writable hunts (-perm -0002) are for recognizing risk on systems you administer — not for creating 777 traps.
When results are huge, narrow: -maxdepth 2, tighter -name, or pipe to head while exploring. Save interesting paths to a notes file under your lab directory. Reproducibility beats a blur of scrolling paths.
4. Wrong vs right: locate files without trespassing
Failure mode — find / as a voyeur tour. Right searches start in your home lab:
Wrong
find /home -name "*.pem" on a multi-user server to "practice CTF." find / -delete because a forum said it cleans disk. Trust locate alone without verifying the path. Put . first in PATH and run random downloaded binaries. Search classmates' directories for id_rsa screenshots.
Right
Seed files under $HOME/cyberlium-lab/m05-find, then find that tree by name, type, and mtime. Use which/type on your shell to see real binary paths. Try locate only if installed, then confirm with ls. Keep system-wide finds on owned VMs with a clear purpose. Quote globs. Print before you -exec.
5. Practical: seed, find, verify binaries
Create a small tree with .log and .txt files, age them if you like (touch), then run find tests. Compare which python3 / type python3. Optionally run locate on your own filename after you know the tool exists.
Command guide
find / which / locate starting under $HOME
YOUR VM / WSL — start finds under $HOME (cyberlium-lab)
Command — copy this
mkdir -p "$HOME/cyberlium-lab/m05-find/logs" \
"$HOME/cyberlium-lab/m05-find/notes"
cd "$HOME/cyberlium-lab/m05-find" || exit 1Command — copy this
echo 'alpha' > notes/alpha.txt echo 'beta' > notes/beta.txt echo 'line' > logs/app.log echo 'old' > logs/old.log touch -d '10 days ago' logs/old.log 2>/dev/null || touch logs/old.log
Command — copy this
echo '--- by name ---' find "$HOME/cyberlium-lab/m05-find" -name '*.log'
Command — copy this
echo '--- files only ---' find "$HOME/cyberlium-lab/m05-find" -type f
Command — copy this
echo '--- recent txt (mtime -1 ≈ last 24h; may vary) ---' find "$HOME/cyberlium-lab" -type f -name '*.txt' -mtime -1
Command — copy this
echo '--- deeper but still under HOME ---' find "$HOME" -maxdepth 3 -type d -name 'cyberlium-lab' 2>/dev/null
Optional command
Which binary will the shell run?
Command — copy this
echo "PATH=$PATH" command -v ssh type ssh 2>/dev/null || true which ssh 2>/dev/null || true
locate (optional; DB may be stale / tool may be missing)
Command — copy this
if command -v locate >/dev/null 2>&1; then locate -n 5 'm05-find' 2>/dev/null || true else echo 'locate not installed — find is enough for this lesson' fi
Save a short inventory
Command — copy this
find "$HOME/cyberlium-lab/m05-find" -type f | tee file-inventory.txt
NEVER:
Optional command
find /home -name id_rsa # on shared hosts / other users find / -delete
trust a random binary earlier in PATH without checking which
Mission: find your own needles
1) Create $HOME/cyberlium-lab/m05-find with at least one .txt and one .log. 2) Use find starting under $HOME/cyberlium-lab to list them by -name and -type f. 3) Run command -v ssh (or which ssh) and note the path. 4) Write one sentence: why find should start under $HOME for class practice. Do not search other users' homes; do not -delete from /.
Stuck? Ask Cyberlium AI Mentor
If find returns nothing, ask Cyberlium AI Mentor for a hint — not a root-wide destructive command. Try: "Hint only: why must I quote *.log in find -name, and how does -maxdepth help?" Stay under $HOME/cyberlium-lab while you debug.
You can locate files with find tests, verify binaries with which/type/command -v, understand locate's index tradeoff, and keep searches inside authorized trees — starting at $HOME. Next — Lab — Hunt Through Logs — combines pipes, grep, and disciplined evidence saving on sample data you control.
Knowledge Check
APPLY: You need files named *.log under your practice folder. Best starting command?
Multiple choice
Knowledge Check
APPLY: which python3 and /usr/local/bin/python3 disagree with what you expected after installing a package. What should you check?
Multiple choice
Knowledge Check
APPLY: True or False: Because find can start at /, class practice authorizes searching other students' home directories for SSH keys.
True or False