How to Limit Depth for Recursive File List?

Is there a way to limit the depth of a recursive file listing in linux?

The command I'm using at the moment is:

ls -laR > dirlist.txt

But I've got about 200 directories and each of them have 10's of directories. So it's just going to take far too long and hog too many system resources.

All I'm really interested in is the ownership and permissions information for the first level subdirectories:

drwxr-xr-x 14 root   root  1234 Dec 22 13:19 /var/www/vhosts/domain1.co.uk  
drwxr--r-- 14 jon    root  1234 Dec 22 13:19 /var/www/vhosts/  
drwxr--r-- 14 jon    root  1234 Dec 22 13:19 /var/www/vhosts/  
drwxr-xr-x 14 root   root  1234 Dec 22 13:19 /var/www/vhosts/domain2.co.uk  
drwxr-xrwx 14 proftp root  1234 Dec 22 13:19 /var/www/vhosts/  
drwxr-xrwx 14 proftp root  1234 Dec 22 13:19 /var/www/vhosts/  
drwxr-xr-x 14 root   root  1234 Dec 22 13:19 /var/www/vhosts/domain3.co.uk  
drwxr-xr-- 14 jon    root  1234 Dec 22 13:19 /var/www/vhosts/  
drwxr-xr-- 14 jon    root  1234 Dec 22 13:19 /var/www/vhosts/  
drwxr-xr-x 14 root   root  1234 Dec 22 13:19 /var/www/vhosts/domain4.co.uk  
drwxr-xr-- 14 jon    root  1234 Dec 22 13:19 /var/www/vhosts/
drwxr-xr-- 14 jon    root  1234 Dec 22 13:19 /var/www/vhosts/

EDIT:

Final choice of command:

find -maxdepth 2 -type d -ls >dirlist
1

4 Answers

Checkout the -maxdepth flag of find

find . -maxdepth 1 -type d -exec ls -ld "{}" \;

Here I used 1 as max level depth, -type d means find only directories, which then ls -ld lists contents of, in long format.

4

Make use of find's options

There is actually no exec of /bin/ls needed;

Find has an option that does just that:

find . -maxdepth 2 -type d -ls

To see only the one level of subdirectories you are interested in, add -mindepth to the same level as -maxdepth:

find . -mindepth 2 -maxdepth 2 -type d -ls

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.