mv a b
cp a b
;rm a
These are two sets of statements. Is there some difference between what they do?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityAssuming 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.
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
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
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.
On the same file system mv changes the directory reference, pointing to the same inode (file data and metadata) thus:
Copy and remove
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.
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.
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.
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
cp and rm is a much heavier on the disk usage, and may fail for disk space reasons.
mv
is similar toln
+rm
, though, the former will work for cross-filesystem moves (which then just becomescp
+rm
), whereas the latter will fail atln
(which does not support cross-filesystem hard links).--remove-destination
switch - stackoverflow.com/a/9371263/409638