[Previous entry: "Tasty bits from Slashdot"] [Main Index]

10/02/2005 Entry: "Rename files in a directory"

Given what a pain in the ass iTunes can be when working with mp3 files not of its own making, I've had to rename certain files with a prefix so I can manage them better. Here's a script to rename files with a given prefix. Invoked so:

> prerename.sh

Example:
>prerename.sh "*.mp3" "CD-1-"


Be sure to keep the arguments inside double quotes so the renaming can handle spaces, otherwise the shell will expand them into arguments for you. Oops.

#!/bin/bash
# Program: prerename.sh
# Author: Spastic Mutant mutant@spasticmutant.com
#
# Rename a bunch of files with the given prefix.
# e.g. > ./testrename "fo*" "myprefix"
# renames foo\ 5 to myprefixfoo\ 5 .
#

prefix=$2

for i in $1 ;
do
echo "starting with: " $i
mfile=$(echo $i | sed 's/\.\///')
echo $mfile
echo "finalname:" $prefix$mfile
mv "$mfile" "$prefix$mfile"

done






Symbol for
Universal Recyclables DISCLAIMER: This web page was constructed with 100% recycled electrons (or, during shortages, at least 35% post-consumer electrons). No electrons were harmed in the making of this web page, although several were accelerated against their collective will. No files have been charged. And Remember: the only Good Cow is a Dead Cow.

© 2000, 2001, 2002 Spastic Mutant Productions. All Rights Reserved.