← All lessons
Files and permissions · Lesson 2 of 7

Finding files

find walks a folder and everything below it, printing the paths that match. The first argument is where to start, then come the tests.

$ find . -name "*.txt"
./notes.txt
./projects/duck/todo.txt
$ find /var/log -name "*.log" -mtime -1
/var/log/syslog.log

-name matches the file name (quote any wildcard so the shell leaves it for find), and -iname ignores case. -type f keeps only files and -type d only folders. -size +100M finds files over 100 megabytes, and -mtime -7 finds files changed in the last seven days.

To search inside files rather than by name, use grep -r: grep -r "password" /etc checks every file under /etc. Add -l to list just the names of the files that match.

which says where a program lives: which python3 might print /usr/bin/python3. If it prints nothing, the program isn't on your path (more about the path in the Processes and scripts course).

Searching from / reads the whole disk, so it's slow and prints a lot of Permission denied errors for folders you can't read. Start as close to where the file probably is, and add 2> /dev/null to hide the errors.

Practice

Find every file under the current folder named notes.txt.

More info

Quick reference

find dir -name "*.txt"Files by name (-iname ignores case).
-type f, -type dOnly files; only folders.
-size +100M, -mtime -7Bigger than 100 MB; changed in the last 7 days.
grep -r word dirSearch inside every file; -l lists names.
which cmdWhere a program lives.

The other Linux courses