Wednesday, February 11, 2009

Find and Replace using find and sed command

This is the best command line I have ever found to search and replace text into files within directories, sub directories.

Command:

find searching_dir -name .dir_name_to_prune -prune -o -exec sed -i 's/Searched Text/Replacing Text /g' {} \;

Example:
find . -name .svn -prune -o -exec sed -i 's/Powered by: /Site Developed by /g' {} \;
Description:
find : the find command
. : current directory (you can set other)
name : name the file list
svn : the svn dir will be prouned
prune : used to prune dirs/files
o : options
exec : executind sed command
sed : the sed command
i : this will insert new text while searched text matches
's/ : this will search for finding matches
/g' : make the operation globally.
{} \; : Out put and termanating the command line


Thats all, this works nice.....

2 comments:

  1. the current directory is not working. but when I used *.* instead of . then it worked.

    ReplyDelete
  2. find *.php -name .svn -prune -o -exec sed -i 's/skin_blue/skin_silver/g' {} \;

    this works for all php files in directories

    ReplyDelete