Skip to content
Advertisement

Linux – Sort files by part of name (no delimiters)

I want to get a sorted list of files. Files have the following naming convention:

DATENUMBER.txt (without spaces).

E.g., file 3 on 2015-12-09 looks like: 201512093.txt

The version sort option of ls doesn’t help me:

ls -v:
201512183.txt
201512184.txt
201512188.txt
201512191.txt
201512195.txt
201512199.txt
2015121810.txt
2015121813.txt
2015121910.txt
2015121911.txt
2015121932.txt

sort -V, --key=1.[number] do not work too as I have different filename length.

As I have no delimeter between the date and the number, sort -t, -k does not work too.

As one can see, I need to sort list of files by first 8 symbols in filenames, and after that, by the other part of line.

The expected output:

201512183.txt
201512184.txt
201512188.txt
2015121810.txt
2015121813.txt
201512191.txt
201512195.txt
201512199.txt
2015121910.txt
2015121911.txt
2015121932.txt

How can I do it (having with )? Thanks.

Advertisement

Answer

This will do the sort:

sort -k1.1,1.8 -k1.9n

That defines two keys, the first one being a fixed-length 8-character key (field one, characters 1 through 8), and the secondary key being a numeric key starting at field one character 9 (and extending to the end of the line).

Numeric sorting uses whatever number it finds at the beginning of the key, so you don’t need to get more sophisticated. But if you want to be more precise, you could tell sort to use . to delimit fields, and then use three keys:

sort -t. -k1.1,1.8 -k1.9,1n -k2

You might need this with a POSIX-standard sort utility, if your filenames have varying extensions and you want the extensions to affect the sort. GNU sort (used on Linux) appears to use the entire key in a numeric sort, but the POSIX standard suggests that a numeric sort key consists only of the numeric part.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement