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
Kdevtmpfsi Using the Entire Cpu
Modify Qt Gui from Background Worker Thread
How to Upgrade Glibc from Version 2.12 to 2.14 on Centos
What Does Localhost Means Inside a Docker Container
Why Data and Stack Segments Are Executable
How to Kill a Process by Name Instead of Pid, on Linux
How to Set the Gopath Environment Variable on Ubuntu? What File Must I Edit
Git Asks For Username Every Time I Push
How to Replace a String in Multiple Files in Linux Command Line
How to Obtain the Number of Cpus/Cores in Linux from the Command Line
Controlling a Usb Power Supply (On/Off) With Linux
How to Rename All Folders and Files to Lowercase on Linux
How to View the List of Functions a Linux Shared Library Is Exporting
How to Loop Over Directories in Linux
Which Linux Ipc Technique to Use
"/Usr/Bin/Ld: Cannot Find -Lz"
How to Use Sed to Extract Substring
How to Get the Total Cpu Usage of an Application from /Proc/Pid/Stat