Skip to content

Find files: find and grep -r

Find files by name with find -name, by type with -type, and search inside many files with grep -r. Practise on a deep project folder in your browser.

Lesson 15 of 167 minSkip to the terminal ↓

Somewhere in a project there's a file you need, and you don't remember where. find walks through a directory and every directory inside it, and prints the paths that match:

find projects -name "*.md"

The first argument is where to start (. for here); after it come the tests:

TestMatches
-name "pattern"names matching the pattern (* and ? work)
-iname "pattern"the same, ignoring case
-type ffiles only
-type ddirectories only
-maxdepth 1stop one level down

Tests combine: find projects -type d -name "s*" lists directories starting with s.

Quote the pattern. Unquoted, the shell expands *.md in the current directory first, and find gets the wrong question (or an error saying "paths must precede expression").

Searching inside files

find looks at names. To look inside files, use grep with -r (recursive), which searches every file under a directory:

grep -r TODO projects

-l prints only the names of the files that match, which is handy when the matching lines are long.

Your task

Find the file named config.yml somewhere under projects and cd into the directory that holds it. Then list the files under projects that contain TODO.

  • Go to the directory that holds config.yml (not done yet)
  • List the files that contain TODO (not done yet)

Lesson 15 of 16.

learner@sandbox: ~bash (practice)
Note: Practice shell for this lesson. Type a command and press Enter (help lists them all).