1

On my OSX machine, I can't use ~ as the backup character for in-place sed. Any other character is fine. The error I get is... cryptic: rename(): Not a directory.

Example:

$ echo foo > bar
$ sed -i ~ -e s/foo/hello/ bar
sed: rename(): Not a directory
$ ls -1
bar
$ cat bar
foo
$ sed -i _ -e s/foo/hello/ bar
$ ls -1
bar
bar_
$ cat bar
hello
$ cat bar_
foo
Cyrus
  • 69,405
  • 13
  • 65
  • 117
hraban
  • 1,360
  • 15
  • 25
  • not actually about sed, it turns out, but this is a common command to have a stand-alone tilde with and the error message doesn't explain what's going on. took me a month to figure this out, had to break out dtruss and felt quite dumb in the end :P hope it helps someone down the road. – hraban Apr 16 '17 at 18:26

1 Answers1

4

Bash automatically expands a stand-alone tilde (~) into $HOME:

$ echo ~
/Users/hraban

Therefore, sed -i ~ becomes sed -i /home/you, which leads sed to try to rename bar to bar/home/you---a directory that doesn't exist. To fix this, escape the tilde in bash:

$ sed -i \~ -e s/foo/hello/ bar
$ cat bar
hello
hraban
  • 1,360
  • 15
  • 25