41 lines
867 B
Bash
Executable File
41 lines
867 B
Bash
Executable File
#!/bin/bash
|
|
|
|
# Standardvärden
|
|
DIR="."
|
|
DRY_RUN=false
|
|
|
|
# Läs argument
|
|
for arg in "$@"; do
|
|
case "$arg" in
|
|
--dry-run)
|
|
DRY_RUN=true
|
|
;;
|
|
*)
|
|
DIR="$arg"
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Gå igenom alla filer i katalogen rekursivt
|
|
find "$DIR" -depth -type f | while read -r file; do
|
|
dir=$(dirname "$file")
|
|
base=$(basename "$file")
|
|
|
|
# Byt ut mellanslag mot underscore
|
|
newbase=$(echo "$base" | tr ' ' '_')
|
|
|
|
# Byt ut flera underscores mot en enda
|
|
newbase=$(echo "$newbase" | sed 's/_\+/_/g')
|
|
|
|
# Om namnet har ändrats
|
|
if [ "$base" != "$newbase" ]; then
|
|
newpath="$dir/$newbase"
|
|
if [ "$DRY_RUN" = true ]; then
|
|
echo "[Dry-run] $file -> $newpath"
|
|
else
|
|
echo "Renaming: $file -> $newpath"
|
|
mv -i "$file" "$newpath"
|
|
fi
|
|
fi
|
|
done
|