TypeOfNaN

How to find all files of a specific type recursively in Unix-based operating systems (Linux and Mac)

Nick Scialli
September 28, 2021

New — Check out my free newsletter on how the web works!

The find command line utility is a powerful tool for searching your filesystem in Linux and MacOS. A common use case is trying to find all files with a certain extension.

In the following example, we use the find command to find all txt files within the current directory, recursively.

find . -type f -name "*.txt"

Let’s break down how this works:

  • The first argument we provide the find command is ., which tells find to start from the current directory. if we wanted to start in a subdirectory called my_directory, our command would be find my_directory -type f -name "*.txt".
  • Next up, we have the type flag. As you might intuit, the f is for files. You can also use d for directories.
  • Finally, we have the name flag. Since we’re looking for all .txt files, we use a * wildcard to indicate that we don’t really care what the file name is so long as it ends in .txt.

Note that we don’t specify anything for searching recursively—this will be done automatically!

🎓 Learn how the web works

One of the best ways to level up your tech career is to have a great foundational understanding of how the web works. In my free newsletter, How the Web Works, I provide simple, bite-sized explanations for various web topics that can help you boost your knowledge. Join 2,500+ other learners on the newsletter today!

Signing up is free, I never spam, and you can unsubscribe any time. You won't regret it!

Sign up for the newsletter »
Nick Scialli

Nick Scialli is a senior UI engineer at Microsoft.

© 2024 Nick Scialli