Find Files by Name in Linux

In this article, we will discuss ways to find file by name recursively in Linux.

Suppose you are using a Linux terminal and you need to search for a file you know the name of the file but you don’t know its location in that case you can search for the file by name using the file command. It will look for the specified file in a specified folder recursively

The syntax of find command to find a file by name is as follows

find <DIRECTORY> -type f -name "<FILENAME>"

Here the <DIRECTORY> is the location where the find command will search for the file with name <FILENAME>, and it will look recursively, which means it will also look all the folders inside the specified folders.

Find file by name in a specified directory in Linux

Suppose our file is main.cpp and we want to find in a directory /home/user/documents/

We can do that using the find command for this we need to provide the full path of the directory and the final name in the find command as follows

find /home/varun/work/ -type f -name "main.cpp"

Output:

/home/varun/work/articles/python/nikhil/november/main.cpp
/home/varun/work/articles/python/list/find/main.cpp
/home/varun/work/articles/numpy/main.cpp

It will search for the given file “example.txt” in the given folder “/home/user/documents/” recursively and it will give us a list of all the matched files i.e. it will give us the complete paths of the matched files.

Find files by name in the current folder in Linux

Suppose you want to find a specific file by its name in the current folder only, then you do not need to give the absolute path of the folder you can just provide the dot as the folder path. It will look for the file with the specified name in the current folder and all its internal folders. The syntax is like this,

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

Output:

./articles/python/nikhil/november/main.cpp
./articles/python/list/find/main.cpp
./articles/numpy/main.cpp

It will return a list of matched files. but instead of returning their full path it will return the absolute path of the matched files.

Summary

We learned how to find files in Linux by name.

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