Bash: script to remove all spaces and uppercase chars from multiple filenames

January 17th, 2012 by Leandro Morgado

I needed to remove the spaces in filenames for many files but doing it by hand was a chore. Here is a small script that did the job for me:

read -p "Are you sure you want to rename all files? (Y/N)" contflag
flag=$(echo $contflag| tr A-Z a-z)
if [ ! ${flag} == "y" ] ; then
 exit 1
for f in *; do
     file=$(echo $f | tr A-Z a-z | tr ' ' _)
     [ ! -f $file ] && mv "$f" $file
exit 0

I know this could be improved, for example, by taking a list of files as an argument, but it does what I want. Use at your own risk.

Happy file renaming! =)

Leave a Reply

You must be logged in to post a comment.