sed: can't read : No such file or directory

This is a wiki page. Be bold and improve it!

If you have any questions about the content on this page, don't hesitate to open a new ticket and we'll do our best to assist you.

Table of Contents

Error message:

sed: can't read : No such file or directory

This is due to running sed with the option --in-place without having a given file:

$ sed --in-place s/from/to/ ""

In a bash script

This error is usually encountered when sed is employed within a bash script. For example:

#!/bin/bash
sed --in-place "s/from/to/" "${1}"

and forgetting to run the script with a file argument:
$ myscript.sh
sed: can't read : No such file or directory
$ myscript.sh file.txt # OK.

Also, make sure the file name is enclosed in double quotes, in case there are spaces in the file name:

# This is brittle. Expect breakage with some file names:
sed --in-place "s/from/to/" ${1}

With xargs

The same principle applies with xargs as with a bash script. Beware of file names which include spaces.

Use the xargs option -0, and make sure that whatever pipes the data to xargs is doing something similar:

# With find:
find "something" -print0 | xargs -0 sed --in-line "s/old/new/"
# with egrep:
egrep -Z "something" *.txt  | xargs -0 sed --in-line "s/old/new/"

Other

Other things to consider:
The 'file' may be a symlink to a non-existent file. Make sure the symlink points to something valid.