Issue
I need to do a regex find and replace on all the files in a folder (and its subfolders). What would be the linux shell command to do that?
For example, I want to run this over all the files and overwrite the old file with the new, replaced text.
sed 's/old text/new text/g'
Solution
There is no way to do it using only sed. You'll need to use at least the find utility together:
find . -type f -exec sed -i.bak "s/foo/bar/g" {} \;
This command will create a .bak
file for each changed file.
Notes:
- The
-i
argument forsed
command is a GNU extension, so, if you are running this command with the BSD'ssed
you will need to redirect the output to a new file then rename it. - The
find
utility does not implement the-exec
argument in old UNIX boxes, so, you will need to use a| xargs
instead.
Answered By - osantana Answer Checked By - David Goodson (WPSolving Volunteer)