cp vs. mv: which operation is more efficient?

cp mv
15,230

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.

Share:
15,230

Related videos on Youtube

Ketan Maheshwari
Author by

Ketan Maheshwari

Updated on September 18, 2022

Comments

  • Ketan Maheshwari
    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 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?

    • Alessio
      Alessio about 8 years
      When 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).
    • Alessio
      Alessio about 8 years
      BTW 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.
  • muru
    muru about 8 years
    (…across filesystems). In the same filesystem, it's atomic.
  • psusi
    psusi about 8 years
    More specifically, in the same filesystem it simply calls rename().
  • heemayl
    heemayl about 8 years
    @muru Didn't know that..thanks..edited..
  • muru
    muru about 8 years
    Nice post about that behaviour: unix.stackexchange.com/questions/193436/…
  • Viesturs
    Viesturs over 6 years
    In the case of mv in file transfers could the unlink() part sometimes fail?
  • heemayl
    heemayl over 6 years
    @Viesturs Of course e.g. permission issue.
  • Viesturs
    Viesturs over 6 years
    What if I call mv file1 file2 and the transmission (cp part) fails. Will unlink() be called in this case?
  • heemayl
    heemayl over 6 years
    @Viesturs Nope.