Search code examples
linuxunixpermissionsfindowner

In Linux / Unix, is there a way to list every group ownership that appears under a specific folder path?


As the title says...I need to go through a path and list every group that owns a file under that path...without knowing the names of the groups.

Example:

MyFolder contains:
-rw-rw-rw- 1 myuser group1 107717 Feb 26 08:03 thisfile
-rw-rw-rw- 1 myuser group2 114746 Mar 1 12:53 thatfile
-rw-rw-rw- 1 myuser group3 115327 Mar 2 08:42 theirfile
-rw-rw-rw- 1 myuser group4 110555 Mar 10 10:32 myfile
-rw-rw-rw- 1 myuser group5 102131 Feb 1 15:59 stupidfile

I would need this output:
group1
group2
group3
group4
group5

Bear in mind...This needs to be a command or script of some sort...my example is simple, but the actual folders are 5-10 subfolders deep, with hundreds of thousands of files, and potentially several dozen groups.

Thanks!


Solution

  • Assuming GNU find:

    find /path/to/MyFolder -printf "%g\n" | sort | uniq -c
    

    will show all distinct groups owning files in the directory tree, and how many files each group owns.