1

I have file /some/foo which is a plain text file. I can edit it with vim and I can cat something else to it. But whenever I try to automate the editing process with sed, it says

sed: cannot move '/some/sedGl29Uj': Device or resource busy

I tried redirecting sed's output to a temporary file and cat, it's OK. But if I try to move the temporary file onto it, it will say

root@localhost:/some # mv tmp foo
mv: Device or resource busy

I'm quite concerned why vim and cat can write the file but mv can't.
Ubuntu 16.04.1 LTS. /some is a subfolder of / (root mount point, ext4).

iBug
  • 3,508

2 Answers2

1

Found the answer when managing drive mounts. A strange line showed up when I was checking mounted drives.

~ # mount | grep /etc/hosts
/tmp/hosts on /etc/hosts type tmpfs (rw,nosuid,nodev,noexec,bind)

That made me surprised. After digging all the scripts that I've used, I found this line in a hosts modifier script

sudo mount -o bind /tmp/hosts /etc/hosts

Then I tried something else:

/tmp # echo aaa > a
/tmp # mount -o bind a b; cat b
aaa
/tmp # echo bbb > b; cat a
bbb
/tmp # echo ccc > c
/tmp # mv c b
mv: Device or resource busy

The solution is, of course, cancel that mount point:

sudo umount /etc/hosts
iBug
  • 3,508
-3

It is likely you don't have write access to the directory. The file operations: create, delete and rename all modify the directory.

cat and vim just modify the file contents and don't need to modify the file. sed -i writes a temporary file and renames it.

BillThor
  • 8,965
  • 3
    If I didn't have write access to thw directory, it would have said Permission denied instead of Device or resource busy. Also, sed wouldn't have been able to create the temp file without write access to the directory. – iBug Apr 24 '17 at 05:46