Reputation: 880
I have a folder with many files. The files have been created by many different users. I do not know about shell scripting.
I need to get the list of the username (only) of the owners of the files.
I may save the output of ls -l and then parse it using perl python etc...
But how can i do this using shell scripting?
Upvotes: 11
Views: 13566
Reputation: 15092
The two solutions so far are good, but have their limitations.
This should guarantee you properly and recursively search every file in a directory tree.
sudo find /some/dir/ -exec stat -c "%U" {} + | sort | uniq
In other words, recursively search for files in /some/dir
, and execute stat -c "%U"
(print username) on the files, in as few invocations of stat
as possible (-exec <cmd> {} +
syntax), then of course sort
the list of usernames, and in turn cull them to just the uniq
ue set of usernames.
To limit the search to just regular files, then add the -type f
clause:
sudo find /some/dir/ -type f -exec stat -c "%U" {} + | sort | uniq
_______
Upvotes: 5
Reputation: 368191
A simple one is
ls -l /some/dir/some/where | awk '{print $3}' | sort | uniq
which gets you a unique and sorted list of owners.
Upvotes: 9