List directories first with MinGW ls

3

1

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?

ewok

Posted 2012-12-04T18:00:00.760

Reputation: 2 743

Answers

1

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.

Scott

Posted 2012-12-04T18:00:00.760

Reputation: 17 653