Find Files by Extension in Linux

In this article, we will learn how to find files by extension in Linux.

Find files by Extension recursively in Linux

If you are using Linux, then you might have encountered a situation, where you need to search for certain types of files in a directory reccursiverly. For exxample, suppose we want to find all the pdf files in a specific folder, and in its nested folders. Basically we need to search for a file by its extension in a directory recursively.

We can do that using the find command from Linux. Syntax for using the find command for searching files by extension is,

find <Directory> -type f -name "*.<extension>"

The <Directory> can be relative path to a folder or an absolute path.
The <extension> is the extension of file like, “pdf”, “txt”, or “py” etc.

It will look for all the files with given extension in the given directory. It look recursively, it means it will cover all the nested folders inside this directory.

Examples of searching files by extension in Linux

Let’s see some examples,

Find all “pdf” files in a specific folder

Here, we will try to search for all the pdf files in a specific folder “/home/varun/work/”. This command will look for pdf files, in this folder and all its nested folders.

find /home/varun/work/ -type f -name "*.pdf"

Output:

/home/varun/work/articles/sample1.pdf
/home/varun/work/articles/examples/test2.pdf
/home/varun/work/articles/examples/test3.pdf
/home/varun/work/articles/examples/20_jan_2023/results.pdf

Find Files by Extension in Current folder

To find files in current folder reccursively, you need to provide the “.” as directory, while using the find command. Here, we will try to search for all the cpp files in current folder recursively.

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

Output:

./examples/vector/done/ex1_1.cpp
./examples/vector/done/ex13_4.cpp
./examples/20_jan_2023/vector/done/ex4_2.cpp
./examples/20_jan_2023/vector/done/ex5_2.cpp

Summary

So, this is how we can use the find command to find files by extension in Linux recursively.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top