a) mv a b
b) cp a b;rm a
These are two sets of statements. Is there some difference between what they do?
|
a) b) These are two sets of statements. Is there some difference between what they do?
| |||
|
feedback
|
This question came from our site for professional and enthusiast programmers.
|
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. | |||||||||||||||
feedback
|
|
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
| |||
|
feedback
|
|
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).
| |||
|
feedback
|
|
On the same file system mv changes the directory reference, pointing to the same inode (file data and metadata) thus:
Copy and remove
| |||
|
feedback
|
|
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. | |||
|
feedback
|
|
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 | |||
|
feedback
|
|
cp and rm is a much heavier on the disk usage, and may fail for disk space reasons. | |||
|
feedback
|
|
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. | |||
|
feedback
|
mvis 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). – Chris Jester-Young Jan 28 '10 at 13:16