0

I am trying to copy/move a large file (15 GB) to a directory in Linux and want to have a dependency on that event. Now lets say I have a file named abc.txt, and I am running below command:

mv /usr/tmp/abc.txt /usr/data/

When the move process start I see a file in data directory with the actual file name i.e. abc.txt but with data still being in transit. As the data directory list the file abc.txt in its directory my dependent process thinks that the file is available and it start the dependent process however the file is not completely moved and hence my dependent process triggers prematurely.

Is there a way I can move a file with transient name i.e. while the data transfer is going on it will use a transient name(some swap file name) and change the name to actual file when it is completely transferred?

  • Is the dependent application under your control? If yes, using inotify and waiting for a CLOSE event on the file would be a viable approach. – Hermann Mar 1 at 16:52
2

You must be moving between two different filesystems, so in effect the file is copied. Try to first copy it then, and after that's done, move within the destination. This should do:

mv /usr/tmp/abc.txt /usr/data/.abc.txt && mv /usr/data/.abc.txt /usr/data/abc.txt

I assume your watching process won't recognise the hidden file. Otherwise you could make a temp directory at the target location or something similar.

  • Yeah it was between different file system so I think that's why it was not using transient name. Thanks for your response. – Explorer Mar 1 at 17:01
  • @Explorer Your assumption "that's why it was not using transient name" is not correct. The mv command will never use a transient name. The difference is that it can rename the file if source and destination are in the same file system and it must copy the source to the destination followed by removing the source if not. – Bodo Mar 1 at 17:29
  • @Tomasz I tried moving file between 2 directories in same file system and it was fast so I was not able to see whether it was using using transient name or not but when I copied the file it was not using the transient name. Is that the case? – Explorer Mar 1 at 17:36
  • Neither mv nor cp use any transient names. @Explorer – Tomasz Mar 1 at 17:40
  • Oh ok so the operation /usr/data/.abc.txt /usr/data/abc.txtis one bit operation? (as files is on same filesystem) – Explorer Mar 1 at 21:12

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.