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).

  • 2
    Possible duplicate of How to get over "device or resource busy"? – Yaron Apr 23 '17 at 14:49
  • Try: mv ./tmp foo – Mike Waters Apr 23 '17 at 14:50
  • 2
    What filesystem and OS is this? – ilkkachu Apr 23 '17 at 14:54
  • 3
    The difference between vim and cat on the one hand, and sed -i and mv on the other hand, is that cat >/some/foo writes to the existing file, whereas mv /some/foo /some/bar renames the file. This file can be written to, but can't be renamed. To understand why the file can't be renamed, we'd need to know more about this file and the filesystem that it's on and what OS you're running. – Gilles 'SO- stop being evil' Apr 23 '17 at 22:10

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
/tmp # echo bbb > b; cat a
/tmp # echo ccc > c
/tmp # mv c b
mv: Device or resource busy

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.

  • 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 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.