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

Était-ce utile?

La 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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top