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.
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
Linux sort numerically based on first column
tail -n +2 file.csv | sort -k1,2 -n -t","
should do the trick.
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
how to use Linux command Sort to sort the text file according to 4th column, numeric order?
sort -nk4 file
-n for numerical sort
-k for providing key
or add -r option
for reverse sorting
sort -nrk4 file
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
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
...
sorting files in a directory based on numerical index within file-names
You can try using sort
with the -V
option used for natural sorting of numbers within text:
for var in `ls ipsec_packet*.txt | sort -V`; do echo $var; done
Numerical sort in shell when each line does not begin with number
$ sort -nk2 foo.txt
foo 1
foo 12
foo 15
foo 110
foo 120
From the sort man page:
-k, --key=POS1[,POS2]
start a key at POS1, end it at POS2 (origin 1)
As for an answer to the first part of your question: I don't know for sure (i.e., I don't find it explicitly mentioned), but sort works by fields, with the default separator set to whitespace. So by default, it sorts on the first field only, and ignores the other fields. If you would try and combine both fields, e.g. by using -t
and setting it to some non-used character (-t$
), -n
sorts on numerical string value, which would be alphabetical (since foo
simply isn't a number). Thus, foo 110
would still come before foo 12
.
Related Topics
Difference Between $@ and $* in Bash Script
Code Snippet Managers for Linux Desktops
Bash File Is Running Fine in Windows for Testng But It Is Not Working in Linux/Mac
How to Mount a Remote Linux Folder in Windows Through Ssh
Linux Cross-Compilation for Arm Architecture
How to Check the Version of Openmp on Linux
Bash Script to Detect When My Usb Is Plugged in and to Then Sync It with a Directory
How to Get Exit Status of a Shell Command Used in Gnu Makefile
Application 'Appname' Failed to Start (Port 8080 Not Available) on Open Shift Node App
Linking Symbols to Fixed Addresses on Linux
How to Recursively Copy a Directory into Another and Replace Only the Files That Have Not Changed
Dummy Questions About Setting Up Git on Amazon Cloud Ec2
Spidev Linux Driver on Intel Atom Board
Crontab Day of the Week Syntax
Colors with Unix Command "Watch"
Fuzzy File Search in Linux Console
Rename Files in Multiple Directories to the Name of the Directory
How to Convert ".." in Path Names to Absolute Name in a Bash Script