New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
Linux, command line for search and replace (recursive)
Hi,
I am trying to find a way, from a linux command line, to replace all occurrences of a string by another string, in all files in a folder and its subfolders.
So I have the folder : "/home/somepath/" and would like to replace the string "/conf/server1/" by "/conf/server2/" in all files and sub folders.
I think it would be a mix of the command "find" and "sed", but I have some difficulties to figure it out correctly.
Thanks in advance,
Comments
find ./ -type f | xargs sed -i 's/string1/string2/'
Sometimes you need -E for extended regex, or some expressions will not work.
cd /to/dir
for E in
/bin/ls
; do sed -i 's/string1/string2/' $E ; donefck vanilla
code is here:
https://pastebin.com/2emDBNAK
Something like:
find ./home/somepath/* -type -f | xargs sed -i 's@/conf/server1/@/conf/server2/@g'
also some other example here: https://coderwall.com/p/2ivpsg/recursive-find-replace-with-sed
Any perl one-liner masters here?
xargs < -exec {} \;
I suspect the following incantation will do the trick
normally might need to '\' escape the '/' in sed for your specific example - using # as delimiter is a way to avoid that messiness. And find -exec is an alternative to using xargs (which is also fine to use).
Please consider avoiding common pitfalls as a matter of habit, no matter how limited is the scope of your code/one-liner
https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29
https://mywiki.wooledge.org/Quotes
https://mywiki.wooledge.org/FullBashGuide#BashGuide.2FInputAndOutput.line-86
https://mywiki.wooledge.org/BashFAQ/082
Thank you very much everybody!
perl -v
For this type of questions, I recommend searching https://unix.stackexchange.com/ or asking there.
Yes I know, but I find stackexchange's community very unfriendly. If you ask a question, which has been covered 10 years earlier, buried within millions of topics, you get slapped on the fingers, and half of members will blame you for asking questions which seem obvious to them. If it's a matter of being criticized or blamed, I prefer that it comes from @eol or @deank who will always have a smart and clever way to point your ignorance.
So much this.