From 4a6efe57f9bd34924df17bf23ec955f0ec9ea548 Mon Sep 17 00:00:00 2001 From: filip <“filip.rabiega@gmail.com”> Date: Mon, 16 Feb 2026 12:43:40 +0100 Subject: rewrite rmw --- rmw | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/rmw b/rmw index 3a00564..5707eb0 100755 --- a/rmw +++ b/rmw @@ -1,25 +1,33 @@ #!/bin/sh -# Remove whitespaces in all filenames in a given directory -if [ "$#" -gt 1 ]; then - echo "Usage: rmw " >&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 -- cgit v1.2.3