What are the differences between MV and CP command?


This article includes the mv and cp commands with the examples to differentiate them based on deletion of file and the time stamp. These are very useful command in Unix for copying and moving files with condition, whether original file is required or not.

mv command in Unix: mv is used to move or rename the files but it will delete the original file while moving.

cp command in Unix: cp is used to copy the files but like mv it's not delete the original file means original file remain as it is.

To differentiate between mv and cp let's assume we have two files test and test1 as shown below:

-rw-r--r-- 1 kamlesh kamlesh 8 2011-08-18 23:55 test
-rw-r--r-- 1 kamlesh kamlesh 6 2011-08-18 23:56 test1

here the time of creation of test is 23:55 and 23:56 for the test1.

If we do

"mv test test_n"

then 1) test file will be deleted means no more present.
2) The time stamp will be remain same means 23:55.

-rw-r--r-- 1 kamlesh kamlesh 8 2011-08-18 23:55 test_n


And if we do

"cp test1 test1_n"

then 1) test1 file will not be deleted means it's present.
2) The time stamp will be changed to the time of copying.


-rw-r--r-- 1 kamlesh kamlesh 8 2011-08-18 23:55 test_n
-rw-r--r-- 1 kamlesh kamlesh 6 2011-08-18 23:56 test1
-rw-r--r-- 1 kamlesh kamlesh 6 2011-08-18 23:58 test1_n

In short if we want original file as well then go for cp else go for mv so that original file will be deleted.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: