Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Created January 14, 2026 09:38
Show Gist options
  • Select an option

  • Save ZiTAL/d177f7327dd3e2fdd2905b7183ddab5a to your computer and use it in GitHub Desktop.

Select an option

Save ZiTAL/d177f7327dd3e2fdd2905b7183ddab5a to your computer and use it in GitHub Desktop.
bash: clean filenames
#!/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