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 2.0k times
9
Ketan
- 9,226
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
- 56,300
-
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
mvin file transfers could theunlink()part sometimes fail? – Viesturs Dec 29 '17 at 12:43 -
1
-
What if I call
mv file1 file2and the transmission (cppart) fails. Willunlink()be called in this case? – Viesturs Dec 29 '17 at 12:49 -
1
cp,verify,rmstrategy is safer because it is easier to recover from an unexpected error - e.g. themvorcpfailing partway through due to disk full error or (if the destination fs is a network share) the network going down, etc. Withcpyou still have your complete original data in its original directory structure. Withmvyou 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:56cpfails before completion due to error, once you have resolved the error you can complete thecpfrom where it left off by usingrsync. In fact, you can usersyncinstead ofcpright from the start. – cas Apr 19 '16 at 07:58