Linux - Sort files by name (no delimiters)

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

DATENUMBER.txt (no spaces).

For example, file 3 on 2015-12-09 as follows: 201512093.txt

The version sort ls option 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] doesn't work either , since I have a different file length.

Since I don't have a division between date and number, sort -t, -k doesn't work either.

As you can see, I need to sort the file list by the first 8 characters in the file names and after that by the other part of the line .

Expected Result:

 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 this ( with )? Thanks.

+6
source share
1 answer

This will make it look:

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

This defines two keys, the first of which is an 8-character key with a fixed length (field one, characters 1 to 8), and the secondary key is a numeric key starting from the field one character 9 (and ending to the end of the line).

Numerical sorting uses any number that it finds at the beginning of the key, so you don't need to get more complicated. But if you want to be more precise, you can tell sort use . to delimit fields, then use three keys:

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

This may be required using the standard POSIX standard sort utility if your file names have different extensions and you want the extensions to affect sorting. GNU sort (used on Linux) appears to use the entire key in numerical sorting, but the POSIX standard assumes that the numerical sort key consists of only the numerical part.

+10
source

All Articles