I would like to use ls command to first show direcotries and then files. I tried

ls -la | sort -k 1

But I got a wrong order.

link|improve this question

it's because - comes before d when using sort – Nifle Feb 16 '10 at 13:32
feedback

7 Answers

up vote 5 down vote accepted

The following command will list directories first, ordinary files second, and links third.

ls -la | grep "^d" && ls -la | grep "^-" && ls -la | grep "^l"

Also, it would make a great deal of sense to create an alias for this command to save keystrokes.

Edit:

If you want directories first, and then everything that is not a directory second, use this:

ls -la | grep "^d" && ls -la | grep -v "^d"

link|improve this answer
This command doesn't list everything if there are, for example, sockets or FIFOs in the folder – Studer Feb 16 '10 at 14:01
feedback

I do so love *nix and love seeing the inventiveness that goes into some of these replies...

Mine's not nearly as fancy on GNU Linux :

alias ls='ls --color -h --group-directories-first'
link|improve this answer
unfortunately, this doesn't work on osx terminal, since -- option is not available. – MEM May 20 at 13:30
feedback

Here's a function to do this (bash or zsh): And... I'm not suggesting this is the best way, but it's the one I came up with and am using right now:

function lss
{
    # Shows directory listing with directories at the top.

    command ls  --color=always $@ | egrep '^d|total'
    command ls  --color=always $@ | egrep -v '^d|total';
}
link|improve this answer
feedback

You've got several choices, depending if you want to keep alphabetical order.

You could simply try :

ls -al | sort -k1 -r

or this, to keep alphabetic order for files with the same permissions :

ls -al | sort -k1,1 -k9,9 -r

or, as eleven81 said (but this version lists everything) :

ls -la | grep "^d" && ls -la | grep "^-" && ls -al | grep -v "^[d|-]"

link|improve this answer
This is a nicer solution IMO. Using several processes and pipes to do sorting instead of piping to sort seems kind of backwards. Especially since ls- al|sort -k1 -r works. What was missing is just the -r flag. – brice Feb 17 '10 at 13:54
feedback

To delerious010's answer, I would add that if you want old-style ordering:

LANG=C ls -la --group-directories-first

(or use LC_ALL or LANGUAGE or LC_COLLATE set to "C").

This will give something similar to:

.
..
DIR
Dir
dir
.hidden
123
UC_FILE
Uc_file
lc_file

Although, if I recall correctly, the hidden dot files originally appeared before the directories.

link|improve this answer
feedback

ls -laX will show you directories first in alphabetical order, but will screw the file list.

link|improve this answer
This would only work if you could be absolutely certain that every directory had no dots in its name. – eleven81 Feb 16 '10 at 13:43
feedback

Here is a good web site, Unix LS Command, 15 best practices

It's detailed and a lot of information is listed.

There is also the LS Wikipedia page that can give some good information.

Hope this will help!

David.

link|improve this answer
1  
The websites you list do not answer the given question. Also, your answers can be much more helpful if you quote the relevant parts of the website and site them appropriately. – heavyd Feb 16 '10 at 13:33
1  
Ok, thanks for the advice. – r0ca Feb 16 '10 at 13:36
sed 's/site/cite/' – Dennis Williamson Feb 16 '10 at 14:43
Indeed @Dennis, but my edit period has timed out... – heavyd Feb 16 '10 at 16:54
feedback

Your Answer

 
or
required, but never shown

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