2011-09-07 13 views
13

有沒有辦法只獲取文件的所有者和組,並由unix shell中的空格分隔?在unix中查找文件的所有者

我想寫一個腳本來查找目錄中所有文件的所有者並打印它(以特定格式,不能使用ls -la)。

回答

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

這會做到這一點

+0

這是什麼'-tr'? – sanmai

+0

您可以添加|排序| uniq的,如果你只希望不同的用戶/組,而不是一個長長的清單與重複 – CPJ

+0

的TR只是我的手指抽動出於習慣 - 它通過一次訂購他們,相反 – CPJ

1
ls -l | cut -f3,4 -d" " | tail -n +2 
2

GNU發現有-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。

10

使用stat命令,只要您的UNIX版本:

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

,或者做此操作的所有目錄中的文件和打印每個文件的名字:

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