Oct
10

Find and replace command for whole directory in Linux

In many case most of the projects have so many files in so many different folder. So at some point you want to do find and replace for particular word in your whole project or whole directory in that case this command will help you.

In linux for doing this there is no single command but we can design one new command by combination of several.

find /path/to/directory -type f -exec sed -i.bak 's/oldword/newword/g' {} \;

here it creates a .bak backup file of every file it processes, in case you need to revert. If you don’t need the backup files at all, change -i.bak to -i.

To remove all the backup files when you no longer need them:

find /path/to/directory -type f -name "*.bak" -exec rm -f {} \;

NOTE : if your oldword or newword cantains any special character like & or ‘ or newline then you need to Quote it by adding prefix of \

Example:  if you want to replace

ShareProgrammingTips

with

&shareprogrammingtips

then your code will be like

find /path/to/directory -type f -exec sed -i.bak 's/ShareProgrammingTips/\&shareprogrammingtips/g' {} \;

 

JIGAR PATEL

hey I am an Artist who love to write code…! Well I am an EC graduate From Ganpat University and now i am working as Embedded software engineer in one private firm.. find me at here JigAr Patel

More PostsWebsiteTwitterFacebook

You may like to read this also....

Post comment

*

CommentLuv badge
Follow us on Twitter! Follow us on Twitter!

Search in this website

our sponsors

latest comments

Find us on Facebook

Top Authors

35 posts
saurabh
22 posts
10 posts

Find us on stackoverflow

Polls

Tell us who you are

View Results

Loading ... Loading ...

My Bookmarks

Sponsers Link