diff options
| -rwxr-xr-x | rmw | 40 |
1 files changed, 24 insertions, 16 deletions
@@ -1,25 +1,33 @@ #!/bin/sh -# Remove whitespaces in all filenames in a given directory -if [ "$#" -gt 1 ]; then - echo "Usage: rmw <dir>" >&2 +# Remove whitespace and certain characters from filenames in a directory + +[ "$#" -gt 1 ] && { + printf "Usage: rmw [dir]\n" >&2 exit 1 -fi +} -if [ "$#" -eq 0 ]; then - dir="$(pwd)" -else - dir="$1" -fi +dir=${1:-$(pwd)} -cd "$dir" || { - echo "Couldn't cd to directory. Exiting..." >&2 +cd -- "$dir" || { + printf "Couldn't cd to directory.\n" >&2 exit 1 } -for file in "$dir"/*; do - newname="$(echo "$file" | sed -e 's/["`”“-]*//g' -e 's/[ \t]+/\_/g')" - mv "$file" "$newname" -done +for file in .* *; do + [ -e "$file" ] || continue + + newname=$(printf '%s\n' "$file" | + sed -E \ + -e 's/["`”“-]*//g' \ + -e 's/[[:space:]]+/_/g') -exit 0 + [ "$file" = "$newname" ] && continue + + if [ -e "$newname" ]; then + printf "Skipping '%s' (target exists)\n" "$file" >&2 + continue + fi + + mv -- "$file" "$newname" +done |
