Created
January 14, 2026 09:38
-
-
Save ZiTAL/d177f7327dd3e2fdd2905b7183ddab5a to your computer and use it in GitHub Desktop.
bash: clean filenames
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 | |
| find . -depth -type f -exec bash -c ' | |
| for f; do | |
| dir=$(dirname "$f") | |
| name=$(basename "$f") | |
| ext="${name##*.}" | |
| base="${name%.*}" | |
| clean=$(echo "$base" | tr -cd "A-Za-z0-9\ \-_") | |
| # fallback if name becomes empty | |
| [ -z "$clean" ] && clean="file" | |
| target="$dir/$clean.$ext" | |
| mv -- "$f" "$target" | |
| done | |
| ' bash {} + |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment