Tag Archives: Scripting

Shell script: recursive find/replace

Lately I had a requirement of finding & replacing a specific string from all the files under a directory tree, while keeping the file permissions intact. Here is the shell script that I eventually came up..

#!/bin/bash

cd target_dir

for _file in `find .`;
do
  if [ ! -d $_file ]; then
    sed "s/SEARCH_STR/REPLACE_STR/g" $_file > tmp; cp tmp $_file; rm tmp;
  fi
done

Hope this would help somebody.. disfrutar!