linux上文件夹中的文件所有者列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3882487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
list of file owners in folder on linux
提问by Anupam
I have a folder with many files. The files have been created by many different users. I do not know about shell scripting.
我有一个文件夹,里面有很多文件。这些文件是由许多不同的用户创建的。我不知道 shell 脚本。
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...
我可以保存 ls -l 的输出,然后使用 perl python 等解析它...
But how can i do this using shell scripting?
但是我如何使用 shell 脚本来做到这一点?
采纳答案by Dirk Eddelbuettel
A simple one is
一个简单的是
ls -l /some/dir/some/where | awk '{print }' | sort | uniq
which gets you a unique and sorted list of owners.
这为您提供了一个独特且经过排序的所有者列表。
回答by ghostdog74
stat -c "%U" *| sort -u
回答by CivFan
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, so you can in turn cull them to just the uniq
ue set of usernames.
换句话说,递归搜索 中的文件/some/dir
,并stat -c "%U"
在文件上执行(打印用户名),以尽可能少的调用stat
(-exec <cmd> {} +
语法),然后当然sort
是用户名列表,因此您可以依次将它们剔除到uniq
ue一组用户名。