Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this line

find . -type f \( ! -name '*.plist' \) -and \( ! -name '*.mp4' \) -and \( ! -name '.DS*' \) -print0 | xargs -0 sed -i 's/AAA/BBB/g'

my intention is from the current directory recursively change all occurrences of AAA to BBB in all files except those with the following extensions: .plist, .mp4 and .DS*

but I get the message

sed: 1: "./myDir/pro ...": invalid command code .
share|improve this question
Are you on a mac or other BSD? I've noticed you have to include a suffix with -i on those. Try -i.bak – Kevin Jan 7 '13 at 3:16
Mac. where do I put this -i.bak???? – SpaceDog Jan 7 '13 at 3:21
Replace the plain -i with it. find [...] | xargs -0 sed -i.bak 's/AAA/BBB/g' – Kevin Jan 7 '13 at 3:24
ok, now I get another error: sed: RE error: illegal byte sequence – SpaceDog Jan 7 '13 at 3:25
Looks like someone on SO had that problem, answer here. Add LANG=C before the xargs: find [...] | LANG=C xargs -0 [...] – Kevin Jan 7 '13 at 3:39
up vote 1 down vote accepted

The easiest solution is to use this:

find . -type f \( ! -name '*.plist' \) -and \( ! -name '*.mp4' \) -and \( ! -name '.DS*' \) -print0 | xargs -0 perl -pi -w -e  's/AAA/BBB/g'
share|improve this answer

Unlike GNU sed, the sed that ships with Mac OSX requires an argument to the -i option, specifying the backup suffix; to suppress the backup you have to pass an empty string (''). Therefore, sed was interpreting your s/AAA/BBB/ as the backup suffix and taking your fist file name (./myDir/pro ...) as the command to execute. Of course, . is not a valid sed command, causing the error. Keeping that in mind, the following will work:

find [...] | xargs -0 sed -i.bak 's/AAA/BBB/g'

Or, if you insist on suppressing the backup

find [...] | xargs -0 sed -i '' 's/AAA/BBB/g'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.