Skip to content

Instantly share code, notes, and snippets.

@jasondmoss
Last active December 21, 2017 15:47
Show Gist options
  • Save jasondmoss/7365594 to your computer and use it in GitHub Desktop.
Save jasondmoss/7365594 to your computer and use it in GitHub Desktop.
Bash: Replace blank spaces with underscore for file(s) in current directory.
###
## Replace blank spaces with underscore for file(s) in current directory
###
function blankRename() {
ONE=1 # For getting singular/plural right
number=0 # Keeps track of how many files actually renamed
FOUND=0 # Successful return value
# Traverse all files in directory
for filename in *
do
# Check whether filename contains space(s)
echo "$filename" | grep -q "_"
if [ $? -eq $FOUND ]
then
fname=$filename # Strip off path
n=`echo $fname | sed -e "s/_/-/g"` # Substitute underscore for blank
mv "$fname" "$n" # Do the actual renaming
let "number += 1"
fi
done
# For correct grammar
if [ "$number" -eq "$ONE" ]
then
echo "$number file renamed."
else
echo "$number files renamed."
fi
exit 0
}
@vsmori
Copy link

vsmori commented Dec 21, 2017

great job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment