MQ-Jr
MQ-Jr
unixcop Admin

How to list files recursively in Linux

Introduction

Recursive means that Linux command works with the contains of directories, and if a directory has files, the command works on those files too (recursively).

if you want to receive the list, all directories and files recursively try the following commands, you should read this article.

List files recursively

I have a directory like this, it’s called unixcop, it contains sub directories and they contain a files.

I can display them with tree command, so just install it on your system:

sudo dnf install -y tree   #CentOS
sudo apt install -y tree   #ubuntu

Then display the directory with

tree + [directory _name]

For my example:

tree unixcop

Some examples to get a recursive directory listing in Linux system:

  • Linux recursive directory listing using ls -R command in the current working directory.
ls -R
  • the same command but with a specific directory
ls -R unixcop
  • Recursive directory listing using find command with print option instead of -R .
find unixcop/ -print
find unixcop/ -print -ls
  • Also you can use namei command with -l (list option) to recursive a directory listing its contents but with displaying the ownership and their permissions as shown in the above command output but with another command.
namei -l [full path]
namei-l unixcop/mahmoud/qadry/script.sh
  • You can use du command recursively to calculate the directories and files usage as shown below.
du -ah unixcop/

-h for human readable.

  • To run command recursively on files. use the syntax below:
[command] $(find /dir/ -name 'pattern' -print)
rm -i $(find unixcop/ -name '*.sh' -print)

Conclusion

That’s it

We illustrated how to list file recursively in Linux.

Thank you

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook

x