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
>
Tags: tips, Tips & Tricks

5th June 2009, 13:01
This will also filter out directories having ‘root’ in their name…
Maybe cleaner solution would be:
find /var ! -user root -type d -ls
5th June 2009, 13:29
@chlovechek: you are correct. This is a much better solution! thanks for your comment.
Cheers,
- Marius -
23rd July 2009, 00:18
I think this will help
tree -dfup /var | grep -v root
31st December 2009, 00:17
1. xargs is a good way to chunk especially when the return set is large [which tends to be the case for find]
2. -mount to keep within a filesystem.
as an example,
find /var -mount ! -user root -o ! -group root | xargs -n 100 chown root:root
(came here for arping -S -B ) and stayed to comment
happy new year
3rd February 2010, 19:30
[...] I ripped this straight off of ducea.com: [...]
15th February 2010, 07:48
prasana: You can do the chown inside of find
find /var -mount -type f ! -user root -exec chown root:root {} \;
4th April 2011, 02:56
Peter Manis: but this way you will start 100 times more processes, one chown for each directory. If there is more directories, one chown per 100 of them is better solution. Only works if the command can handle more arguments of course (chown does).