How to get a list of all owners of files in a directory
Solution 1
You can use find
to print the user (owner) and group and then extract the uniq combinations e.g.
$ sudo find /var -printf '%u:%g\n' | sort -t: -u
_apt:root
avahi-autoipd:avahi-autoipd
clamav:adm
clamav:clamav
colord:colord
daemon:daemon
lightdm:lightdm
lp:lp
man:root
root:adm
root:crontab
root:lp
root:mail
root:mlocate
root:root
root:shadow
root:staff
root:syslog
root:utmp
root:whoopsie
speech-dispatcher:root
statd:nogroup
steeldriver:crontab
steeldriver:lightdm
steeldriver:steeldriver
syslog:adm
systemd-timesync:systemd-timesync
testuser:crontab
Solution 2
stat -c %U *
will list owners of all files.
This can be sorted and duplicates removed by piping it into sort -u
:
stat -c %U * | sort -u
As pointed out by steeldriver, this is not recursive. I missed that this was asked for. It can be made recursive by enabling globstar:
shopt -s globstar
stat -c %U **/* | sort -u
Alltogether, steeldriver's answer is probably better and should be the accepted answer here :)
Solution 3
You may find it more efficient to directly search for the files not owned by the user ...
find /directory ! -user username -printf "%u %p\n"
Solution 4
DIY method via Python:
#!/usr/bin/env python3
import sys,os,pwd
for f in sys.argv[1:]:
username = pwd.getpwuid(os.stat(f).st_uid).pw_name
print( ":".join([f,username]) )
This iterates over all filenames listed on command-line, gets UID of the file's owner, and using pwd module gets the username of the owner. After that, filename and username joined for pretty printing and separated via colon. Works as so:
$ ./get_owners.py /etc/*
/etc/acpi:root
/etc/adduser.conf:root
/etc/alternatives:root
. . .
Jack7076
Updated on September 18, 2022Comments
-
Jack7076 over 1 year
I am currently trying to fix my quota system. My problem is that I cannot determine if all files in a directory are owned by the same user. If possible is there a way to list the different owners of files in a directory (recursively).
e.g
get-owners-of DIRNAME
-
Byte Commander about 6 yearsSo do you want to list all owners only, or all files with their owners, or all files owned by anyone other than a specific user?
-
-
CSM about 6 yearsWon't that go over the command-line length, if there is a large number of files in the search? If so, then @steeldriver 's answer is better.
-
vidarlo about 6 years@CSM it will. Which is why I say that steeldrivers answer is a better one in many cases.
-
steeldriver about 6 years@CSM I guess if
ARG_MAX
is an issue you could doprintf '%s\0' **/* | xargs -0 stat -c %U
(sinceprintf
is a builtin, it shouldn't have the same length limitation) -
David Foerster about 6 yearsTo evaluate directory content only (and not the root directory/-ies of the search itself) add
-mindepth 1
before-printf
. And I wouldn't includesudo
in the example when OP doesn't appear to work in a context where it's required. -
kasperd about 6 yearsDoes
-t:
make a difference in this context? -
steeldriver about 6 years@kasperd good point - probably not (it might affect the sort order - but we're not really interested in that)