find: search a directory tree
find looks through a directory and everything inside it. The first argument is where to start; the rest describe what to look for. With no conditions, it lists everything:
find projects/websiteIn the terminal
student@vulntech:~$ find projects/website projects/website projects/website/css projects/website/css/style.css projects/website/js projects/website/js/app.js projects/website/index.html projects/website/about.html
Find by name
find projects -name main.pyIn the terminal
student@vulntech:~$ find projects -name main.py projects/app/main.py
Use wildcards in -name, but put the pattern in quotes so that find, not the shell, expands it:
find projects -name "*.conf"In the terminal
student@vulntech:~$ find projects -name "*.conf" projects/app/config/settings.conf projects/app/config/database.conf
-iname ignores upper and lower case:
find projects -iname "readme*"In the terminal
student@vulntech:~$ find projects -iname "readme*" projects/app/README.md
Find by type
-type f finds only files, -type d only directories:
find projects -type dIn the terminal
student@vulntech:~$ find projects -type d projects projects/website projects/website/css projects/website/js projects/app projects/app/config
Find by size
-size +1M means bigger than 1 megabyte, -size -10k smaller than 10 kilobytes:
find projects -size +1MIn the terminal
student@vulntech:~$ find projects -size +1M projects/app/data.db
Combining conditions
Several conditions must all be true:
find projects -type f -name "*.html"In the terminal
student@vulntech:~$ find projects -type f -name "*.html" projects/website/index.html projects/website/about.html
| Condition | Finds |
|---|---|
-name "x" | Names matching x (case-sensitive) |
-iname "x" | Names matching x (any case) |
-type f / -type d | Files / directories |
-size +5M | Bigger than 5 MB (k, M, G) |
-mtime -1 | Modified in the last day |
-empty | Empty files and directories |
Tip: Searching the whole system with
find / -name ...prints Permission denied for directories you're not allowed to read. That's normal.
Remember:
find where -name "pattern". Quote the pattern.