Last active
April 22, 2026 17:42
-
-
Save kmatt/3e4bdd333a24726af9247ed3fe883081 to your computer and use it in GitHub Desktop.
Slug filenames in directory
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 | |
| if [ "$#" -eq 0 ]; then | |
| echo "Usage: $0 FILENAME | 'WILDCARD' (in single quotes)" | |
| exit | |
| fi | |
| slug() { | |
| # Replace non-alpha characters and rename file | |
| f=$(basename "$1") | |
| s=$(echo "$f" | awk '{ gsub(/[^0-9a-zA-Z_\-\. ]/, ""); gsub(/[ ]+/, "-"); gsub(/--+/, "-"); print; }') | |
| if [ "$f" != "$s" ]; then | |
| mv -v "$f" "$s" | |
| fi | |
| } | |
| # Slug all filenames in current directory, this `find` invocation safe for paths with newline characters | |
| find . -type f -depth 1 -name "$1" -print0 | while IFS= read -r -d '' fn; do slug "$fn"; done | |
| # vim: set filetype=sh : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment