Error when using 'sed' with 'find' command on OS X: "invalid command code ."
Solution 1
If you are on a OS X, this probably has nothing to do with the sed command. On the OSX version of sed
, the -i
option expects an extension
argument so your command is actually parsed as the extension
argument and the file path is interpreted as the command code.
Try adding the -e
argument explicitly and giving ''
as argument to -i
:
find ./ -type f -exec sed -i '' -e "s/192.168.20.1/new.domain.com/" {} \;
See this.
Solution 2
On OS X nothing helps poor builtin sed to become adequate. The solution is:
brew install gnu-sed
And then use gsed instead of sed, which will just work as expected.
Solution 3
You simply forgot to supply an argument to -i
. Just change -i
to -i ''
.
Of course that means you don't want your files to be backed up; otherwise supply your extension of choice, like -i .bak
.
Solution 4
Simply add an extension to the -i flag. This basically creates a backup file with the original file.
sed -i.bakup 's/linenumber/number/' ~/.vimrc
sed will execute without the error
Solution 5
Probably your new domain contain /
? If so, try using separator other than /
in sed
, e.g. #
, ,
etc.
find ./ -type f -exec sed -i 's#192.168.20.1#new.domain.com#' {} \;
It would also be good to enclose s///
in single quote rather than double quote to avoid variable substitution or any other unexpected behaviour
Related videos on Youtube
helion3
Been working in the web since the mid-90s. Development has been my full-time career for a decade. I've dabbled with client-side, server-side, and OS languages and have used thousands of frameworks and libraries. I run several popular open source projects and have a great time communicating with my users/collaborators. I'm always searching for excuses to try something new.
Updated on March 03, 2022Comments
-
helion3 about 2 years
Being forced to use CVS for a current client and the address changed for the remote repo. The only way I can find to change the remote address in my local code is a recursive search and replace.
However, with the sed command I'd expect to work:
find ./ -type f -exec sed -i "s/192.168.20.1/new.domain.com/" {} \;
I get an error for every file:
sed: 1: ".//file/path ...": invalid command code .
I've tried to escape the periods in the sed match/replacement but that doesn't solve anything.
-
helion3 over 10 yearsThe example command you give however, gives: sed: RE error: illegal byte sequence
-
damienfrancois over 10 yearsThat most probably is a copy-paste encoding problem as it works with no problem in my tests. See this
-
acheron55 about 10 yearsIf you spent 10 min like I did finding the difference, it is -e option
-
damienfrancois about 10 years@acheron55 I updated my answer to be explicitly about the
-e
option -
JonasVautherin almost 10 yearsThis question answers the
RE error: illegal byte sequence
on MacOS. -
Pierre Houston over 9 yearsi used an empty string '' as the parameter for -i and that worked, like
sed -i '' 's/blah/xx/g'
-
mdup over 9 yearsFor me, adding
-e
after-i
madesed
backup all my files in this way: "foo.txt" -> "foo.txt-e". Obviously what I wanted was rather-i ''
, i.e. don't backup changed files. -
mdup over 9 yearsI definitely am on OS X default sed, not homebrew'd. Mavericks 10.9.3 (maybe sed differs with OS X versions?). No worries, I've added the answer I'd like to have had (and I guess it would have solved the OP's problem as well).
-
aspyct over 9 yearsSame problem for me. This
-i -e
combined with afind
resulted in many many files ending in-e-e-e-e-e-e-e
. -
Joost over 8 yearsSame here, using the sed that ships with 10.10. I recommend
-i ''
-
mdup almost 8 yearsI find it funny how you edited your answer to make it look like mine :) Anyway, as long as users solve their problems, I don't care. Cheers!
-
damienfrancois almost 8 years@mdup I updated my answer after your comment, which I could not verify on my system, but which was backed up by several other commenters. My edit dates Oct 1 '15 at 4:44 and your answer is dated Oct 2 '14 at 8:54. Cheers.
-
JDS about 7 yearsYou can solve this, and all the other problems with macOS, by installing Gnu versions of everything
-
Darrell Teague almost 7 yearsA working revised answer on OSX is: find ./ -type f -exec sed -ie "s/192.168.20.1/new.domain.com/" {} \;
-
bentael over 6 yearsis there a version where the same command would work for both OS X and Linux?
-
damienfrancois over 6 years@bentael Using Homebrew on OS X you can install the GNU
sed
which is the one installes on Linux. -
Stephen Shank about 4 yearsThank you for this answer, which worked great for me! Working on both Linux and Mac, I'd much prefer access to a familiar tool than learning arcane differences between two closely related ones.
-
Jaime Hablutzel over 3 years@bentael if you don't want to install an alternative
sed
see stackoverflow.com/questions/5694228/… -
Jeppe Gravgaard about 3 yearsThanks - worked for me too. A nice little tip is to set up an alias like
alias sed='gsed'
to make the alignment between macOS and linux complete. -
Tom Grushka about 3 yearsWhy is this upvoted 578 times? Creates many backup files ending in ".oldext-e"
-
Hamza A.Malik about 3 yearsThank youuuu! Been trying to write a script for generating an app and have been searching around for the past couple of hours for this.
-
tripleee almost 2 yearsThe OS generally does no such thing. Did you copy/paste from a blog or a word processor or something?
-
Ricardo Gonçalves almost 2 years@tripleee you're right! Updated my answer.
-
tripleee almost 2 yearsSo the real lesson here is never use a word processor for code. There are some blog platforms which unfortunately apply "styling" to code and thus breaking it, which you as a consumer of that blog can't really do much about.
-
tripleee almost 2 years(Also, irritatingly, the keyboard substitution facility on IOS will "helpfully" replace ASCII quotes with "typographic" ones no matter what you do, so it's not great for saving snippets of code.)