$ ls |sort

$ /bin/ls |sort

$ which ls
alias ls='/bin/ls --color'

Note that the sorting is different between the two commands (ls |sort results in incorrect sorting). This must be due to the color flag, but why?

  • 1
    To sort the output of ls, in many cases, you may found useful to specify directly the -v option: ls -v . Better to avoid the deprecated and risky parsing of its output... In many modern distributions the alias of ls is directly set to /bin/ls --color=auto, as they suggest you in an answer.
    – Hastur
    Apr 23, 2019 at 10:24
  • 1
    The thing you're missing is how ANSI colour codes are signalled in a terminal. I'm not sure if it's constant or varies between terminals, but for me red can be triggered by printing \033[91m. Once you imagine the colour codes that are output by ls (normally consumed by the terminal), you can see why your sort would break (and then, the terminal strips them out so you can't see why the sort broke). I'm guessing the results were grouped by colour, then sorted by name?
    – Basic
    Apr 23, 2019 at 19:28

2 Answers 2



/bin/ls --color > file1
/bin/ls > file2

and compare content, you'll see the difference.

Aliasing ls to /bin/ls --color=auto is likely better idea, it will stop ls from using color codes when not writing directly to terminal (like when piping to next program or writing to a file).

  • 16
    @JoshM. Well, sorting should be done by ls itself, if possible. Sorting with sort is parsing, not recommended in general. Apr 22, 2019 at 19:43
  • 6
    @JoshM., rather than using /bin/ls, change your alias to what Tomak suggested and you will get the better behavior automatically.
    – John1024
    Apr 22, 2019 at 19:46
  • 5
    @JoshM. You can also use \ls for an unaliased ls or command ls for /bin/ls.
    – Freddy
    Apr 22, 2019 at 19:58
  • 10
    Yes, @Freddy, very true. But, using alias ls='/bin/ls --color' is just a recipe for trouble.
    – John1024
    Apr 22, 2019 at 20:00
  • 1
    How does this answer answer the OP's question?
    – pts
    Apr 24, 2019 at 8:12

In the sorted colored output ls|sort, we can see that the last line xyz-0.0.1-1554323568.rpm is the first line of the non-colored output. The other lines are sorted equally.

If we have at a look at the colored escape codes (non-sorted), we can see that the first line starts with a different escape code ^[[0m. This is causing the wrong order when sorted (^[[01 before ^[[0m).

$ /bin/ls --color xyz* | cat -A

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .