I find myself moving reasonably large amount of data (20+ GB) from one directory tree to other. Often times they are on the same filesystem but sometimes they are on different ones. I do cp just to preserve the original data just-in-case. Once the copy is done I delete the original data after verifying that the data has been copied alright. Sometimes I just do mv if I feel too lazy to clean original data afterwards. However, I am wondering, from purely technical point of view, which operation is more efficient? Why?
Asked
Active
Viewed 1.8k times
9
Ketan Maheshwari
- 9,054
- 6
- 40
- 53
-
2When moving data across filesystems, neither is more efficient - they're about the same. the `cp`,verify,`rm` strategy is safer because it is easier to recover from an unexpected error - e.g. the `mv` or `cp` failing partway through due to disk full error or (if the destination fs is a network share) the network going down, etc. With `cp` you still have your complete original data in its original directory structure. With `mv` you have some of your data in the original dir, some in the target dir, and re-merging them may be difficult (esp. if the target dir contains other data). – cas Apr 19 '16 at 07:56
-
4BTW if `cp` fails before completion due to error, once you have resolved the error you can complete the `cp` from where it left off by using `rsync`. In fact, you can use `rsync` instead of `cp` right from the start. – cas Apr 19 '16 at 07:58
1 Answers
16
Technically, mv is not atomic when the source and destination are on different filesystems, it is actually cp + unlink(). So at first mv will copy the file and then call unlink() to remove the file from the directory's list of entries.
So in this case, AFAIU whether you cp and then rm (unlink()) or use mv directly is totally your personal preference.
Whereas while mv -ing within the same filesystem, you should use mv as its atomic within the same filesystem (calls rename()) so less overhead.
Thanks @muru and @psusi for the pointing out the FS dependent behavior.
heemayl
- 54,820
- 8
- 124
- 141
-
3
-
2
-
-
Nice post about that behaviour: http://unix.stackexchange.com/questions/193436/are-there-any-dangers-in-using-mv-instead-of-cp-followed-by-rm – muru Apr 19 '16 at 02:14
-
In the case of `mv` in file transfers could the `unlink()` part sometimes fail? – Viesturs Dec 29 '17 at 12:43
-
1
-
What if I call `mv file1 file2` and the transmission (`cp` part) fails. Will `unlink()` be called in this case? – Viesturs Dec 29 '17 at 12:49
-
1