In this article, we will learn how to find files by extension in Linux.
Table of Contents
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
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>"
<Directory> can be relative path to a folder or an absolute path.
<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
- Find latest modified files in a directory in Linux (Recursively)
- Find Files containing specific Text in Linux
- Find files in directory by wildcard matching in Linux
- How to Find Files containing a string in Linux?
find /home/varun/work/ -type f -name "*.pdf"
/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"
./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
So, this is how we can use the find command to find files by extension in Linux recursively.