Use ls Command Recursively
Here are various examples and usage of the ls command in recursive mode.
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:
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:
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
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:
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:
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
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
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:
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
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:
You can also sort the results of the ls command by the date and time:
I hope you will find this guide helpful.