“ls” is a command-line utility in Unix-based operating systems that
allows users to list the contents of a directory. The “ls” command is
a versatile tool that provides users with a variety of options to
display information about files and directories.
One of the most powerful features of the “ls” command is its ability to
list directories and subdirectories recursively. Recursion is the
process of calling a function within itself, and in the case of “ls”
it means listing the contents of a directory and all its subdirectories.
Recursively listing directories is a useful tool for finding and organizing files, especially when working with large and complex file systems. This feature can help users quickly locate files in a directory hierarchy, and it can also be used to perform batch operations on files in multiple directories.
In this article, we will explore how to use the “ls” command to list
directories and subdirectories recursively. We will cover the syntax of
the command, the various options available, and some useful examples to
help you get started.
We have following directory structure:
my_directory/
├── file1.txt
├── file2.txt
├── subdir1/
│ ├── file3.txt
│ └── file4.txt
└── subdir2/
├── file5.txt
└── file6.txt
Method-1: ls -R
The ls -R command is used to list all the files and subdirectories in
a directory recursively, including all files and directories in all
subdirectories. The -R option stands for “recursive”.
# ls -R my_directory/
my_directory/:
file1.txt file2.txt subdir1 subdir2
my_directory/subdir1:
file3.txt file4.txt
my_directory/subdir2:
file5.txt file6.txt
Each directory and its subdirectories and files are listed.
Method-2: ls -lR
The ls -lR command is used to list all files and subdirectories in a
directory recursively, along with their detailed information, such as
permissions, ownership, size, and modification time. The -l option
stands for “long format”, which means that it will display each file or
directory’s detailed information.
# ls -lR my_directory/
my_directory/:
total 0
-rw-r--r-- 1 root root 0 Mar 14 10:42 file1.txt
-rw-r--r-- 1 root root 0 Mar 14 10:42 file2.txt
drwxr-xr-x 2 root root 40 Mar 14 10:43 subdir1
drwxr-xr-x 2 root root 40 Mar 14 10:43 subdir2
my_directory/subdir1:
total 0
-rw-r--r-- 1 root root 0 Mar 14 10:43 file3.txt
-rw-r--r-- 1 root root 0 Mar 14 10:43 file4.txt
my_directory/subdir2:
total 0
-rw-r--r-- 1 root root 0 Mar 14 10:43 file5.txt
-rw-r--r-- 1 root root 0 Mar 14 10:43 file6.txt
Method-3: ls -LR
The ls -LR command is used to list all files and subdirectories in a
directory recursively, with subdirectories shown before their contents.
The -L option tells ls to follow symbolic links, and the -R option
tells ls to list all files and subdirectories recursively.
# ls -LR my_directory/
my_directory/:
file1.txt file2.txt subdir1 subdir2
my_directory/subdir1:
file3.txt file4.txt
my_directory/subdir2:
file5.txt file6.txt
Method-4: ls -1R
This command lists all files and subdirectories in a directory
recursively, with each item on a separate line. The -1 option tells
ls to display each file or directory on a separate line, and the -R
option tells ls to list all files and subdirectories recursively.
Here’s an example:
# ls -1R my_directory/
my_directory/:
file1.txt
file2.txt
subdir1
subdir2
my_directory/subdir1:
file3.txt
file4.txt
my_directory/subdir2:
file5.txt
file6.txt
Method-5: ls -lLR
This command lists all files and subdirectories in a directory
recursively, with detailed information about each item. The -l option
tells ls to display detailed information about each file or directory,
and the -LR option tells ls to list all files and subdirectories
recursively. Here’s an example:
# ls -lLR my_directory/
my_directory/:
total 0
-rw-r--r-- 1 root root 0 Mar 14 10:42 file1.txt
-rw-r--r-- 1 root root 0 Mar 14 10:42 file2.txt
drwxr-xr-x 2 root root 40 Mar 14 10:43 subdir1
drwxr-xr-x 2 root root 40 Mar 14 10:43 subdir2
my_directory/subdir1:
total 0
-rw-r--r-- 1 root root 0 Mar 14 10:43 file3.txt
-rw-r--r-- 1 root root 0 Mar 14 10:43 file4.txt
my_directory/subdir2:
total 0
-rw-r--r-- 1 root root 0 Mar 14 10:43 file5.txt
-rw-r--r-- 1 root root 0 Mar 14 10:43 file6.txt
Method-6: ls -aLR
This command lists all files and subdirectories in a directory
recursively, including hidden files and directories (those whose names
begin with a dot). The -a option tells ls to show hidden files and
directories, and the -LR option tells ls to list all files and
subdirectories recursively. Here’s an example:
# ls -aLR my_directory/
my_directory/:
. .. file1.txt file2.txt subdir1 subdir2
my_directory/subdir1:
. .. file3.txt file4.txt
my_directory/subdir2:
. .. file5.txt file6.txt
Summary
In Linux, the ‘ls’ command is used to list the contents of a
directory. It can be used to list files and directories recursively.
When you use the ‘ls’ command with the ‘-R’ option, it recursively
lists all files and directories in the specified directory and its
subdirectories.
To list recursively using the ‘ls’ command, open your terminal and
navigate to the directory that you want to list. Then, enter the
‘ls -R’ command and press enter. This will display all the files and
directories in the current directory and its subdirectories.
By default, the ‘ls’ command lists files and directories in
alphabetical order. If you want to sort the output by size, time, or
some other criterion, you can use additional options with the ‘ls’
command.
In summary, the ‘ls’ command with the ‘-R’ option is an efficient
way to recursively list all files and directories in a directory and its
subdirectories in Linux.
References
man7.org - Linux manual page( ls )

![How to list recursively with 'ls'? [SOLVED]](/ls-recursively/ls-list-recursively.jpg)
