Skip to content

Instantly share code, notes, and snippets.

@tvwerkhoven
Created April 5, 2010 20:02
Show Gist options
  • Save tvwerkhoven/356790 to your computer and use it in GitHub Desktop.
Save tvwerkhoven/356790 to your computer and use it in GitHub Desktop.
Recursively rename all files in a directory
#!/bin/bash
#
# Recursively rename all files in a directory with the following rules:
# - Convert letters to lower case
# - Convert whitespace to underscore
# - Remove 'track' in the name
#
# Used to clean up filenaming of mp3s
#
# Tim van Werkoven, 20100405 <[email protected]>
# This file is licensed under the Creative Commons Attribution-Share Alike
# license versions 3.0 or higher, see
# http://creativecommons.org/licenses/by-sa/3.0/
function rename {
LOWER='abcdefghijklmnopqrstuvwxyz'
UPPER='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
if [ -n "$1" ]; then cd $1; fi
pwd
for i in *; do
j=`echo $i | sed s/\ /_/g | sed y/$UPPER/$LOWER/ | sed s/track//g`
if [ ! "$i" = "$j" ]; then mv -- "$i" "$j"; fi
if [ -d "$j" ]; then
rename $j
cd ..
pwd
fi
done
}
rename $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment