first commit
This commit is contained in:
40
rename_space.sh
Executable file
40
rename_space.sh
Executable file
@ -0,0 +1,40 @@
|
||||
#!/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
|
||||
Reference in New Issue
Block a user