Last active
December 21, 2017 15:47
-
-
Save jasondmoss/7365594 to your computer and use it in GitHub Desktop.
Bash: Replace blank spaces with underscore for file(s) in current directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
## 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great job!