Avatar of gudii9
gudii9Flag for United States of America

asked on 

mv vs cp

hi,

what is difference between mv vs cp

when we use which one. any advantage of using one over other

please advise
LinuxLinux NetworkingLinux OS DevUnix OSLinux Distributions

Avatar of undefined
Last Comment
gudii9
ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Seth Simmons
Seth Simmons
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of Hugh Fraser
Hugh Fraser
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

Move the file will join longer be accessible in the old location.

you mean as below right?
Move the file will no longer be accessible in the old location.
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

First, moving a file (within the same filesystem) never actually moves the contents of the file itself; it simply moves the directory entry. For a large file, this is much faster.
i am not clear on above. can you please elaborate?
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

mv and cp can be applied to directories and files as well?
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

just tested mv  as below on file
sh-4.4$ /bin/bash                                                                                                                                  
bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ ls -ltr                                                                                                                                  
total 4                                                                                                                                            
-rw-r--r-- 1 14392 14392 978 Nov 22 16:46 README.txt                                                                                              
bash-4.4$ touch 123.txt                                                                                                                            
bash-4.4$ touch 456.txt                                                                                                                            
bash-4.4$ vi 123.txt                                                                                                                              
bash-4.4$ mv 123.txt 456.txt                                                                                                                      
bash-4.4$ ls -ltr                                                                                                                                  
total 8                                                                                                                                            
-rw-r--r-- 1 14392 14392 978 Nov 22 16:46 README.txt                                                                                              
-rw-r--r-- 1 14392 14392   7 Nov 22 16:47 456.txt                                                                                                  
bash-4.4$ vi 456.txt                                                                                                                              
                                                                                                                                                   
[1]+  Stopped                 vi 456.txt
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

so mv is for renaming files and directories

i tested renaming directory as below

bash-4.4$ pwd                                                                                                                                      
/home/cg/root                                                                                                                                      
bash-4.4$ ls -ltr                                                                                                                                  
total 4                                                                                                                                            
-rw-r--r-- 1 14392 14392 978 Nov 22 16:46 README.txt                                                                                              
bash-4.4$ touch 123.txt                                                                                                                            
bash-4.4$ touch 456.txt                                                                                                                            
bash-4.4$ vi 123.txt                                                                                                                              
bash-4.4$ mv 123.txt 456.txt                                                                                                                      
bash-4.4$ ls -ltr                                                                                                                                  
total 8                                                                                                                                            
-rw-r--r-- 1 14392 14392 978 Nov 22 16:46 README.txt                                                                                              
-rw-r--r-- 1 14392 14392   7 Nov 22 16:47 456.txt                                                                                                  
bash-4.4$ vi 456.txt                                                                                                                              
                                                                                                                                                   
[1]+  Stopped                 vi 456.txt                                                                                                          
bash-4.4$ mkdir dir1                                                                                                                              
bash-4.4$ ls -ltr                                                                                                                                  
total 12                                                                                                                                          
-rw-r--r-- 1 14392 14392  978 Nov 22 16:46 README.txt                                                                                              
-rw-r--r-- 1 14392 14392    7 Nov 22 16:47 456.txt                                                                                                
drwxr-xr-x 2 14392 14392 4096 Nov 22 16:49 dir1                                                                                                    
bash-4.4$ mv dir dir2                                                                                                                              
mv: cannot stat 'dir': No such file or directory                                                                                                  
bash-4.4$ mv dir1 dir2                                                                                                                            
bash-4.4$ ls -ltr                                                                                                                                  
total 12                                                                                                                                          
-rw-r--r-- 1 14392 14392  978 Nov 22 16:46 README.txt                                                                                              
-rw-r--r-- 1 14392 14392    7 Nov 22 16:47 456.txt                                                                                                
drwxr-xr-x 2 14392 14392 4096 Nov 22 16:49 dir2                                                                                                    


as per this link
http://www.thegeekstuff.com/2010/12/mv-command-examples/
Avatar of arnold
arnold
Flag of United States of America image

mv is to move files or directories.
if you move a file or directory from one name to another while at the same time moving it from one location to another,
you would be renaming the file or the directory while relocating it in a single command.
versus rename first, then move.
/home/myusernmae/myfile
need to be in
/home/userusername/yourfile
mv /home/myusernmae/myfile /home/yourusername/yourfile
will do this in a single step provided that I have write access into your homedirectory which is commonly no permitted.

It is difficult to understand what you are after in the way you form your question and in the way you copy/paste information in support of what you are asking.

most linux systems have a rename command. but mv achieves the same goal when moved within the same location.
if you want dir1 to be within dir2
mv dir1 dir2/ will relocate dir1 to be within dir2. since dir2 did not exist prior to your issuing the command, mv interpreted what you did as a move. if dir2 existed, you would get dir1 moved into dir2.
Avatar of Hugh Fraser
Hugh Fraser
Flag of Canada image

Maybe a bit of filesystem basics will help. A linux filesystem is just a list of pointers (called inodes), and a pool of free blocks. An inode contains a pointer to the first set of blocks allocated to a file. As files grow, more blocks are grabbed from the free pool. But other programs are also creating files at the same time grabbing free blocks from the pool. Since the new set of blocks aren't necessarily adjacent to the previous set in the file, each set also contains a pointer to the next set. So a file is just an inode that contains a pointer to the start of a linked list of blocks.

