“In Linux, we can list files and directories recursively using various commands and tools. In this tutorial, we will give you a few commands and tools you can use to accomplish this.”
Linux Tree Command
Recursive directory listing allows you to view and find files that are nested inside other subdirectories.
One of the commands that are built for that purpose is the tree command. A tree is a simple and powerful directory listing utility. You can specify the level of depth that you wish to show in a single command.
Tree, as the name suggests, allows you to show files in nested directories in a tree-like format. It also gives you details about the specified path, including the total number of files and directories, etc.
To install it, use your system package manager:
$ sudo pacman -Sy tree
$ sudo yum install tree
You can then use the tree command followed by the path to the target directory.
For example, the command below shows all the files and directories inside the /var/logs directory:
To find all the files and directories, including hidden files, you can use the -a flag with the tree command:
Using Find
You can also use the find command followed by the target directory and the file you wish to locate.
For example, to locate the file access.logs in the root directory, use the command:
The command will start in the root directory and recursively search all the subdirectories and locate any file with the specified name.
To find any files ending with a specific extension, run the command:
The command will start in the current working directory as specified by the period and recursively search for all files ending with the .txt extension.
Using fd Command
The fd command is a free, open-source utility that is developed as an alternative to the find command.
It is very fast and user-friendly and has incredible functionality. It’s a great tool when you need to locate a file that is nested inside a series of subdirectories.
To install it, run the commands:
Debian
REHL/Fedora
Arch
openSUSE
Once installed, you can search for a file by running the fd command followed by the target filename.
The command will scan the entire filesystem and locate any file matching the specified pattern. This is very useful when you are searching for config files.
To find files that match a specific regular expression pattern:
In this case, the command will search for all files matching .rc, such as .bashrc, .zshrc, etc.
To list all files and directories recursively, run the fd command in your target directory. For example, to show all the files and directories in the /var/log directory:
fd
The command will show all the files and directories in a list. You can then pipe this result into tools such as less or grep.
To search for files matching a specific extension with the fd command, run:
The command will recursively search for all files ending with the .py extension.
To search for tar archives and extract them, run:
Conclusion
This tutorial provides you with unique ways of recursively finding files and directories in your Linux system.