summaryrefslogtreecommitdiff
path: root/rmw
diff options
context:
space:
mode:
authorfilip <“filip.rabiega@gmail.com”>2026-02-16 12:43:40 +0100
committerfilip <“filip.rabiega@gmail.com”>2026-02-16 12:43:40 +0100
commit4a6efe57f9bd34924df17bf23ec955f0ec9ea548 (patch)
tree8a759c630a4fb87ce32767323d88b67e41f1089a /rmw
parentd763f6025fb04c4790b1a84854468191932597c9 (diff)
downloadchadscripts-master.tar.gz
chadscripts-master.tar.bz2
chadscripts-master.zip
rewrite rmwHEADmaster
Diffstat (limited to 'rmw')
-rwxr-xr-xrmw40
1 files 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 <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