[英]find all users that own files in a folder in Linux using bash
I am trying to find all the users that own files in a specific path.我正在尝试查找在特定路径中拥有文件的所有用户。 Right now I am using
find
with sort
and uniq
but if there are a lot of files/folders it can take a long time.现在我正在使用带有
sort
和uniq
find
但如果有很多文件/文件夹可能需要很长时间。 I was wondering if there is a faster/better way?我想知道是否有更快/更好的方法?
$ sudo find / -xdev -type f -printf "%u\n" | sort | uniq
man
root
user1
user2
user3
Per you comment, when thinking of locations on a Linux/Unix system where a list of unique user ID's can be obtained more efficiently than running a find
on /
piped to sort | uniq
根据您的评论,当考虑在 Linux/Unix 系统上的位置时,可以比在
/
管道上运行find
更有效地获取唯一用户 ID 的列表以进行sort | uniq
sort | uniq
, the system password file would be a much better source of the information. sort | uniq
,系统密码文件将是更好的信息来源。
For example to get a sorted unique list of all user accounts on the system you can use:例如,要获取系统上所有用户帐户的排序唯一列表,您可以使用:
$ awk -F: '{print $1}' /etc/passwd | sort
or if you prefer sed
, then或者如果你更喜欢
sed
,那么
$ sed 's/:.*$//' /etc/passwd | sort
Either will be orders of magnitude faster that piping the results of find
to sort | uniq
与将
find
的结果进行sort | uniq
速度要快几个数量级sort | uniq
sort | uniq
. sort | uniq
。
Good luck with your scripting.祝你的脚本好运。
Using your find
command can be done in a "better way."可以以“更好的方式”使用您的
find
命令。 Specifically since you are actually wanting the ownership of files as a user list but limited to a specific path.特别是因为您实际上希望文件的所有权作为用户列表但仅限于特定路径。 Your slash
/
after the find command is starting the search from the top. find 命令之后的斜杠
/
将从顶部开始搜索。 So you will the results of everything from the top down.因此,您将自上而下地查看所有结果。 (Those results may be close to just listing the users.)
(这些结果可能接近于仅列出用户。)
You can either cd
to a specific directory cd /var/www/html/
您可以
cd
到特定目录cd /var/www/html/
and run the same command replacing /
with .
并运行相同的命令将
/
替换为.
find . -xdev -type f -printf "%u\n" | sort | uniq
Or drop in the path.或者掉进小路。
find /var/www/html/ -xdev -type f -printf "%u\n" | sort | uniq
Results at /var/www/html
10 users have files.结果在
/var/www/html
10 个用户有文件。
And cd
into test directory.并
cd
进入测试目录。 6 users have files. 6 个用户有文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.