16

I need to list the files in my current directory sorted alphabetically by the file owner name. Does ls have a way to do this?

5 Answers 5

14

The canonical solution:

ls -l | sort -k3,3

A lone 3 (as in '-k3') would tells sort to use column 3 to the end-of-line for sorting. This lets you do more advanced sorts like ls -l | sort -k3,3 -rnk5,5, which would sort your files first by user-name, then by size, largest first.

As always, for more information, run man sort.

2
  • Is this recursive?
    – Michel
    Oct 13, 2011 at 16:17
  • @Michel: no, but I don't think that was wanted by the OP.
    – bukzor
    Oct 14, 2011 at 17:22
5

I would use find -printf "%u %h/%f\n" | sort

2
  • find: bad option -printf Oct 13, 2011 at 16:12
  • find --version find (GNU findutils) 4.4.2 Copyright (C) 2007 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Eric B. Decker, James Youngman, and Kevin Dalley. Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS() CBO(level=0)
    – Michel
    Oct 13, 2011 at 16:14
1

I don't think there's anything built in, but you can pipe together a bit of a hack:

find . -ls | sort -k5

This works because on my installation the fifth column (-k5) of output from find is the username. Clearly this isn't portable.

2
  • do I even need to use find? I tried ls -l | sort -k3 and I think it worked.
    – austinminn
    Oct 13, 2011 at 16:16
  • find also processes files and folders in subdirectories
    – Michel
    Oct 13, 2011 at 16:18
1

ls -l | awk '{print $3"\t\t"$9}' | sort would also do it. The first column is the username, the second is the file/directory name:

[ 09:20 jon@host /home ]$ ls -l | awk '{print $3"\t\t"$9}' | sort

bettina         bettina
caldavd         caldavd
davical_app     postgres
davical_dba     davical_dba
istat           istat
jared           jared
jason           jason
jon             jon
jon             repo
root            lost+found
root            SCN_RepositoryB.tar.gz
tomcat          tomcat

This would show just the file/dir names (sorted by owner):

[ 09:24 jon@host /home ]$ ls -l | awk '{print $3"\t\t"$9}' | sort | awk '{print $2}'

bettina
caldavd
postgres
davical_dba
istat
jared
jason
jon
repo
lost+found
SCN_RepositoryB.tar.gz
tomcat

And this would show them sorted but on a single line:

[ 09:26 jon@host /home ]$ ls -l | awk '{print $3"\t\t"$9}' | sort | awk '{print $2}' | tr "\n" " "
bettina caldavd postgres davical_dba istat jared jason jon repo lost+found SCN_RepositoryB.tar.gz tomcat
0
ls -l | sort -k3

No need to specify 3 after -k3

kracekumar@python-lover:~$ ls -l /tmp | sort -k3  
total 36
drwx------ 2 gdm        gdm        4096 2011-10-14 08:36 orbit-gdm
drwx------ 2 gdm        gdm        4096 2011-10-14 08:36 pulse-    2L9K88eMlGn7
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 keyring-4O5hSc
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 pulse-SBBBfzrceWvC
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 ssh-UkESZoNj1595
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 virtual-kracekumar.5D8Mlv
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:43 orbit-kracekumar
drwx------ 2 root       root       4096 2011-10-14 08:36 pulse-PKdhtXMmr18n
drwxr-xr-x 3 www-data   www-data   4096 2011-10-14 08:36 www-data-temp-aspnet-0
kracekumar@python-lover:~$ ls -l /tmp | sort -k3,3
total 36
drwx------ 2 gdm        gdm        4096 2011-10-14 08:36 orbit-gdm
drwx------ 2 gdm        gdm        4096 2011-10-14 08:36 pulse-2L9K88eMlGn7
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 keyring-4O5hSc
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 pulse-SBBBfzrceWvC
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 ssh-UkESZoNj1595
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:36 virtual-kracekumar.5D8Mlv
drwx------ 2 kracekumar kracekumar 4096 2011-10-14 08:43 orbit-kracekumar
drwx------ 2 root       root       4096 2011-10-14 08:36 pulse-PKdhtXMmr18n
drwxr-xr-x 3 www-data   www-data   4096 2011-10-14 08:36 www-data-temp-aspnet-0
kracekumar@python-lover:~$

You must log in to answer this question.