← Back to Bash Course | Chapter 9: File Testing & Filesystem | Lesson 7 of 13

The find Command

find is like a scavenger hunt through every room in the house. You describe what you are looking for, like its name or type, and it searches all the folders for you.

In this page:

  1. The find Command
Syntax
bash
find directory -name "pattern" -type f|d

The find Command

find searches a directory tree for files and directories matching criteria like name, type, or modification time. find . -name "*.txt" searches the current directory recursively for files ending in .txt.

It's far more powerful than simple globbing for searching nested directory structures.

Note: find . -name "*.log" -mtime -1 finds .log files modified in the last day — great for log cleanup scripts.

Example: The find Command

bash
#!/bin/bash
mkdir -p search_demo
touch search_demo/a.txt search_demo/b.log search_demo/c.txt

find search_demo -name "*.txt"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting to quote patterns, as in find . -name *.txt, which the shell expands first.
  2. Putting options in the wrong order, such as -maxdepth after tests, which gives a warning.
  3. Using -exec without ending it with \; or +.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.