Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to list all of the files my WD sorted alphabetically by the file owner name. Does ls have something for this?

share|improve this question

migrated from stackoverflow.com Oct 13 '11 at 18:01

This question came from our site for professional and enthusiast programmers.

add comment

5 Answers 5

up vote 2 down vote accepted

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.

share|improve this answer
    
Is this recursive? –  Michel Oct 13 '11 at 16:17
    
@Michel: no, but I don't think that was wanted by the OP. –  bukzor Oct 14 '11 at 17:22
add comment

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

share|improve this answer
    
find: bad option -printf –  Martin Carpenter Oct 13 '11 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 '11 at 16:14
add comment

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.

share|improve this answer
    
do I even need to use find? I tried ls -l | sort -k3 and I think it worked. –  austinminn Oct 13 '11 at 16:16
    
find also processes files and folders in subdirectories –  Michel Oct 13 '11 at 16:18
add comment

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
share|improve this answer
add comment
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:~$
share|improve this answer
add comment

Your Answer

 
discard

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