Search code examples
linuxunixsharecp

Linux cp command, temporary naming the file until copy is done copying the whole file to destination


is there a way, when we use copy command on Linux :

$ cp ../dir_a/a.txt ../dir_b/a.txt

To name file a.txt to a.tmp.txt when copy start copying then renaming it automatically to a.txt original name when all the file is copied ? i have a java program that consume this file too fast before it is copied completely from share to share and thus note consuming all the file :

$ cp ../dir_a/a.txt ../dir_b/a.txt
$ ls ../dir_b/ #copying in progress
$ a.tmp.txt
$ ls ../dir_b/ #copying done
$ a.txt

Thank you,


Solution

  • Copy to the temporary name, then rename it. Renaming is atomic, so there won't be any partial contents.

    cp ../dir_a/a.txt ../dir_b/a.txt.temp
    mv ../dir_b/a.txt.temp ../dir_b/a.txt