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 have recently started using MinGW instead of Cygwin on my Windows 7 machine, and I am having a little issue with MinGW's ls. In Cygwin, ls has a --group-directories-first option that, obviously, causes directories to be listed before other file types. However, MinGW doesn't seem to have this option, and I can't find a substitute for it. Does one exist?

share|improve this question
add comment

1 Answer

This comes under the heading “a substitute for it”.  The following script will approximately emulate the behavior of ls –l with the difference that it groups subdirectories at the beginning of each directory listing.

#!/bin/sh -
sort_ls_output()
{
    sed -n -e '1s/^/1#/p' -e '1n' \
        -e 's/^/#/' -e 's/^#d/2#d/' -e 's/^#/3#/' -e p \
        | cat -n | sort -n -k2 -k1 | sed 's/[^#]*#//'
}

if [ $# = 0 ]
then
    ls -l | sort_ls_output
else
    for arg
    do
        echo
        if [ -d "$arg" ]
        then
            echo "${arg}:"
            ls -l "$arg" | sort_ls_output
        else
            ls -l "$arg"
        fi
    done
fi

It is a rough cut.  It doesn’t handle individual ordinary files (non-directories) on the command line the same way as ls –l does, and it doesn’t handle options (e.g., –a) at all.

share|improve this answer
add comment

Your Answer

 
discard

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.