开发者

sorting ls-l owners in Unix

开发者 https://www.devze.com 2023-01-24 02:48 出处:网络
I want to sort the owners in alphabe开发者_JAVA技巧tical order from a call to ls -l and cannot figure out a way to do it. I know something like ls-l | sort would sort the file name but how do i sort t

I want to sort the owners in alphabe开发者_JAVA技巧tical order from a call to ls -l and cannot figure out a way to do it. I know something like ls-l | sort would sort the file name but how do i sort the owners in order?


The owner is the third field, so use -k 3:

ls -l | sort -k 3

You can extend this idea to sorting based on other fields, and you can have multiple -k options. For instance, maybe you want to sort by owner, and then size in descending order:

ls -l | sort -k 3,3 -k 5rn


I am not sure if you want only the owners or the whole information sorted by owner. In the former case superfo's solution is almost correct. Additionally you need to remove repeating white spaces from ls's output with tr because otherwise cut that uses them as a delimiter won't work in all directories.*

So in the end you get this:

ls -l | tr -s ' ' | cut -d ' ' -f 3 | sort | uniq

*Some directories have a two digit value in the second field and all other lines with a single digit get an additional whitespace to preserve the layout.


How about ...

ls -l | cut -d ' ' -f 3 | sort | uniq


Try this:

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

It will print the user name, the group name and the file name. (File name cannot contain spaces)

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

This will print the user name, group name and the full ls -l output, sorted by the user name first, then the group name, then what ls -l prints first

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号