How to copy a file in creation without destroying it in Unix/Linux How to copy a file in creation without destroying it in Unix/Linux unix unix

How to copy a file in creation without destroying it in Unix/Linux


No, reading from a file that is currently being written to will not disrupt that writing process. However, the copy you take will be incomplete, as obviously you can't copy parts that haven't been written yet. Only the first file will get subsequent writes, not the copy, so if you want the entire file, wait until the writing is complete.


Unless the second process uses a file descriptor associated with the same open file description as the process creating the file, there will be no problem. If the file descriptor does share the same open file description, then the read process will move the file position (as will the write process), leading to confusion. However, that requires the processes to have a common ancestor that opened the file (for example, the copier is forked by the creator). This is unlikely to be the case, I think, from your description.

Your copying process can do as tail -f does and read up to the first EOF, then pause and read again when there is more data available, repeating until told to stop, or it can determine that the file is complete. If it has no way of telling when the file is complete, then you'll have to kill it (interrupt, or worse).


Depending on what exactly the semantics of your operation should be, links may be what you need. Check out the behaviour of symbolic and hard links. One of the two may be exactly what you want.