improved wording for not finding file
[feisty_meow.git] / scripts / files / replace_spaces_with_underscores.sh
1 #!/bin/bash
2
3 # takes names given to it and replaces any spaces or other gnarly characters with underscores.
4
5 #hmmm: this starts to look like a useful function that the bracket fixer could also use.
6
7 if [ $# -lt 1 ]; then
8   echo "This script requires one or more file names whose names should be fixed."
9   echo "Any spaces or single-quote characters will be stripped out in a useful way."
10   exit 1
11 fi
12
13 while [ $# -gt 0 ]; do
14   file="$1"; shift
15   # first turn spaces into underscores.  then process characters we don't want
16   # in names.  then translate multiple underscores into just one.  then turn
17   # number followed by underscore into just number (?).  then translate
18   # underscore dash underscore into just dash.
19   newname="$(echo "$file" | tr -s ' ' '_' | tr -d "\$\!|@&#%}{)(][\\\~',:?><\"" | sed -e 's/__/_/g' | sed -e 's/\([0-9]\)_\./\1./g' | sed -e 's/_-_/-/' )"
20   if [ "$file" != "$newname" ]; then
21     # we've effected a name change, so let's actually do it.
22     echo "'$file' => '$newname'"
23     mv "$file" "$newname"
24   fi
25 done
26