Sort Files Numerically in Bash

Sort files numerically in bash

I would try following code. Works on my testing scenario:

ls -1 *\.flv | sort -n -k1.2

The ls lists flv files 1 on each line, sort takes first (and only one) word on each line starting on second character (start of the number). Sorts numerically

Sort files numerically by name, then by parent directory in BASH

Use / as the separator, first sort by the filename, then by the directory name:

find . -type f | sort -t/ -k3,3n -k2,2n

How to sort files numerically from linux command line

This would be my first thought:

ls -1 | sed 's/\-\([kM]\)\?\([0-9]\{2\}\)\./-\10\2./' | sort | sed 's/0\([0-9]\{2\}\)/\1/'

Basically I just use sed to pad the number with zeros and then use it again afterwards to strip off the leading zero.

I don't know if it might be quicker in Perl.

Process files in numerical order

You can use the ls option -v for this. From man ls:

-v natural sort of (version) numbers within text

If you change your inner loop to

for f in `ls -v` ; do      # iterate over files in sub-directory
term="$term $f"
done

The results from ls will be sorted ascending numerical order.

Another option is sort, from man sort:

-g, --general-numeric-sort
compare according to general numerical value

Piping the results from ls through sort -g gives the same result.

Edit

Since using the output of ls to get filenames is a bad idea, consider also using find instead, e.g.

for f in `find * -type f | sort -g`; do
...

How to do a bash sort by numerical value contained between two different delimiters in the set of strings

We can use bash sort;

ls | sort -g -t '_' -k 2
  • -g: Sort on general-numeric
  • -t: field-separator (_)
  • -k: Key, 2 means: Get second col (after -t)

Example:

-> ls -t
DataFile_42.txt DataFile_162.txt DataFile_15.txt DataFile_1.txt
->
-> ls | sort -g -t '_' -k 2
DataFile_1.txt
DataFile_15.txt
DataFile_42.txt
DataFile_162.txt

If you add the --debug option, we can visually see how -t and -k works:

-> ls | sort -g -t '_' -k 2 --debug
sort: text ordering performed using simple byte comparison
sort: key 1 is numeric and spans multiple fields
DataFile_1.txt
__
______________
DataFile_15.txt
___
_______________
DataFile_42.txt
___
_______________
DataFile_162.txt
____
________________
->

How to sort a file, based on its numerical values for a field?

Take a peek at the man page for sort...

   -n, --numeric-sort
compare according to string numerical value

So here is an example...

sort -n filename

BASH: loop files numbered by #{number} in sorted order

sort -k 1.2 -n should do the trick

-k F.C defines that input should be sorted according to field F, starting at character C. Both starting at 1

Edit: Just now I realize that my answer is pretty much the same answer to the question you linked. So definitely a duplicate

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

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

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

Terminal: SORT command; how to sort correctly?

If I understand the problem correctly, you want the "natural sort order" as described in Natural sort order - Wikipedia, Sorting for Humans : Natural Sort Order, and macos - How does finder sort folders when they contain digits and characters?.

Using Linux sort(1) you need the -V (--version-sort) option for "natural" sort. You also need the -f (--ignore-case) option to disregard the case of letters. So, assuming that the file names are stored one-per-line in a file called files.txt you can produce a list (mostly) sorted in the way that you want with:

sort -Vf files.txt

However, sort -Vf sorts underscores after digits and letters on my system. I've tried using different locales (see How to set locale in the current terminal's session?), but with no success. I can't see a way to change this with sort options (but I may be missing something).

The characters . and ~ seem to consistently sort before numbers and letters with sort -V. A possible hack to work around the problem is to swap underscore with one of them, sort, and then swap again. For example:

tr '_~' '~_' <files.txt | LC_ALL=C sort -Vf |  tr '_~' '~_'

seems to do what you want on my system. I've explicitly set the locale for the sort command with LC_ALL=C ... so it should behave the same on other systems. (See Why doesn't sort sort the same on every machine?.)



Related Topics



Leave a reply



Submit