How to limit depth for recursive file list?

LinuxBash

Linux Problem Overview


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/domain1.co.uk/htdocs  
drwxr--r-- 14 jon    root  1234 Dec 22 13:19 /var/www/vhosts/domain1.co.uk/cgi-bin  
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/domain2.co.uk/htdocs  
drwxr-xrwx 14 proftp root  1234 Dec 22 13:19 /var/www/vhosts/domain2.co.uk/cgi-bin  
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/domain3.co.uk/htdocs  
drwxr-xr-- 14 jon    root  1234 Dec 22 13:19 /var/www/vhosts/domain3.co.uk/cgi-bin  
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/domain4.co.uk/htdocs
drwxr-xr-- 14 jon    root  1234 Dec 22 13:19 /var/www/vhosts/domain4.co.uk/cgi-bin

EDIT:

Final choice of command:

find -maxdepth 2 -type d -ls >dirlist

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

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


Use output formatting

When the details that get shown should be different, -printf can show any detail about a file in custom format; To show the symbolic permissions and the owner name of the file, use -printf with %M and %u in the format.

I noticed later you want the full ownership information, which includes the group. Use %g in the format for the symbolic name, or %G for the group id (like also %U for numeric user id)

find . -mindepth 2 -maxdepth 2 -type d -printf '%M %u %g %p\n'

This should give you just the details you need, for just the right files.

I will give an example that shows actually different values for user and group:

$ sudo find /tmp -mindepth 2 -maxdepth 2 -type d -printf '%M %u %g %p\n'
drwx------ www-data  www-data /tmp/user/33
drwx------ octopussy root     /tmp/user/126
drwx------ root      root     /tmp/user/0
drwx------ siegel    root     /tmp/user/1000
drwxrwxrwt root      root     /tmp/systemd-[...].service-HRUQmm/tmp

(Edited for readability: indented, shortened last line)


Notes on performance

Although the execution time is mostly irrelevant for this kind of command, increase in performance is large enough here to make it worth pointing it out:

Not only do we save creating a new process for each name - a huge task - the information does not even need to be read, as find already knows it.

Solution 3 - Linux

tree -L 2 -u -g -p -d

Prints the directory tree in a pretty format up to depth 2 (-L 2). Print user (-u) and group (-g) and permissions (-p). Print only directories (-d). tree has a lot of other useful options.

Solution 4 - Linux

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

I found a easy solution while playing my fish, which fits your need perfectly.

ll `ls`

or

ls -l $(ls)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonView Question on Stackoverflow
Solution 1 - LinuxAlberto ZaccagniView Answer on Stackoverflow
Solution 2 - LinuxVolker SiegelView Answer on Stackoverflow
Solution 3 - LinuxSameerView Answer on Stackoverflow
Solution 4 - LinuxrecolicView Answer on Stackoverflow