#!/bin/bash # Substitutes underscores for blanks in all the filenames in a directory. ONE=1 number=0 # Keeps track of how many files actually renamed. FOUND=0 for filename in * do echo "$filename" | grep -q " " # Check whether filename if [ $? -eq $FOUND ] #+ contains space(s). then fname=$filename n=`echo $fname | sed -e "s/ /_/g"` # Substitute underscore for blank. mv "$fname" "$n" let "number += 1" fi done if [ "$number" -eq "$ONE" ] then echo "$number file renamed." else echo "$number files renamed." fi exit 0