cp vs. mv: which operation is more efficient?
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.
Related videos on Youtube
Ketan Maheshwari
Updated on September 18, 2022Comments
-
Ketan Maheshwari almost 2 years
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 domv
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?-
Alessio about 8 yearsWhen 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. themv
orcp
failing partway through due to disk full error or (if the destination fs is a network share) the network going down, etc. Withcp
you still have your complete original data in its original directory structure. Withmv
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). -
Alessio about 8 yearsBTW if
cp
fails before completion due to error, once you have resolved the error you can complete thecp
from where it left off by usingrsync
. In fact, you can usersync
instead ofcp
right from the start.
-
-
muru about 8 years(…across filesystems). In the same filesystem, it's atomic.
-
psusi about 8 yearsMore specifically, in the same filesystem it simply calls
rename()
. -
heemayl about 8 years@muru Didn't know that..thanks..edited..
-
muru about 8 yearsNice post about that behaviour: unix.stackexchange.com/questions/193436/…
-
Viesturs over 6 yearsIn the case of
mv
in file transfers could theunlink()
part sometimes fail? -
heemayl over 6 years@Viesturs Of course e.g. permission issue.
-
Viesturs over 6 yearsWhat if I call
mv file1 file2
and the transmission (cp
part) fails. Willunlink()
be called in this case? -
heemayl over 6 years@Viesturs Nope.