bash scripting – rename many files at once

let say you have many files in a directory, and you need to rename them all at once. you can go the route and do each one by one. but man that takes way too long. why not a bash script? with bash you can easily rename all your files quickly at once.

first off go into the directory you want to do this in and choose the file extensions, let say we are doing this with images this will list all the *.jpg images in your directory

lanqy@lappy2000:~/$  for i in *.jpg; do echo $i; done

you can also list this way it does the exact same thing.

lanqy@lappy2000:~/$  for i in *.jpg; do echo ${i}; done

let say you want to rename a particular portion w/in the jpg image.
the wild cards can be places strategically.

lanqy@lappy2000:~/$ for i in *_blah_*.jpg; do echo $i; done

now that you have this, and you want to remove the “_blah”, test the waters with the “echo” command before the “mv” command

lanqy@lappy2000:~/$ for i in *_blah*.jpg; do echo mv "$i" "${i/_blah}"  ; done

the command below is the how you do a simple string replace

lanqy@lappy2000:~/$ do echo mv "$i" "${i/_blah}"

you are basically saying w/in $i, look for _blah and remove. if you put another slash afterwards it means replace with _meh

do echo mv "$i" "${i/_blah/_meh}"

thats all! have fun replacing files, oh yeah remove the echo when you want to execute it completely.

advertisments: if you feel compelled to click on any, please do!

Your Ad Here

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.