Skip to the content.

Quick rename in shell

If you do not have rename command in shell, here is some quick alternative cmds.

Given there are some files in current dir:

$ ls
0520.csv  0523.csv  526.csv  522.csv

And 526.csv and 522.csv is ill-formatted by missing leading zero. Now use cmd and see:

$ for f in ???.csv; do echo $f; done
522.csv
526.csv

Then add mv after echo cmd:

$ for f in ???.csv; do echo $f; mv $f "0$f"; done
$ ls
0522.csv  0523.csv  0525.csv  0526.csv

The mv part could be more sophisticated:

$ for f in ???.csv; do echo $f; mv $f "${f%.csv}0.csv"; done
$ ls
0523.csv  0525.csv  5220.csv  5260.csv

// add trailing zero to file name with mv $f “${f%.csv}0.csv”

$ for f in ???.csv; do echo $f; mv $f "$(echo $f | sed 's/csv/txt/')"; done
$ ls
0523.csv  0525.csv  5220.csv  5260.csv

// replace any part of file name with sed cmd and regex mv $f “$(echo $f | sed ‘s/csv/txt/’)”

END

Written on May 18, 2019