DEV Community

Cover image for Demystifying the find command
Antonin J. (they/them)
Antonin J. (they/them)

Posted on • Updated on • Originally published at antjanus.com

Demystifying the find command

📃 Cheatsheet

List all files and folders in current directory:

find
Enter fullscreen mode Exit fullscreen mode

Find files/folders that match a pattern:

find -name "*pattern*"
Enter fullscreen mode Exit fullscreen mode

Run a command per result. Substitute command with your actual command.

find . -name "*pattern*" -exec command {} +;
Enter fullscreen mode Exit fullscreen mode

What is find?

find is a command I rarely come across but it turns out, it's one of the more powerful commands in Unix-like system. Find, at its basest, lists all the files and folders in a directory. Kind of like a recursive ls. This can be piped into any command. But find also has other features like filtering and the ability to execute a command on each matching file.

Find is also sometimes used as the "backend" for fuzzy file searchers. For example, fzf uses find under the hood to employ fuzzy search in the command line (which is super useful).

Plain "find"

Using find alone will print out all directories and files located under your current working directory. This is useful for piping or other actions.

find
Enter fullscreen mode Exit fullscreen mode

Just for the sake of completeness, you can also specify a path but find will assume you want to search your current directory by default.

find /path/to/dir
Enter fullscreen mode Exit fullscreen mode

Find file or directory by pattern

To find a file or directory, one can specify its name and pass it in as an argument. Both files or directories will match.

find -name "package.json"
find -name "node_modules"
Enter fullscreen mode Exit fullscreen mode

Find will return any matches including nested directories. Note that the match isn't done on the path but on the actual directory/file name itself.

You can also specify a pattern using * as a wildcard like so:

find -name "pattern*"
find -name "*.spec.js"
Enter fullscreen mode Exit fullscreen mode

Execute a command on all found files

find allows you to execute logic on every single match it finds via the -exec flag. There are 2 ways to execute commands: once on all the results or run it per result.

Here's once for all results:

# substitute any kind of command for the keyword "command"
find . -name "*.js" -exec command {} \;
Enter fullscreen mode Exit fullscreen mode

The command has several parts. We're already familiar with the pattern matching so next comes the -exec flag. After exec, you can type out your command and end it with \;. The {} braces will be substituted with the find results.

To run the command separately per result, end your command with + instead of \;.

# substitute any kind of command for the keyword "command"
find . -name "*.js" -exec command {} +
Enter fullscreen mode Exit fullscreen mode

Other demystifying articles

Top comments (5)

Collapse
 
chrisgreening profile image
Chris Greening

I’ve been relying more and more on the command line and grep and find have been some of my favorite tools for exploring new codebases

Collapse
 
antjanus profile image
Antonin J. (they/them)

Same! I use grep all the time to find references to functions/constants when I don't know where they could be. And same with find (especially when paired with fzf)

Collapse
 
cacilhas profile image
Montegasppα Cacilhας

If you both like grep, look for The Simple Searcher (ag).

It’s gonna blow your minds.

Collapse
 
cacilhas profile image
Montegasppα Cacilhας

I’m a find-fan too, but, if you need something simpler, I recommend taking a look at fd (available for Ubuntu).

Collapse
 
miguelmj profile image
MiguelMJ

Nice