diff options
| author | filip <“filip.rabiega@gmail.com”> | 2026-02-16 12:43:40 +0100 |
|---|---|---|
| committer | filip <“filip.rabiega@gmail.com”> | 2026-02-16 12:43:40 +0100 |
| commit | 4a6efe57f9bd34924df17bf23ec955f0ec9ea548 (patch) | |
| tree | 8a759c630a4fb87ce32767323d88b67e41f1089a | |
| parent | d763f6025fb04c4790b1a84854468191932597c9 (diff) | |
| download | chadscripts-4a6efe57f9bd34924df17bf23ec955f0ec9ea548.tar.gz chadscripts-4a6efe57f9bd34924df17bf23ec955f0ec9ea548.tar.bz2 chadscripts-4a6efe57f9bd34924df17bf23ec955f0ec9ea548.zip | |
| -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 |