Theoretically, that's all that's needed. You can open a file by it's inode number if you know it, but it's kind of like an unlisted phone number. So another file is created to contain a directory of file names and pointers to their inodes. This is a directory file. It's just another data file; only the contents are different. There's one for each directory and subdirectory in the filesystem. And since a directory is just a file like any other, a subdirectory is just another entry in its parent directory file. Of course, there's a bit more to it than this, but in a discussion of mv vs cp, this is enough to explain how it works.

So moving a file doesn't change its inode... it just changes which directory file contains the pointer to it's inode. In the same way, moving an entire directory (which is just another file) doesn't change its inode, just which parent directory file points to its inode.

Copying a file, on the other hand, means creating an entirely new file and making a copy of all the contents.

Deleting a file involves removing the directory entry, returning the blocks to the free pool, and finally marking the inode as unused.

BTW,  the chaining together of sets of blocks as a file grows is what leads to fragmentation, so there's lots of sophistication i've glossed over in this explanation to ensure a filesystem performs well.
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

if you want dir1 to be within dir2
mv dir1 dir2/ will relocate dir1 to be within dir2. since dir2 did not exist prior to your issuing the command, mv interpreted what you did as a move. if dir2 existed, you would get dir1 moved into dir2.
if there is no dir2 before hand what happens?

i tried creating dir1 then gave
mv dir1 dir2/
it created dir2 directory but did not moved dir1 into dir2


sh-4.4$ mkdir dir1                                                                                                                        
sh-4.4$ ls -ltr                                                                                                                          
total 8                                                                                                                                  
-rw-r--r-- 1 30410 30410  978 Dec 19 16:38 README.txt                                                                                    
drwxr-xr-x 2 30410 30410 4096 Dec 19 16:42 dir1                                                                                          
sh-4.4$ mv dir1 dir2/                                                                                                                    
sh-4.4$ ls -ltr                                                                                                                          
total 8                                                                                                                                  
-rw-r--r-- 1 30410 30410  978 Dec 19 16:38 README.txt                                                                                    
drwxr-xr-x 2 30410 30410 4096 Dec 19 16:42 dir2                                                                                          
sh-4.4$ cd dir2                                                                                                                          
sh-4.4$ ls -ltr                                                                                                                          
total 0                                                                                                                                  
sh-4.4$ pwd                                                                                                                              
/home/cg/root/dir2                                                                                                                        

please advise
Avatar of arnold
arnold
Flag of United States of America image

what do you mean, note you created dir1. you then ran mv dir1 dir2/.
note the resulting listing you have dir2, but you do not have dir1.
please note my prior explanation and note the time stamp of the new dir2 as having the same mtime stamp.

dir2 must exists for mv to move dir1 into it.
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

let me try

not sure why below link strangely redirecting to some home page instead of unix editor
http://www.tutorialspoint.com/unix_terminal_online.php

please advise
Avatar of arnold
arnold
Flag of United States of America image

see response to your other identical query. issues with a site, should be addressed to the owners of that site or requesting assistance on that site.
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

sure, it works again today
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

http://www.indiastudychannel.com/resources/144215-What-are-the-differences-between-MV-and-CP-command.aspx

now i can run both

sh-4.4$ pwd                                                                                                                                                                                                                    
/home/cg/root                                                                                                                                                                                                                  
sh-4.4$ ls -ltr                                                                                                                                                                                                                
total 4                                                                                                                                                                                                                        
-rw-r--r-- 1 18799 18799 978 Feb 20 21:36 README.txt                                                                                                                                                                          
sh-4.4$ mv dir1 dir2/                                                                                                                                                                                                          
mv: cannot stat 'dir1': No such file or directory                                                                                                                                                                              
sh-4.4$ touch test                                                                                                                                                                                                            
sh-4.4$ ls -ltr                                                                                                                                                                                                                
total 4                                                                                                                                                                                                                        
-rw-r--r-- 1 18799 18799 978 Feb 20 21:36 README.txt                                                                                                                                                                          
-rw-r--r-- 1 18799 18799   0 Feb 20 21:42 test                                                                                                                                                                                
sh-4.4$ touch test1                                                                                                                                                                                                            
sh-4.4$ mv test test_n                                                                                                                                                                                                        
sh-4.4$ ls -ltr                                                                                                                                                                                                                
total 4                                                                                                                                                                                                                        
-rw-r--r-- 1 18799 18799 978 Feb 20 21:36 README.txt                                                                                                                                                                          
-rw-r--r-- 1 18799 18799   0 Feb 20 21:42 test_n                                                                                                                                                                              
-rw-r--r-- 1 18799 18799   0 Feb 20 21:43 test1                                                                                                                                                                                
sh-4.4$ cp test1 test1_n                                                                                                                                                                                                      
sh-4.4$ ls -ltr                                                                                                                                                                                                                
total 4                                                                                                                                                                                                                        
-rw-r--r-- 1 18799 18799 978 Feb 20 21:36 README.txt                                                                                                                                                                          
-rw-r--r-- 1 18799 18799   0 Feb 20 21:42 test_n                                                                                                                                                                              
-rw-r--r-- 1 18799 18799   0 Feb 20 21:43 test1                                                                                                                                                                                
-rw-r--r-- 1 18799 18799   0 Feb 20 21:45 test1_n                                                                                                                                                                              


mv is like rename as the timestamp remain as original file
cp is changes timestamp as it keeps original file and also creates new file with same content
Linux
Linux

Linux is a UNIX-like open source operating system with hundreds of distinct distributions, including: Fedora, openSUSE, Ubuntu, Debian, Slackware, Gentoo, CentOS, and Arch Linux. Linux is generally associated with web and database servers, but has become popular in many niche industries and applications.

71K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo