[SOLVED] find and replace text from many files in a dir
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a many directories each with about 20 html files inside. All the files have .html ext. What I'm hoping is possible is from command line to find some text in each one and replace it with some other text.
Basically what I want to replace is;
/awstats/
with
awstats/
I can do this easily with dreamweaver or some other application but because I have 960 pages total to do I'm hoping to do it this way.
find . -type f -iname "*.html" -exec sed -i.bak 's%/awstats/%awstats/%' {} \;
This finds all html files from the dir you execute the command (and all subdirectories) and changes /awstat/ into awstat/. This is done in-place (the -i switch).
Do test this first!! (create a temp directory, copy a few html files into it and execute this command from within that directory). I did test it on 2 files and that worked, but you need to be sure.
Hope this helps.
EDIT
I just added the .bak part to the -i switch, this will create a backup of the original.
/EDIT
Last edited by druuna; 07-08-2010 at 11:18 AM.
Reason: Added .bak to -i switch for backups.
Thank you so much! This does work. I used it in test dir on one of the files I need to work with. However, there is only 1 issue.
It only changes the first instance it finds in any block of html. I spent some time trying to figure out why and as a test just ran the script again and it got the next instance, a third got the third.
So if it's just a matter of running it 5-6 times in each directory I can do that but would like to know if there is a way around doing so.
Also, one other thing I should have asked when i first posted was how would I change the script to be run from outside of the directories so I can do them all at once instead of going into each directory and running the script? I have something like 60 total directories that need to be done.
If the directories are all in one place (ie all are sub directories, sub-sub directories of a parent folder say "~/src"), then you could simply change the . after find to ~/src since find traverses sub directories by default.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.