14
  • mv a b

  • cp a b;rm a

These are two sets of statements. Is there some difference between what they do?

4
  • 1
    atomicity? I don't really know if the mv command is atomic, though. Jan 28, 2010 at 13:13
  • Yes. Why do you ask?
    – S.Lott
    Jan 28, 2010 at 13:14
  • 1
    Now, mv is similar to ln + rm, though, the former will work for cross-filesystem moves (which then just becomes cp + rm), whereas the latter will fail at ln (which does not support cross-filesystem hard links). Jan 28, 2010 at 13:16
  • Every day is a school day - for anybody coming to this wondering how to override this default behaviour you need the --remove-destination switch - stackoverflow.com/a/9371263/409638
    – robert
    Feb 4, 2016 at 12:35

8 Answers 8

20

Assuming the files involved are on the same file system, then mv simply changes pointers in the file system, whereas cp copies the entire contents of the file, and rm once again changes pointers. So mv is far more efficient.

4
  • 1
    but they do the exact same thing, right?
    – Lazer
    Jan 28, 2010 at 13:15
  • 8
    No, they don't.
    – nb2580
    Jan 28, 2010 at 13:17
  • 3
    @eSKay: No. Two scenarios: 1. The target does not exist. mv just renames the file (same inode as original file). cp makes a new inode for the new file. 2. The target does exist. mv unlinks (removes) the target file, and renames, in one step. cp overwrites the target file's inode. Jan 28, 2010 at 13:19
  • 8
    Also mv never changes permission or ownership. Imagine you want to move a file from your buddy's homedir to your own homedir. If you mv it you will end up with a file in your homedir that belongs to your buddy. You can't chown or chmod and depending on the permissions not even modify or read it. If you do cp followed by rm the file will belong to you and everything is fine. Jan 28, 2010 at 19:16
9

Yes, mv has a chance of being atomic on the same disk, whereas the combination of cp and rm never has.

This is assuming that mv is implemented using rename(), which is the call that has the guarantee. See, for instance, this newsgroup post, which quotes POSIX:

This rename() function is equivalent for regular files to that defined by the ISO C standard. Its inclusion here expands that definition to include actions on directories and specifies behavior when the new parameter names a file that already exists. That specification requires that the action of the function be atomic.

8

On the same file system mv changes the directory reference, pointing to the same inode (file data and metadata) thus:

  • is an atomic operation (it cannot be interrupted by another process file operation)
  • takes only a trivial amount of additional disk space (the additional name in the directory)
  • preserves file permissions and ownership
  • can be much faster, depending on amount of data

Copy and remove

  • is not atomic (another process could interfere between the cp and rm commands)
  • requires storing the file data twice on disk for a short period (between the cp and rm commands)
  • changes file permissions and ownership to defaults
  • can be much slower or even fail, depending on amount of data
4

When the source and the destination are on the same physical volume, then the first approach is simply a rename and is very fast (even if the file(s) are very big).

cp & rm will always have to load/store all the data, even if it weren't necessary.

3

mv is in essence a "rename" operation. This means the file itself is left in the same spot on disk. No actual file operation is performed.

3

The difference is that mv conserves file-attributes while cp by default doesn't, for example setting creation-date to the current date.

To override this default, use "cp -p" to preserve the last data modification, the time of the last access, the user ID and group ID (only if it has permissions to do this), file permission bits and the SUID and SGID bits.

2

Yes.

mv simply changes the filesystem metadata on the file relating to it's name and location, whereas cp creates a seperate copy of the file, which takes much longer as it must fully read the first file and then write it's contents to another file

2

cp and rm is a much heavier on the disk usage, and may fail for disk space reasons.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .