| 1 | #! /bin/bash |
| 2 | # blank-rename.sh |
| 3 | # |
| 4 | # Substitutes underscores for blanks in all the filenames in a directory. |
| 5 | |
| 6 | ONE=1 # For getting singular/plural right (see below). |
| 7 | number=0 # Keeps track of how many files actually renamed. |
| 8 | FOUND=0 # Successful return value. |
| 9 | |
| 10 | for filename in * #Traverse all files in directory. |
| 11 | do |
| 12 | echo "$filename" | grep -q " " # Check whether filename |
| 13 | if [ $? -eq $FOUND ] #+ contains space(s). |
| 14 | then |
| 15 | fname=$filename # Yes, this filename needs work. |
| 16 | n=`echo $fname | sed -e "s/ /_/g"` # Substitute underscore for blank. |
| 17 | mv "$fname" "$n" # Do the actual renaming. |
| 18 | let "number += 1" |
| 19 | fi |
| 20 | done |
| 21 | |
| 22 | if [ "$number" -eq "$ONE" ] # For correct grammar. |
| 23 | then |
| 24 | echo "$number file renamed." |
| 25 | else |
| 26 | echo "$number files renamed." |
| 27 | fi |
| 28 | |
| 29 | exit 0 |