sexta-feira, 28 de junho de 2019

Unix Command - How to rename much files in directory to fixing filename?

Using shellscript with awk to change filename.

Wrong filename.: 0400000000192062019.zip
Wanted filename: 04000000001920062019.zip

First, create a mask to list files:


>$ ls -1 ????????????2062019.zip ????????????2062019.zip.fin

0400000000192062019.zip


0400000000192062019.zip.fin

Second, create a loop to work with filenames:


>$ for x in $(ls -1 ????????????2062019.zip ????????????2062019.zip.fin); do echo $x; done
0400000000192062019.zip

0400000000192062019.zip.fin

And next, using AWK to fix filename:

Testing

>$ for x in $(ls -1 ????????????2062019.zip ????????????2062019.zip.fin ); do new=$(awk 'BEGIN{file=ARGV[1]; print substr(file,1,14) "0" substr(file,15)}' $x); echo "changing filename $x to $new"; done

changing filename 0400000000192062019.zip to 04000000001920062019.zip
changing filename 0400000000192062019.zip.fin to 04000000001920062019.zip.fin


Fixing
>$ for x in $(ls -1 ????????????2062019.zip ????????????2062019.zip.fin ); do new=$(awk 'BEGIN{file=ARGV[1]; print substr(file,1,14) "0" substr(file,15)}' $x); echo "changing filename $x to $new"; mv $x $new; done

changing filename 0400000000192062019.zip to 04000000001920062019.zip
changing filename 0400000000192062019.zip.fin to 04000000001920062019.zip.fin