How to Find a File Recursively in Linux

Abid Ullah Feb 02, 2024
How to Find a File Recursively in Linux

In this Linux article, we will learn how to find files recursively in Linux. We will also see how to search for files recursively in subdirectories in the Linux system.

There are multiple ways in which we will use different Linux commands. We will learn them one by one.

Find a File Recursively in Linux

In Linux, we have directories having subdirectories and files, so when we want to find a file, it is better to use the recursive method. We use the tree command in Linux to find a file recursively.

The easiest way to see the list of files and sub directories in a directory is using the tree command in Linux. This command lists files in a directory and produces a list of files.

When we give no arguments with the tree command, it only lists the files in the current directory. When the directory arguments are provided with the tree command, it lists all the files and sub directories found in each turn.

When the listing of files and sub directories is completed, the tree command returns the total number of files and sub-directories listed in a specific directory. We will see this in the example in more detail.

Before using the tree command, you must install it using this command in the Linux terminal.

$ sudo apt install tree

After installing the tree command program, we can now use it to find files and sub directories recursively.

$ tree -a

When we use the above command in Linux, it will display all the files and sub-directories and print the total number of files and sub-directories in our Linux operating system.

Using tree Command With -a

The output of the command:

Using the tree -a to Return All Files and Subdirectories Recursively

As we can see in the above image, the tree -a command displays all the files and subdirectories and returns the total number of files and sub-directories.

Now, we use the following command if we want to find a file recursively in a specific directory.

$ tree Desktop

In the above command, we gave an argument with tree that is Desktop. In this case, the Desktop is our specified directory in which we want to find files recursively.

Running the above command in the terminal will display all the files and sub-directories in the desktop directory.

Using the tree Command With Argument - Desktop Directory

The output of the command:

Using the tree Command to Display All Files and Subdirectories in Desktop

As we can see, the tree Desktop command displays all the files and sub-directories in the Desktop directory and returns the total number of files and sub-directories as shown in the above image.

We hope you find this article helpful in understanding how to recursively find files and sub-directories using the tree command in Linux.

Author: Abid Ullah
Abid Ullah avatar Abid Ullah avatar

My name is Abid Ullah, and I am a software engineer. I love writing articles on programming, and my favorite topics are Python, PHP, JavaScript, and Linux. I tend to provide solutions to people in programming problems through my articles. I believe that I can bring a lot to you with my skills, experience, and qualification in technical writing.

LinkedIn

Related Article - Linux File