Tuesday, May 28, 2013

Difference between mv and cp command in Linux


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 will  not delete the original file means original file remain as it is.


[rajani@localhost ~]$ cat > myfile
This is my 1st line
This is my 2nd line
[rajani@localhost ~]$ ls -l
total 28
-rw-r--r-- 1 rajani users   0 May 27 19:37 cha02
-rw-r--r-- 1 rajani users   0 May 27 19:37 chap01
-rw-r--r-- 1 rajani users   0 May 27 19:37 chap03
-rw-r--r-- 1 rajani   504 141 May 27 23:07 dbhome.txt
-rw-r--r-- 1 rajani users  40 May 28 09:00 myfile  
[rajani@localhost ~]$ 

[rajani@localhost ~]$ cp myfile myfilenew
[rajani@localhost ~]$ ls -l
total 36
-rw-r--r-- 1 rajani users   0 May 27 19:37 cha02
-rw-r--r-- 1 rajani users   0 May 27 19:37 chap01
-rw-r--r-- 1 rajani users   0 May 27 19:37 chap03
-rw-r--r-- 1 rajani   504 141 May 27 23:07 dbhome.txt
-rw-r--r-- 1 rajani users  40 May 28 09:00 myfile
-rw-r--r-- 1 rajani users  40 May 28 09:01 myfilenew
[rajani@localhost ~]$ mv myfile myfilenn
[rajani@localhost ~]$ ls -l
total 36
-rw-r--r-- 1 rajani users   0 May 27 19:37 cha02
-rw-r--r-- 1 rajani users   0 May 27 19:37 chap01
-rw-r--r-- 1 rajani users   0 May 27 19:37 chap03
-rw-r--r-- 1 rajani   504 141 May 27 23:07 dbhome.txt
-rw-r--r-- 1 rajani users  40 May 28 09:01 myfilenew
-rw-r--r-- 1 rajani users  40 May 28 09:00 myfilenn
[rajani@localhost ~]$

cp command created a new file myfilenew with new timestamp but didn't delete the myfile.

mv command renamed the existing file with no timestamp change to myfilenn and deleted the old file myfile.

cheers
Rajani


No comments:

Post a Comment