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' *
Find and replace with sed in directory and sub directories
Your find
should look like that to avoid sending directory names to sed
:
find ./ -type f -exec sed -i -e 's/apple/orange/g' {} \;
Search and replace text in all files of a linux directory
The following will do it:
sed -i 's/old_link/new_link/g' file...
Don't forget to escape any slashes, dots, and any other regex special chars in the link addresses with a backslash.
How to find all files containing specific text (string) on Linux?
Do the following:
grep -rnw '/path/to/somewhere/' -e 'pattern'
-r
or-R
is recursive,-n
is line number, and-w
stands for match the whole word.-l
(lower-case L) can be added to just give the file name of matching files.-e
is the pattern used during the search
Along with these, --exclude
, --include
, --exclude-dir
flags could be used for efficient searching:
This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
For directories it's possible to exclude one or more directories using the
--exclude-dir
parameter. For example, this will exclude the dirsdir1/
,dir2/
and all of them matching*.dst/
:grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/search/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.
For more options, see man grep
.
How do I replace text in all files containing specific text on Linux?
Yes, there is a command in Linux that replaces the text in all files. Try:
find "/path/mypath" -type f -exec sed -i "s/mypattern/mypattern2/g" {} \;
as U880D proposes. No need to fetch first the matching files with grep
, sed
is able to search and to replace.
Find and replace words and special characters from specific files from linux
You have extra backslashes to escape special characters. Please try:
# find . -type f -name "gnuplot_RTre_*.gnplt" -exec sed -i 's/set xrange \[0:20]/set xrange \[0:3]/g' {} +
# The backslash in the REPLACEMENT above is unnecessary, although harmless.
# Please adopt the following instead.
find . -type f -name "gnuplot_RTre_*.gnplt" -exec sed -i 's/set xrange \[0:20]/set xrange [0:3]/g' {} +
In sed
, the characters $.*[\^
need to be escaped with a backslash to be treated as literal.
[EDIT]
The right square bracket "]"
is usually not a special character in regex and
you do not have to esacape it:
echo "[abc]" | sed 's/]/*/g'
=> [abc*
But "]"
behaves as a metacharacter if preceded by an unescaped left square bracket "["
to compose a character class
.
echo "[abc]abc" | sed 's/[abc]/*/g'
=> [***]***
In order to make "["
to be literal, we need to escape it.
echo "[abc]abc" | sed 's/\[abc]/*/g'
=> *abc
"]"
can be also escaped just for visual symmetricity.
echo "[abc]abc" | sed 's/\[abc\]/*/g'
=> *abc
How to change all occurrences of a word in all files in a directory
A variation that takes into account subdirectories (untested):
find /var/www -type f -exec sed -i 's/privelages/privileges/g' {} \;
This will find
all files (not directories, specified by -type f
) under /var/www
, and perform a sed
command to replace "privelages" with "privileges" on each file it finds.
Find and Replace string in all files recursive using grep and sed
As @Didier said, you can change your delimiter to something other than /
:
grep -rl $oldstring /path/to/folder | xargs sed -i s@$oldstring@$newstring@g
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'
Related Topics
How to Delete All Files Older Than 3 Days When "Argument List Too Long"
How to Know Which of the /Dev/Input/Eventx (X=0..7) Have the Linux Input Stream
Display Only Files and Folders That Are Symbolic Links in Tcsh or Bash
Find Files in Created Between a Date Range
Best Practices for Git Repositories on Open Source Projects
I Want to Contribute to the Linux Kernel
Preserve Colouring After Piping Grep to Grep
How to Calculate System Memory Usage from /Proc/Meminfo (Like Htop)
How to Learn the Structure of Linux Wireless Drivers (Mac80211)
Linux Free Shows High Memory Usage But Top Does Not
Internals of a Linux System Call
How to Perform a 'Git Pull' Without Re-Entering My Ssh Password
How to Install Chkconfig on Ubuntu
Is Usb Supported on Bash on Ubuntu on Windows 10
Linux Script with Curl to Check Webservice Is Up
Error When Trying to Run .Asm File on Nasm on Ubuntu
How to Redirect the Output of an Application in Background to /Dev/Null