Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I list directories with ls and sort them by their owner and group?

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer
s/row numbers/column numbers/ – Dennis Williamson May 14 '10 at 11:20
Thanks, corrected. – petersohn May 20 '10 at 15:59

As petersohn said, something similar to:

  • ls -l | awk '{print $3, $4, $8, $0}' | sort | column -t
    added the $8 and the column -t for pretty print

Or even better:

  • ls -l | sort -k 3 - sorts by owner and by default sorts the next field (group) and on
  • ls -l | sort -k 4,4 -k 3 - sorts by group and then by owner
  • ls -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.

share|improve this answer
To sort by group and then by owner I had to add -b to ignore leading blanks, otherwise the list would be sorted by the string length of the owner name. – Stefan Schmidt May 27 '13 at 20:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.