nixos-config/components/media/sanitize_folder.sh

20 lines
373 B
Bash
Raw Permalink Normal View History

2023-11-07 16:48:09 +01:00
#!/bin/bash
2023-11-07 09:02:41 +01:00
# todo : add a dry-run
sanitize() {
2023-11-07 16:48:09 +01:00
shopt -s extglob
2023-11-07 09:02:41 +01:00
filename=$(basename "$1")
directory=$(dirname "$1")
filename_clean="${filename//+([^[:alnum:]_-\\.])/_}"
2023-11-07 16:48:09 +01:00
if [[ $filename != "$filename_clean" ]]; then
2023-11-07 09:02:41 +01:00
mv -v --backup=numbered "$1" "$directory/$filename_clean"
fi
}
export -f sanitize
2023-11-07 16:48:09 +01:00
find "$1" -depth -exec bash -c 'sanitize "$0"' {} \;