Skip to content

Instantly share code, notes, and snippets.

@matthew-e-brown
Created August 16, 2021 00:47
Show Gist options
  • Save matthew-e-brown/bf2c49a2a60764b2ea63d6c8a419a362 to your computer and use it in GitHub Desktop.
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
#!/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