Skip to main content
Tips

Use ls Command Recursively

Here are various examples and usage of the ls command in recursive mode.

β€” Sagar Sharma

Warp Terminal

By default, when you execute the ls command, it will list the files and directories for the current working directory.

But what about files hidden inside subdirectories?

Well, there's a specific flag for the ls command that lets you find the files inside of the subdirectory.

For that, you'd need to use the -R flag with the ls command and in this tutorial, I will walk you through various (and practical) ways to use the ls command recursively.

How to use the ls command recursively

As I mentioned earlier, to use the ls command recursively, you'd need to use the -R flag with it.

So in this section, I will show you various ways to use the -R flag to give you an idea of what options you have while using the ls command recursively.

But before that, let me show you the directory structure that I'll be using throughout this guide:

Use the tree command to list files in tree format

Now, let's start with the first example.

1. List files and directories recursively

To list all the files and directories available in the current and subdirectories, all you have to do is use the -R flag as shown here:

ls -R /path/to/directory

In my case, the target directory is Recursive, so I will be using the following:

use ls command with the -R flag

The way it works is first, it will list the files and directories of the target directory (Recursive in my case) and then if there's a subdirectory present, it will list and print the content of that directory.

2. List files and directories recursively with permissions

Most users use the ls command with the -l flag to list files along with the additional information.

The good news is you can do the same while using the ls command recursively:

ls -lR /path/to/directory
Get file premissions while using the ls command recursively

But if you've never used the ls command to list the file permissions, it may look confusing to you, and in that case, you can refer to our detailed guide on file permissions:

Linux File Permissions and Ownership Explained with Examples
Linux file permissions explained in simpler terms. Also learn how to change the file permissions and ownership in Linux in this detailed beginner’s guide.

3. List hidden files recursively

To list hidden files recursively, all you have to do is pair the -R flag with the -a as shown here:

ls -aR /path/to/directory

Once you execute the command, you will see files starting with . in addition to regular files:

List hidden files recursively

4. List file recursively in human-readable sizes

By default when you use the -l flag with the ls command, it will show you the size of the file in bytes which is not human-readable in any manner.

To tackle this situation, you can use the -h flag with the -l as shown:

ls -lhR /path/to/directory
List files recursively in human-readable form

5. Sort files while listing them recursively

While listing files recursively, you may want to sort files based on their size which can easily be done using the -S flag:

ls -lhRS /path/to/directory

When you execute the above command, it will sort the files based specific to the directories. This means it is only relevant to the files for that specific directory in which the file is located:

Sort files while listing them recursively using the ls command

But if you want to sort every file available in current and subdirectories, then you will have to pair the ls command with the sort command:

ls -lhR /path/to/directory | grep "^-" | sort -k5rh
sort files while listing files recursively by pairing the ls command with the sort command

Here,

  • grep "^-": used to filter only regular files and exclude directories and other file types.
  • sort -k5rh: Sorts files based on the 5th column in reverse order and in human-readable format.

More on the ls command

Want to get the most out of the ls command? Here are some practical examples of using the ls command:

ls Command in Linux: 17 Useful Examples Explained
The ls command in Linux is one of the most used commands. But most people don’t utilize its full potential. Check out the basics as well as advanced ls command examples in Linux.

You can also sort the results of the ls command by the date and time:

Sort ls Command by Date and Time
Looking for modified files? Learn how you can sort the output of the ls command by date and time.

I hope you will find this guide helpful.

Sagar Sharma