Home » Questions » Computers [ Ask a new question ]

Is there some difference between mv and {cp + rm the old file} on Unix?

Is there some difference between mv and {cp + rm the old file} on Unix?

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

Asked by: Guest | Views: 68
Total answers/comments: 5
Guest [Entry]

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.
Guest [Entry]

"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"
Guest [Entry]

"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."
Guest [Entry]

"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"
Guest [Entry]

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