How to show recursive directory listing on Linux or Unix

last updated in Categories , , ,

I am a new Linux system user. How do I see a recursive directory listing on macOS Unix system? In Linux, how can I get a recursive directory listing?

Introduction – If you like to receive the list, all directories and files recursively try the following commands.

What is a recursive listing of files?

Recursive means that Linux or Unix command works with the contains of directories, and if a directory has subdirectories and files, the command works on those files too (recursively). Say you have a directory structure as follows:
tree dir1
Recursive directory listing on Linux or Unix
From the above outputs, it is clear that running the tree dir1 gives a list of dir1 directory and its subdirectories and files. The base directory is dir1. Then you have all the child directroies. All all the child directories have additional files and directories (say grand directories), and so on. You can use various Linux commands going through each directory recursively until it hits the end of the directory tree. At that point Linux commands come back up to a branch in the tree a does the same thing for any sub-directories if any.

How to get a recursive directory listing in Linux or Unix

Try any one of the following command:

  1. ls -R : Use the ls command to get recursive directory listing on Linux
  2. find /dir/ -print : Run the find command to see recursive directory listing in Linux
  3. du -a . : Execute the du command to view recursive directory listing on Unix

Let us see some examples to get a recursive directory listing in Unix or Linux systems.

Linux recursive directory listing command

Type the following command:
ls -R
ls -R /tmp/dir1

In Linux, how do I get a recursive directory listing
Linux recursive directory listing using ls -R command.

Unix recursive directory listing command

Since, not all versions of Linux, macOS, *BSD, and Unix-like system have -R option for the ls command. Try to use find command:
find . -print
find /tmp/dir1 -print
find /tmp/dir1/ -print -ls

Recursive directory listing in Linux or Unix using find
Recursive directory listing in Linux or Unix using the find command

How to list all files recursively in a directory

Our final example uses the du command as follows:
du -a .
du -a /tmp/dir1/

You can also use the tree command as follows:
tree .
tree /tmp/dir1/

Unix and Linux list all files recursively in a directory

Recursively working with files

It is possible to run command recursively on files. The syntax is:
my-command-here $(find /dir/ -name 'pattern' -print)
rm -i $(find /home/nixcraft/ -name '*.bak' -print)

Of course, your can run command using find itself:
find /dir1/ -name 'pattern' -print -exec command ;
find /dir1/ -name 'pattern' -print -exec command {} ;
find /dir/2/foo/bar -name "*.pl" -exec rm -rivf {} \;
find /dir1/ -type f -name "*.doc" -exec rm -fiv {} \;
## find file recursively and delete them ##
find /dir1/ -name 'pattern' -print -delete

See “Linux / Unix: Find And Remove Files With One Command On Fly” for more info.

Conclusion

You learned how to list all files recursively in a directory under Linux, macOS, *BSD and Unix-like operating system using the ls, du, and find commands.

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.

Start the discussion at www.nixcraft.com

Historical Comment Archive

1 comment

    Still, have a question? Get help on our forum!