Weblog
Thoughts which doesn't affect the world's peace

Fast batch rename of files in bash

Whoever had to rename dozens of files in a folder knows how painfully slow that can be. Here is a one line bash script how to do this fast.

for FILE in * ; do NEWFILE=`echo $FILE | sed 's/-//g'` ; mv "$FILE" "$NEWFILE" ; done

You will have to change the regular expression in “sed”, as the current line will only remove any dashes it finds. If you change it to “s/-.*//g”, it will remove anything after the first dash it finds.

For bash 4.0 and up, a simple “any case to lower case” rename is as easy as:

for FILE in * ; mv $FILE ${FILE,,} ; done

If there is need for images to be converted, the convert command can be used. Here is an example about converting all “tif” images in a directory into jpg (according to their file extension):

for file in * ; do  filetype=${file##*.}; if [ $filetype = "tif" ]; then echo "converting $file to ${file%.*}.jpg"; convert "$file" "${file%.*}.jpg"; fi ; done

After the process finishes, you’ll have both the tif and the jpg images.

Tags: , , , , ,

3 Responses to “Fast batch rename of files in bash” »

  1. Malik Dettorre Says:

    Thanks a lot for sharing this with all of us you really know what you’re talking about! Bookmarked. Kindly also visit my website =). We could have a link exchange arrangement between us!

  2. Robbin Says:

    I will kindly decline that proposal and in fact – I had to edit your post and remove the link to your site. I disapprove the public spam and in particular – random posts on random blogs on the net (the this called SEO about which you speak in your web site as well). If you like the stuff I write in my blog (which are mostly found out during my work as a system administrator and programmer), do come back and share with me 😉

  3. Glynis Rauelo Says:

    Thank you, I have recently been on the lookout for information on this subject for ages and this blog is the best I’ve identified to date.