在unix中查找文件的所有者

时间:2011-09-07 09:35:35

标签: unix

有没有办法只获取文件的所有者和组,在unix shell中用空格分隔?

我正在尝试编写脚本来查找目录中所有文件的所有者并打印它(以特定格式,不能使用ls -la)。

5 个答案:

答案 0 :(得分:18)

ls -l | awk '{print $3, $4 }'

那就行了

答案 1 :(得分:11)

使用stat命令(如果您的UNIX版本可用):

    $ stat -c "%U %G" /etc/passwd
    root root

或者,对目录中的所有文件执行此操作并打印每个文件的名称:

    $ stat -c "%n %U %G" *

答案 2 :(得分:3)

尝试使用stat命令:

stat -c %U file

答案 3 :(得分:2)

GNU find有-printf选项,它将为您执行此操作:

# if you want just the files in the directory, no recursion
find "$dir" -maxdepth 1 -type f -printf "%u %g\n"

# if you want all the files from here down 
find "$dir" -type f -printf "%u %g\n"

# if you need the filename as well for disambiguation, stick a %f in there
find "$dir" -maxdepth 1 -type f -printf "%u %g %f\n"

其他系统可能会将此视为gfind。

答案 4 :(得分:1)

ls -l | cut -f3,4 -d" " | tail -n +2
相关问题