Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

sed is a popular tool for working with text data. However, if not used properly, it may cause errors.

In this tutorial, we’ll look at a situation where sed fails to find a file in a Linux directory.

First, we’ll replicate the sed problem. Then, we’ll learn how to fix it on Linux. Finally, we’ll see why the same sed command fails on Linux but works correctly on macOS.

2. Replicating the sed Problem

First, let’s create a sample file with a simple text string using the touch and echo commands:

$ touch test
$ echo "a" > test

Now, we should have a file named test with the string “a” as its content. Let’s check it using the cat command:

$ cat test
a

Here, we can use a simple sed command to replace the “a” string with “b” in the test file:

$ sed -i "" -e 's/a/b/g' test
sed: can't read : No such file or directory

However, we obtain the “No such file or directory” error.

Next, we’ll see how we can resolve this error.

3. Fixing the Error on Linux

Let’s look at the previous sed command in more detail:

  • the sed expression ‘s/a/b/g’ finds the occurrence of the “a” string and replaces it with the “b” string
  • the -i option is for editing the file in place
  • the -e option is for using the sed expression

However, we can see that after the -i option, there is an empty double quote (“”).

In fact, sed interprets the expression as if we provided an empty filename to it. This isn’t something we intended to do.

Therefore, to fix the above error, we should remove the “” after the -i option:

$ sed -i -e 's/a/b/g' test

Now, the operation is completed successfully.

Let’s check if the “a” string has changed to the “b” string:

$ cat test
b

Indeed, the test file now contains a “b” string instead of an “a” string.

4. Using sed on Linux and macOS

The reason for adding the empty quotes after the -i option is to avoid the creation of a sed backup file during command execution.

However, this syntax works on macOS only. The GNU sed implementation on Linux works differently. Therefore, we get the above error on Linux.

To make our script agnostic to the OS, we can add a check for the underlying OS:

if [[ "$OSTYPE" == "darwin"* ]]; then
  sed -i "" -e 's/a/b/g' test
else
  sed -i -e 's/a/b/g' test
fi

As we can see, now we have both options available based on the OS type used.

5. Conclusion

In this article, we’ve looked at a particular sed command error where sed fails to find a correct filename.

First, we created a sample file and replicated the problem on Linux. Then, we looked at the sed command in more detail and learned how to fix the error. Finally, we examined the difference in the sed implementation between Linux and macOS.