Linux Commands

Linux Find Recursive

“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 apt-get install tree
$ 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:

$ tree /var/logs

To find all the files and directories, including hidden files, you can use the -a flag with the tree command:

$ sudo tree -a /path/to/directory

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:

$ sudo find / -name access.logs

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:

$ sudo find . -name “*.txt”

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

$ sudo apt-get install fd-find

REHL/Fedora

$ sudo yum install fd-find

Arch

$ sudo pacman -S fd

openSUSE

$ sudo zypper in fd

Once installed, you can search for a file by running the fd command followed by the target filename.

$ fd 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:

$ fd '^.*rc$'

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:

$ cd /var/log
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:

$ sudo fd -e py

The command will recursively search for all files ending with the .py extension.

To search for tar archives and extract them, run:

$ fd -e tar -x tar -xvf

Conclusion

This tutorial provides you with unique ways of recursively finding files and directories in your Linux system.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list