Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
  • mv a b

  • cp a b;rm a

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

share|improve this question
1  
atomicity? I don't really know if the mv command is atomic, though. –  Adriano Varoli Piazza Jan 28 '10 at 13:13
    
Yes. Why do you ask? –  S.Lott Jan 28 '10 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). –  Chris Jester-Young Jan 28 '10 at 13:16
add comment

migrated from stackoverflow.com Jan 28 '10 at 13:20

This question came from our site for professional and enthusiast programmers.

8 Answers

up vote 15 down vote accepted

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.

share|improve this answer
    
but they do the exact same thing, right? –  Lazer Jan 28 '10 at 13:15
4  
No, they don't. –  nb2580 Jan 28 '10 at 13:17
1  
@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. –  Chris Jester-Young Jan 28 '10 at 13:19
1  
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. –  Ludwig Weinzierl Jan 28 '10 at 19:16
add comment

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.

share|improve this answer
add comment

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
share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.