Search code examples
bashfilenamesls

Is it possibly to sort files numerically when names are real numbers?


I have a files which names are real numbers, both positive and negative. I need numerical order of files, like -1.0, -0.5,0.0, 0.5, 1.0 .... to make animation from the files

I have tried

ls -v1 -- *.pgm

but the result is

0.000000.pgm
0.080000.pgm
0.160000.pgm
0.240000.pgm
0.320000.pgm
0.400000.pgm
0.480000.pgm
0.560000.pgm
0.640000.pgm
0.720000.pgm
0.800000.pgm
0.880000.pgm
0.960000.pgm
1.040000.pgm
1.120000.pgm
1.200000.pgm
1.280000.pgm
1.360000.pgm
1.440000.pgm
1.520000.pgm
1.600000.pgm
1.680000.pgm
1.760000.pgm
1.840000.pgm
1.920000.pgm
2.000000.pgm
-0.080000.pgm
-0.160000.pgm
-0.240000.pgm
-0.320000.pgm
-0.400000.pgm
-0.480000.pgm
-0.560000.pgm
-0.640000.pgm
-0.720000.pgm
-0.800000.pgm
-0.880000.pgm
-0.960000.pgm
-1.040000.pgm
-1.120000.pgm
-1.200000.pgm
-1.280000.pgm
-1.360000.pgm
-1.440000.pgm
-1.520000.pgm
-1.600000.pgm
-1.680000.pgm
-1.760000.pgm
-1.840000.pgm
-1.920000.pgm
-2.000000.pgm

How can I do it?


Solution

  • You're looking for sort -g "general numeric sort"

    mapfile -t sorted_files < <(printf '%s\n' *.pgm | sort -g)
    declare -p sorted_files