Bash renaming

CLI >
I took a look at using pure bash commands to run renaming tasks and this is what I found out, the real basics (and practically what I tend to need the most). These commands, coupled with Ranger, have replaced my need for any gui rename tool - although I do use XnView's bulk rename tool a lot for pics.

Make sure you copy a load of files to a test directory and carry out some tests before doing anything serious with renaming commands - because a slightly wrong command can see dozens of files overwritten and disappearing in an instant!


rename 's/string-to-replace/with-what/' * the * wild card will search every file in the directory and replace the string all of them.  Use extensions to be safe, if need be, e.g. *.jpg; and use the flag "g" to replace every instance on a line: 's/string/new/g'
rename 's/\.JPG/\.jpg/' * the forward-slashes escape the dots, which otherwise would be a bash pattern
rename 's/\.JPG//' * this will strip the extension off file names
rename 's/ //' * this will remove spaces from file names
rename 's/ /_/' * this will replace spaces for underlines
rename 'y/A-Z/a-z/' * this will change upper case letters to lower case in all file names
rename 's/(.*)/PREFIX$1/' *.odt
or, simpler
rename 's/^/PREFIX/' *.odt
prepends a string PREFIX to files of .odt type
rename 's/(.*)/$1SUFFIX/' beach.png
or
rename 's/$/SUFFIX/' file
appends a string SUFFIX to the file, but appends to end, e.g. beach.png_original
rename 's/\.(.*)/SUFFIX.$1/' *.png  appends a string SUFFIX to files of .png type, inside the extension, ie beach_01-03-12.png
note: if we use 's/(.*)\.(.*)/$1SUFFIX.$2/' as can be found on some web pages, if you try appending digits, say 33 and so enter "$133.$2" the file will vanish!
for file in * ; do mv "$file" "${file:0:8}INFIX${file:8}" ; done inserts a string INFIX to all file names in the dir, at character 8
for file in *; do mv "$file" "$(echo $file | sed -e 's/string-to-replace/with-what/')"; done nb, rename command may not be the best for a huge number of files, but it's not bad.  for with mv and sed can be used (as below)
rename 's/whot/what/' **/*.odt yet rename can search deep into all levels, with globstars
find . -depth -name "* *" -execdir rename 's/ /_/g' "{}" \; this will replace spaces for all files and directories, going to furthest depth first and working its way out to top of each dir hierarchy (without "-depth" when find gets to a dir with space in name it will change the name of it and not go deeper, because the dir name was changed so the paths below have changed) 
for file in *; do mv "$file" "$(echo $file | sed -e 's/^.....//')"; done
or
for file in *; do mv "$file"  "$(echo $file | sed 's/^.\{5\}//')"; done
or
for file in *; do mv "$file" "`echo $file | cut -c5-`"; done
removes here the first 5 characters of all the files names in the dir

for reads the file name, then mv writes that file a new name, which it hears from echo which puts the original file name through sed.
here the "-c5" cuts until chracter 5, so it cuts off 4 char
for file in *; do mv "$file" "$(echo $file | sed -e 's/..$//')"; done this one removes 2 characters from the back of file names, including from the very end ie extension
for file in *; do mv "$file" "$(echo $file | sed -e 's/\(.*\)[0-9a-zA-Z]\{2\}\.\([^.]*\)/\1.\2/')"; done

 this part ([^.]*\) checks to see if the . is the last one or not (ie extension)
this is the one for removing last two characters before the extension of all files in the dir.  Just a little Warning! if removing the last 2 char from all files will produce all with the same name, there will be only ONE file remaining from all of them after running this sed!  All files will write to the same name, leaving only one file.
i=0; for file in *; do mv "$file" $(printf "NAME_%0.3d.jpg" $i); i=$((i+1)); done this one took me hours to find!  it renames files in the dir with sequential numbering, just like the GUI renamers do.  Obviously, replace NAME_ and .jpg with what you like. The 3 stands for how many digits to the numbering, i=0 sets the number to start counting from, and +1 denotes the step.

See my Ranger page for more uses. With easy key-bindings, it's fast work sorting out files with Ranger - faster than GUI clicking and hitting Tab to-next-field of gui renamers probably (though perhaps not).