Finding Files with find

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/website

In 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.py

In 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 d

In 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 +1M

In 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
ConditionFinds
-name "x"Names matching x (case-sensitive)
-iname "x"Names matching x (any case)
-type f / -type dFiles / directories
-size +5MBigger than 5 MB (k, M, G)
-mtime -1Modified in the last day
-emptyEmpty 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.

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…