linux - shell script to rename files in folder keeping order -
in folder have several files like
name_1.txt name_2.txt name_12.txt name_13.txt
and rename them in
name_1.txt -> name_1.txt name_2.txt -> name_2.txt name_12.txt -> name_3.txt name_13.txt -> name_4.txt
the following code partially job, doesn't keep order
#!/bin/sh num=1 file in *.txt; mv "$file" "$(printf "%u" $num).txt" let num=$num+1 done
indeed output is
name_1.txt -> 1.txt name_2.txt -> 4.txt name_12.txt -> 2.txt name_13.txt -> 3.txt
how fix name , order? thanks!
the *.txt
expands name_12.txt name_13.txt name_1.txt name_2.txt
because makes sense computer. need sort start off in our human way. ls
command can nicely natural sort ls -v *.txt
.
this means code need this...
#!/bin/sh num=1 file in $(ls -v *.txt); mv "$file" "$(printf "%u" $num).txt" let num=$num+1 done
Comments
Post a Comment