Linux Tips: get the list of subdirectories with their owner & permissions and full paths

I needed to get a list of all the subdirectories that were owner by some other user than root under /var and their permissions/owner with full paths. My first thought was to use ls and something like this:

ls -dlR */
drwxr-xr-x  2 root root  4096 2009-06-05 06:25 backups/
drwxr-xr-x  8 root root  4096 2009-05-11 06:02 cache/
drwxr-xr-x  2 root root  4096 2009-05-06 04:49 ec2/
drwxr-xr-x 25 root root  4096 2009-05-25 14:55 lib/
...

will show the subdirectories just as I needed but only at one level. Using // would show the next level, etc. This obviously is not a solution and unfortunately I had found no other way to do this with ls. Using:

ls -alR | grep ^d
drwxr-xr-x 15 root root  4096 2009-05-11 06:02 .
drwxr-xr-x 22 root root  4096 2009-06-03 15:02 ..
drwxr-xr-x  2 root root  4096 2009-06-05 06:25 backups
drwxr-xr-x  8 root root  4096 2009-05-11 06:02 cache
drwxr-xr-x  2 root root  4096 2009-05-06 04:49 ec2
drwxr-xr-x 25 root root  4096 2009-05-25 14:55 lib
....

works somehow, but since I don’t have the full paths this is useless.

Maybe this can be done with ls, but since I have not found a way to do this, I turned to find. Find allows me very simple to get the list of subdirectories with their full paths:

find /var -type d
/var
/var/backups
/var/lib
/var/lib/ucf
/var/lib/ucf/cache
/var/lib/vim
/var/lib/vim/addons
/var/lib/php5
/var/lib/iptraf
/var/lib/mysql-cluster
/var/lib/collectd
...

and to get also the owner/permissions we can get help from ls:

ls -dl `find /var -type d`
drwxr-xr-x 15 root     root      4096 2009-05-11 06:02 /var
drwxr-xr-x  2 root     root      4096 2009-06-05 06:25 /var/backups
drwxr-xr-x  8 root     root      4096 2009-05-11 06:02 /var/cache
drwxr-xr-x  3 www-data www-data  4096 2009-05-11 06:02 /var/cache/apache2
drwxr-xr-x  2 www-data www-data  4096 2008-09-08 05:08 /var/cache/apache2/mod_disk_cache
drwxr-xr-x  3 root     root      4096 2009-06-03 09:32 /var/cache/apt
drwxr-xr-x  3 root     root      4096 2009-06-03 09:32 /var/cache/apt/archives
drwxr-xr-x  2 root     root      4096 2009-06-03 09:32 /var/cache/apt/archives/partial
drwxr-xr-x  2 root     root      4096 2009-06-05 06:25 /var/cache/apt-show-versions
drwxr-xr-x  2 root     root      4096 2009-06-03 09:32 /var/cache/debconf
drwxr-xr-x  2 root     root      4096 2009-06-05 06:25 /var/cache/locate
drwxr-sr-x 42 man      root      4096 2009-06-05 06:25 /var/cache/man
...

And finally the oneliner that gives us all the folders that are owned by some other user as root:

ls -dl `find /var -type d` | grep -v root
drwxr-xr-x  3 www-data www-data  4096 2009-05-11 06:02 /var/cache/apache2
drwxr-xr-x  2 www-data www-data  4096 2008-09-08 05:08 /var/cache/apache2/mod_disk_cache
drwxr-s---  2 mysql    adm       4096 2009-06-05 06:25 /var/log/mysql
drwxr-sr-x  2 news     news      4096 2009-05-06 04:49 /var/log/news

time for awk now ;)

Update: as per the comment bellow from chlovechek a much cleaner solution is:

find /var ! -user root -type d -ls
comments powered by Disqus