How To List Directories In Linux or Unix Recursively

last updated in Categories , , , , ,

I‘m a new Linux and Unix-like system shell user. How can I get a recursive directory listing using command prompt on a Unix-like systems?

You can use any one of the following command to list files and directories in a recursive order on a Linux or Unix-like systems:

=> ls command

=> find command

=> du command

Let use see examples of see a recursive directory listing in Unix.

ls command examples: Get a recursive directory

Type the following command list subdirectories recursively using the ls command:

ls -R
ls -l -R
ls -R /etc/
ls -R /nas01/ | more

Sample outputs:

Fig.01: ls command get a recursive directory listing
Fig.01: ls command get a recursive directory listing

find command examples: Get a recursive directory

Type the following command:

find . -print
find . -ls
 
## or ##
find /path/to/search/ -print
find /path/to/search/ -ls

Sample outputs:

Fig.02: find command get a recursive directory listing on a Unix-like system
Fig.02: find command get a recursive directory listing on a Unix-like system

du command examples: Get a recursive directory

The syntax is as follows to see an entry for each file in a file hierarchy:

du -a .
du -a /path/to/search/
du -a /path/to/search/ | more

Sample outputs:

4	./SOURCES/nginx.suse.init
4	./SOURCES/logrotate
4	./SOURCES/nginx.init
4	./SOURCES/nginx.sysconf
752	./SOURCES/nginx-1.4.4.tar.gz
4	./SOURCES/nginx.vh.default.conf
4	./SOURCES/nginx.conf
4	./SOURCES/nginx.vh.example_ssl.conf
784	./SOURCES
12	./SPECS/nginx.spec
16	./SPECS
804	.

For a more complete discussion of the commands used in this tutorial, see the online manual pages – du(1).

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.