Created
August 16, 2021 00:47
-
-
Save matthew-e-brown/bf2c49a2a60764b2ea63d6c8a419a362 to your computer and use it in GitHub Desktop.
Renames files with specific extensions to contain only snake-case lowercase titles
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
#!/usr/bin/bash | |
# Check if they passed an arugment for which extensions to convert | |
if [[ $# -eq 0 ]]; then | |
exts="mkv" | |
else | |
exts="$@" | |
fi | |
for ext in $exts; do | |
for file in *.$ext; do | |
if [[ -f $file ]]; then | |
new_name=$(echo "$file" | sed -E \ | |
-e 's/.*/\L&/' `# convert to lowercase`\ | |
-e 's/\[[^]]+\]//g' `# remove anything inside []`\ | |
-e 's/\([^)]+\)//g' `# remove anything inside ()`\ | |
-e 's/^[[:blank:]]+//' `# remove leading whitespace`\ | |
-e 's/[[:blank:]]+\.'"$ext/.$ext/" `# remove trailing whitespace`\ | |
-e 's/[[:blank:]]+/-/g' `# convert whitespace to '-'`\ | |
-e 's/-+/-/g' `# replace multiple --- with single -`\ | |
) | |
mv "$file" "$new_name" | |
fi | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment