Skip to content

Instantly share code, notes, and snippets.

@gardner
Last active January 1, 2016 16:39
Show Gist options
  • Save gardner/8171792 to your computer and use it in GitHub Desktop.
Save gardner/8171792 to your computer and use it in GitHub Desktop.
Rename all files and directories for use with my janky bicycle mp3 player.
#!/bin/bash
function rename_usb() {
find . -type "$1" | while read -r file; do
if [ "$file" -eq ".."
newfile=$(echo "$file" | sed 's/[[:space:]]?/_/g
#replace " - " with a single underscore.
s/[[:space:]]-[[:space:]]/_/g
#replace spaces with underscores
#replace "-" dashes with underscores.
s/-?/_/g
#remove exclamation points
s/!//g
#remove commas
s/,//g')
if [ -$1 "$newfile" ]; then
echo "[WARNING] file exists: $newfile... Skipping."
else
echo mv "$file" "$newfile"
fi
done
}
# first rename all the directories
rename_usb 'd'
# then all the files
rename_usb 'f'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment