Posts Tagged ‘rename’

linux batch rename files

October 28th, 2011

Use this command to rename a set of files:

for file in * ; do mv $file `echo $file | sed 's/OLD_STRRING/NEW_STRING/'` ; done

And here is a batch file:
——————
batch_rename.sh
——————

#!/bin/bash

if [ $# -ne 3 ]; then
        echo "Usage: $0 'FilePattern' SearchString ReplaceString";
        echo "Eg: $0 'r-*.sh' 5 6";
else
        for file in $1 ; do mv $file `echo $file | sed s/$2/$3/` ; done
fi

If the 2nd or 3rd parameter contains space, use ‘ ‘ to surround the parameter.