Search code examples
filebashsortinggnomels

Bash sort by file name like GNOME does


Bash is really annoying me with the way that it sorts it's files with the sort command and can't seem to find a way around this. When I sort the files with GNOME though by name, it shows it exactly how I want. I'm trying to sort by real order but the 10's and 1's mess everything up when sorting in bash. Here's how bash sorts a sample of my files:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

I see what it's doing here, but is there anywhere to sort a list like this in bash how it SHOULD be sorted? With the numbers in real numerical order instead of this strange thing that it's doing.


Solution

  • Try

    sort -k2 -t_ -n [file]
    

    That will sort numerically (-n) on the 2nd field (-k2) using _ as the field separator (-t_).

    (Shamelessly stolen from SuperUser)