S/search/replace for All Files in a Directory

Here’s a quick and dirty shell script I wrote to rename all the files in a directory based upon a search and replace string:

1
2
3
4
5
6
7
8
9
10
11
#!/bin/sh

S=$1
R=$2

if [ -z "$R" ]; then
  echo "Usage: rename.sh <search> <replace>"
  exit
fi

for i in `ls *$S*`; do mv $i $(echo $i | sed s/$S/$R/); done

Be careful, virtually no error checking is done.

Comments