Delete whole lines in many files

If you have a load of files which contain a line you don't want, you can use a combination of find and sed to remove them. This came in useful when one of my servers had been compromised and a number of html files had had malicious content inserted.

In this example, the find command finds all html files and sed is used to remove any lines consisting of the_content_to_remove. All files are backed up with a .bak extension.

Note: ALL found files are replaced, whether or not their content was modified.

find . -name "*.html" -exec sed -e '/^the_content_to_remove*/d' -i.bak {} \;

Last modified: 28/07/2008 Tags: (none)

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top