beta
beta

Reputation: 5686

sed script - permission denied on temp file

I just used a combination of find and sed to replace strings in files of a directory.

find . -type f -exec sed -i 's,foo,bar,g' {} +

It got the job done. After that I logged off the server (connected via SSH), and then remembered, that I need to run the command again. So I fired the same command with slightly modified find/replace strings, but it did not work anymore giving the following error:

sed: couldn't open temporary file ./sedPFq4Ck: Permission denied

What is wrong now?

FWIW: the file name of the mentioned temporary file changes after each new try.

Upvotes: 3

Views: 13878

Answers (2)

stackprotector
stackprotector

Reputation: 13588

To complement heemayl's helpful explanation, if you do not want to modify permissions, you can prevent temporary files by not using -i and piping the result of sed into the same file again:

sed 's,foo,bar,g' /path/to/file > /path/to/file

Upvotes: 1

heemayl
heemayl

Reputation: 42107

While editing a file in place, sed creates a temporary file, saves the result and then finally mv the original file with the temporary one.

The problem is that you don't have write permission in the directory where sed is trying to create the temp file.

As the file is ./sedPFq4Ck, check the permission of the directory where you are running the find command.

Upvotes: 7

Related Questions