How to Replace a String in Multiple Files in Linux Command Line

How to replace a string in multiple files in linux command line

cd /path/to/your/folder
sed -i 's/foo/bar/g' *

Occurrences of "foo" will be replaced with "bar".

On BSD systems like macOS, you need to provide a backup extension like -i '.bak' or else "risk corruption or partial content" per the manpage.

cd /path/to/your/folder
sed -i '.bak' 's/foo/bar/g' *

How to replace an unknown string in multiple files under Linux?

you can use the command find and sed for this. make sure you are in the folder that you want to replace files.

find . -name '*.*' -print|xargs sed -i "s/\"url\/1.png\"/\"url\/static.png\"/g"

How to replace one line in multiple file on linux terminal?

You may use this sed:

s='"tree": "/a/anything-here/b/"'
sed -E 's~"(/[^/]*/)[^/]*/~\1new-string/~' <<< "$s"
"tree": /a/new-string/b/"

Or using awk:

awk -v str='new-string' 'BEGIN{FS=OFS="/"} {$3 = str} 1' <<< "$s"
"tree": "/a/new-string/b/"

search and replace string on multiple files from unix terminal

Try this:

find . -name '*.html' -exec sed -i 's/\.html/\.php/g' "{}" \;

It will find all files in the current and subdirectories that end in .html, and run sed on each of them to replace .html with .php anywhere it appears within them.

See http://content.hccfl.edu/pollock/unix/findcmd.htm for more details.

replace a string from first line on multiple files

You are very close with your code, but you need to account for the trailing / char after the . char.

Assuming you are using a modern sed with the -i (inplace-edit) option you can do

sed -i '1s@supersonic\./@supersonic.com/@' * 

Note that rather than have to escape / inside of the s/srchpat\/withSlash/replaceStr/', you can use another char after the the s command as the delimiter, here I use s@...@...@. If your search pattern had a @ char, then you would have to use a different char.

Some older versions of sed need to you to escape the alternate delimiter at the first use, so

     sed 's\@srchStr@ReplStr@' file 

for those cases.

If you're using a sed that doesn't support the -i options, then
you'll need to loop on your file, and manage the tmp files, i.e.

 for f in *.html ; do
sed '1s@supersonic\./@supersonic.com/@' "$f" > /tmp/"$f".fix \
&& /bin/mv /tmp/"$f".fix "$f"
done

Warning

But as you're talking about 10,000+files, you'll want to do some testing before using either of these solutions. Copy a good random set of those files to /tmp/mySedTest/ dir and run one of these solutions there to make sure there are no surprises.

And you're likely to blow out the cmd-line MAX_SIZE with 10,000+ files, so read about find and xargs. There are many posts here about [sed] find xargs. Check them out if needed.

IHTH

find and replace in multiple files on command line

there are many ways .But one of the answers would be:

find . -name '*.html' |xargs perl -pi -e 's/find/replace/g'

How can I change one string in multiple files in few subdirectories?

you can do

find changePhraseDir -type f -a -writeable|xargs sed -i 's/foo/bar/'
  • -type f -> file
  • -a -> and
  • -writable -> your -w


Related Topics



Leave a reply



Submit