Finding files with find

Beginner · 8 min

Search a tree by name, type, age and size with find, run commands on matches with -exec, and avoid the quoting and -o traps. BSD vs GNU notes.

Before thisListing and inspecting filesGlobbing and wildcards

Veronica Explains · 8 min

Finding files with find

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Veronica ExplainsReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • find PATH TESTS ACTION walks a whole tree. Tests such as -name, -type, -mtime and -size filter, and with no action it prints each match.
  • Quote the pattern after -name. Unquoted, the shell expands it against the current directory first and find searches for the wrong thing.
  • Tests combine with AND by default. Use -o for OR inside escaped parentheses, because without them -print binds to only one side.
  • -exec CMD {} ; runs the command once per file and -exec CMD {} + batches many files into one call. -print0 with xargs -0 is the safe pipe form.
  • BSD find on macOS needs an explicit path, has no -printf, and uses -perm +111 where GNU uses -perm /111, so check before copying a Linux one-liner.

Globs only see one directory

A glob like *.js matches names in the directories you give it. To find every .js file anywhere under a project, including three levels down and in folders you’ve forgotten, you need something that walks the tree. That is find.

The shape is: find WHERE TESTS ACTION. WHERE is one or more starting directories. Tests are conditions each file must meet. The action says what to do with matches, and if you give none, find prints each path. I ran everything in a small project tree: src/, node_modules/, data/, logs/ and a README.

$ find . -name '*.js' | sort
./node_modules/pkg/index.js
./src/app.js
./src/lib/util.js

find doesn’t sort its output, so I piped through sort for stable examples.

Quote the pattern

This is the trap that catches everyone once. -name expects a pattern that find applies to each name. If you leave it unquoted, the shell sees *.js first, as a glob, and expands it. I added a top.js to the current directory and ran:

$ find . -name *.js
./top.js

The shell turned that into find . -name top.js, so none of the nested files matched. Quoting ('*.js') hands the pattern to find untouched, as the previous lesson on globbing set up. -iname is the case-insensitive form: find . -iname 'readme*' found ./README.md.

Common tests

  • -type f regular files, -type d directories, -type l symlinks.
  • -name, -iname, -path (matches the whole path, like -path '*/lib/*').
  • -size +1k larger than one KB (units round up on BSD), -empty for zero-length files or empty directories.
  • -maxdepth N limits depth. find . -maxdepth 1 -type f listed only ./README.md and ./empty.txt.
  • -mtime, -newer FILE, -newermt DATE for modification times.

Time tests have an off-by-one that bites. -mtime +1 means “more than one whole day old”, counted after rounding down. With the date 2026-11-23, a file last changed on 20 November was found by -mtime +1, while one from 22 November at noon was not, because it was 15 hours old, which rounds down to zero days. For anything finer, use -mmin or compare to a reference with -newer.

$ find . -type f -mtime +30
./data/people.csv
./src/app.js
$ find . -type f -newermt 2026-11-21 | sort
./README.md
./a b.txt
./data/users.json
./empty.txt
./logs/access.log
./node_modules/pkg/index.js

And, or, not, and prune

Tests are joined with AND by default. -not (or !) inverts one. OR needs -o, and parentheses, which must be escaped from the shell:

$ find . -type f \( -name '*.js' -o -name '*.md' \) | sort
./README.md
./node_modules/pkg/index.js
./src/app.js
./src/lib/util.js

Leave the parentheses off and the answer changes quietly:

$ find . -name '*.js' -o -name '*.md' -print
./README.md

Printing was attached to the .md side only. The .js side matched but had no action. The cleanest way to skip a directory is -prune: “if it’s node_modules, don’t go in, otherwise carry on”.

$ find . -name node_modules -prune -o -name '*.js' -print | sort
./src/app.js
./src/lib/util.js

Doing something with the results

-exec runs a command per match. {} stands for the file name. The terminator matters: \; means “once per file”, while + means “pack as many as fit into one command”:

$ find . -name '*.js' -exec wc -c {} \;
       2 ./node_modules/pkg/index.js
       2 ./src/app.js
       2 ./src/lib/util.js
$ find . -name '*.js' -exec wc -c {} +
       2 ./node_modules/pkg/index.js
       2 ./src/app.js
       2 ./src/lib/util.js
       6 total

The + form started wc once, and wc itself printed the total. That’s fewer processes, a real difference over thousands of files. For other pipelines, names may contain spaces or newlines, so use null separators: find . -name '*.js' -print0 | xargs -0 ls -l. I ran a file named a b.txt through find ... -print0 | xargs -0 -n1 echo "got:" and it arrived whole.

-delete removes matches: find . -name '*.bin' -delete removed my build/big.bin. Test the expression with -print first and only then add -delete. I did not test putting -delete before the tests, which is the documented way to wipe a tree by accident.

BSD find is not GNU find

Linux tutorials assume GNU find. On macOS I hit these:

$ find -name '*.md'
find: illegal option -- n
usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
$ find . -name README.md -printf '%p %s\n'
find: -printf: unknown primary or operator

BSD find requires a starting path (GNU defaults to .) and has no -printf (use -exec stat -f ... or -ls). For permission tests, -perm -u=x and -perm +111 (“any execute bit”) both found my executable file here. GNU spells the second one -perm /111, from its documentation, which I didn’t run. Regular expressions need find -E on BSD with the flag before the path: find -E . -regex '.*\.(js|md)' matched four files. I did not run any of the GNU forms.

About the video

Veronica Explains’ “Demystifying “find” and “find -exec” …Lil’ Linux Lesson!” was published 17 May 2024 and runs 8 minutes 12 seconds. The description gives chapters: why find matters (0:00), “Basic use of find, both GNU and BSD variants!” (1:24), -exec (2:46), other parameters such as -iname, -type, -user, -group, -perm and -size (4:56), and “Why does find -exec end with a plus?” (6:53). English per its title and description, and it has real captions. It says it is not sponsored. I couldn’t read the transcript, so I can’t confirm it warns about quoting patterns, -o precedence or -prune. Those are my additions. Of everything in this group it is the best match for the BSD and GNU split, which the chapter list suggests it covers. Learn Linux TV’s 26-minute treatment is the longer alternative.

What I did not run

GNU find on Linux, -exec with sh -c, -ok, -user and -group, and -execdir, which several guides recommend as safer.

Next

You now handle files by name and by tree. The next group is about the data flowing through commands, starting with stdin, stdout and stderr.

Check yourself

  1. 01You run find . -name *.js in a directory that contains one top-level file, top.js. Why does the search miss the files in subdirectories?

    The shell expands *.js to top.js before find starts, so find actually runs with -name top.js. Quote it: find . -name '*.js'.

  2. 02What does find . -name '*.js' -o -name '*.md' -print print?

    Only the .md files. Without parentheses the implicit AND binds -name '*.md' with -print, and the .js side has no action. Write \( -name '*.js' -o -name '*.md' \) -print.

  3. 03How do you skip node_modules while searching for .js files?

    find . -name node_modules -prune -o -name '*.js' -print. -prune stops descent into the directory, and -o continues with the real search.

  4. 04File names may contain spaces. How do you pass find's results safely to another command?

    Use -exec cmd {} + or find ... -print0 | xargs -0 cmd. Null bytes cannot appear in a name, so splitting on them is safe.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark