basically tech

11 Shell stuff: more multiple file renaming goodness

Wednesday 25th October, 2006

I had an e-mail from a guy named Devon telling me yet more ways to rename multiple files. I thought they were pretty good (damn it!) and had to share these new (to me) techniques.

These alternative methods seem to require the use of the Bash shell, so some people may not wish to read any further!

remove a suffix

$ ls -l

total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 algebra.txt
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 calculus.txt
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 equations.txt
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 geometry.txt
-rw-r--r-- 1 rob rob  50 2006-10-20 13:23 matrices.txt

Note the use of the "%25" symbol to denote a suffix.

$ for FILE in *.txt; do mv -i "$FILE" "${FILE%25.txt}"; done
$ ls -l

total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 algebra
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 calculus
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 equations
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 geometry
-rw-r--r-- 1 rob rob  50 2006-10-20 13:23 matrices

change a suffix

$ ls -l

total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 algebra.txt
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 calculus.txt
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 equations.txt
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 geometry.txt
-rw-r--r-- 1 rob rob  50 2006-10-20 13:23 matrices.txt

$ for FILE in *.txt; do mv -i "$FILE" "${FILE%25.txt}.TXT"; done
$ ls -l

total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 algebra.TXT
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 calculus.TXT
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 equations.TXT
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 geometry.TXT
-rw-r--r-- 1 rob rob  50 2006-10-20 13:23 matrices.TXT

remove a prefix

$ ls -l

total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 maths_algebra
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 maths_calculus
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 maths_equations
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 maths_geometry
-rw-r--r-- 1 rob rob  50 2006-10-20 13:23 maths_matrices

For prefixes, replace the "%25" with a "#".

$ for FILE in maths_*; do mv -i "$FILE" "${FILE#maths_}"; done
$ ls -l

total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 algebra
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 calculus
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 equations
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 geometry
-rw-r--r-- 1 rob rob  50 2006-10-20 13:23 matrices

change a prefix

$ ls -l

total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 maths_algebra
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 maths_calculus
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 maths_equations
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 maths_geometry
-rw-r--r-- 1 rob rob  50 2006-10-20 13:23 maths_matrices

$ for FILE in maths_*; do mv -i "$FILE" "MATHS_${FILE#maths_}"; done
$ ls -l

total 20
-rw-r--r-- 1 rob rob 733 2006-10-20 13:23 MATHS_algebra
-rw-r--r-- 1 rob rob 194 2006-10-20 13:23 MATHS_calculus
-rw-r--r-- 1 rob rob 117 2006-10-20 13:23 MATHS_equations
-rw-r--r-- 1 rob rob 402 2006-10-20 13:23 MATHS_geometry
-rw-r--r-- 1 rob rob  50 2006-10-20 13:23 MATHS_matrices

Thanks to Devon for that!

Home