Search code examples
unixls

How to use ls to ascii sort directory contents with symbols first then alphabetically


I'd like to use ls command to sort a directory but organize the results similar to Windows, with file or directory names beginning with symbols on top -- or like the order in $ man ascii.

Right now I get:

$ /bin/ls -al

total 16
drwxrwxr-x.  2 yasi  yasi   4096 Jan  7 13:58 .
drwxrwxrwt. 18 root root 12288 Jan  7 14:50 ..
-rw-rw-r--.  1 yasi  yasi      0 Jan  7 13:23 abc
-rw-rw-r--.  1 yasi  yasi      0 Jan  7 13:23 bac
-rw-rw-r--.  1 yasi  yasi      0 Jan  7 13:23 _bac
-rw-rw-r--.  1 yasi  yasi      0 Jan  7 13:58 ~bbc
-rw-rw-r--.  1 yasi  yasi      0 Jan  7 13:23 #bca
-rw-rw-r--.  1 yasi  yasi      0 Jan  7 13:23 cab

I would like:

drwxrwxr-x.  2 yasi  yasi   4096 Jan  7 13:58 .
drwxrwxrwt. 18 root root 12288 Jan  7 14:50 ..
-rw-rw-r--.  1 yasi  yasi      0 Jan  7 13:23 #bca
-rw-rw-r--.  1 yasi  yasi      0 Jan  7 13:23 _bac
-rw-rw-r--.  1 yasi  yasi      0 Jan  7 13:23 abc
-rw-rw-r--.  1 yasi  yasi      0 Jan  7 13:23 bac
-rw-rw-r--.  1 yasi  yasi      0 Jan  7 13:23 cab
-rw-rw-r--.  1 yasi  yasi      0 Jan  7 13:58 ~bbc

Solution

  • You will need to set the locale in the shell. Try the following:

    export LC_ALL="C"
    

    This will sort as per your desire.

    You can verify the current settings by using the command locale.