Wednesday, April 13, 2011

sorting files according to their names with number

To sort the files in a directory, such as

file_2.txt
file_12.txt
file_43.txt
file_1.txt

ls command will get you, this
file_1.txt
file_12.txt
file_2.txt
file_43.txt

if you want the sorted list to look like
file_1.txt
file_2.txt
file_12.txt
file_43.txt
according to the numeric order, then you need to sort command

ls *.txt | sort -t_ -k2 -n

-t specifies the separator
-k specifies the field id
-n indicates numeric sorting

No comments:

Post a Comment

RISC-v emulator

Changes are based on https://risc-v-getting-started-guide.readthedocs.io/en/latest/linux-qemu.html For Ubuntu/Debian, in addition to sud...