-
-
Save francoisgeorgy/5e99c0000f304b697260 to your computer and use it in GitHub Desktop.
clean filenames #shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # Sanitizes all files in the given path | |
| # and makes them lowercase too. | |
| # | |
| # param 1: source directory | |
| # param 2: target directory | |
| shopt -s extglob; | |
| # copy as default, you have to change this if you want to move files | |
| MV=cp -v | |
| #MV=mv -v | |
| source="$1" | |
| target="$2" | |
| find ${source} -depth -type f -print | while read path | |
| do | |
| filename=$(basename "$path") | |
| directory=$(dirname "$path") | |
| # remove original path from target to avoid deep path in target directory | |
| directory_clean=${directory#$source} | |
| target="$(dirname "$target")/$(basename "$target")" | |
| # remove invalid chars from directory/filename | |
| directory_clean="${directory_clean//+([^[:alnum:]_-\.\/])/_}" | |
| filename_clean="${filename//+([^[:alnum:]_-\.])/_}" | |
| mkdir -vp "${target}/${directory_clean,,}" | |
| ${MV} "$directory/$filename" "${target}/${directory_clean,,}/${filename_clean,,}" | |
| done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment