Question

I have this series of files 1.dat, 2.dat, ..., 41.dat, qnd I want to rename all this files on scientific format like 0001.dat, 0002.dat, etc and all of that just using script command on unix.

Thank you for your response.

My best

Was it helpful?

Solution

This should work for you:

for file in [0-9]*.txt; do     
  mv "$file" $(printf %04d.%s ${file%.*} ${file##*.})
done

Test:

$ touch 1.txt 2.txt 3.txt
$ ls
1.txt  2.txt  3.txt
$ for file in [0-9]*.txt; do     
mv "$file" $(printf %04d.%s ${file%.*} ${file##*.})
done
$ ls
0001.txt  0002.txt  0003.txt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top