How can I list directories with ls and sort them by their owner and group?
Asked
Active
Viewed 3.4k times
2 Answers
9
Try this:
ls -l | awk '{print $3, $4, $8}' | sort
It will print the user name, the group name and the file name, provided that the file name doesn't contain spaces. Alternatively, you can type:
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 whatever ls -l prints first.
Note that depending on your distribution, the actual column numbers may differ. I tried mine in SUSE and coreutils version 5.2.1.
There are probably better, more elaborate solutions, but this is the simplest one, and will work most of the time.
petersohn
- 2,708
8
As petersohn said, something similar to:
ls -l | awk '{print $3, $4, $8, $0}' | sort | column -t
added the$8and thecolumn -tfor pretty print
Or even better:
ls -l | sort -k 3- sorts by owner and by default sorts the next field (group) and onls -l | sort -k 4,4 -k 3- sorts by group and then by ownerls -l | sort -k 3,3 -k 8- sorts by owner and then by filename
Note: the comma is the terminator field so 3,3 starts and end at field 3 3,5 sorts from fields 3 to 5.
vol7ron
- 505