Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 <t.i.m.vanwerkhoven@xs4all.nl>
# 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
You can’t perform that action at this time.