1

I would like to know if I use the CP or MV command as root to copy a file to a new directory, which already contains that file and is currently open by another user, then would the operation succeed or would it be blocked as it is already in use?

I have tried testing this on a VM with root overwriting a file also open by root in another terminal window, and it does succeed but I'm wondering if this is only because it is the same user.

My specific circumstances will actually be root overwriting the file if someone on a windows machine has it open through an SMB share. I can't currently test it this way.

If it will not succeed when the file is open, then what if I set it so it is read only by all accept the root user?

A bit more info:

I am working on a simple system to create csv files which contains all of our orders for a given month, and simply add on to the end of the file when new orders come in. I and the root user (cronjob running php files) will be the only ones who need to edit the files.

However other people do look at them to find specific orders from time to time. At the minute I manually edit the files and can't save to them if another user has it open, so I'm wondering if I will experience the same problems if they are stored on a linux machine with root user doing the editing.

1
  • After testing on Ubuntu, cp and mv will overwrite an open file, including one being written. I must assume that they don't use the O_EXCL when they open either source of target files. I assume your editor is doing this. A different editor may be able to open files - if not, you can cp FilePath FilePath.tmp, edit FilePath.tmp, and mv FilePath.tmp FilePath.
    – AFH
    Apr 7, 2017 at 15:52

1 Answer 1

1

Yes, cp and mv will overwrite the open files. Perhaps you could look into version control with something like Git or something similar.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .