Coding⏱️ 3 min read📅 2026-06-03

How to Fix: Error when using 'sed' with 'find' command on OS X: "invalid command code ."

Error when using sed with find command on OS X: invalid command code.

Quick Answer: The issue is caused by the period in the path, which is treated as a special character. Use forward slashes instead of backslashes to resolve the issue.

Error when using sed with find command on OS X: invalid command code. This error occurs when you attempt to use the sed command within a find command, resulting in an invalid command code error.

This issue is frustrating because it prevents you from replacing file paths with new domain names using a simple recursive search and replace. However, don't worry, we've got you covered.

💡 Why You Are Getting This Error

  • The main reason for this error is that the find command does not support executing shell commands within its -exec option. The -exec option is used to execute a command on each file found by find, but it must be a valid shell command.
  • Another possible cause of this error is that the sed command is being executed in an unexpected context. When using the -exec option with find, the command is executed in a subshell, which may not support all sed options.

🚀 How to Resolve This Issue

Using the find command without -exec

  1. Step 1: To fix this issue, you can use the find command without the -exec option. This will allow you to use the sed command directly in the find command.
  2. Step 2: First, locate all files using the find command: `find ./ -type f`
  3. Step 3: Then, pipe the output of the find command into the sed command: `find ./ -type f | xargs sed -i 's/192.168.20.1/new.domain.com/' {}'`

Escaping special characters in the sed command

  1. Step 1: Alternatively, you can escape the periods in the sed match/replacement using a backslash. This will allow you to use the sed command with the -exec option.
  2. Step 2: First, locate all files using the find command: `find ./ -type f`
  3. Step 3: Then, pipe the output of the find command into the sed command with escaped periods: `find ./ -type f | xargs sed -i 's/\./192.168.20.1/new.domain.com/ {}'`

💡 Conclusion

To summarize, the error occurs because the find command does not support executing shell commands within its -exec option. By using the find command without -exec or escaping special characters in the sed command, you can successfully replace file paths with new domain names.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions