Moving Files in Linux

0

 The mv command is used to move a file from one location in the filesystem to another.

mv SOURCE DESTINATION

The mv command requires at least two arguments. The first argument is the source, a path to the file to be moved. The second argument is the destination, a path to where the file will be moved to. The files to be moved are sometimes referred to as the source, and the place where the files are to be placed is called the destination.

Follow Along

Use the following command to switch to the Documents directory:

sysadmin@localhost:~$ cd ~/Documents

To move the people.csv file into the Work directory, use the filename as the source, and the directory name as the destination:

sysadmin@localhost:~/Documents$ mv people.csv Work                              

If a file is moved from one directory to another without specifying a new name for the file, it will retain its original name. The move above can be confirmed using the ls command on the Work directory:

sysadmin@localhost:~/Documents$ ls Work                                         
people.csv

The mv command is able to move multiple files, as long as the final argument provided to the command is the destination. For example, to move three files into the School directory:

sysadmin@localhost:~/Documents$ mv numbers.txt letters.txt alpha.txt School        
sysadmin@localhost:~/Documents$ ls School                                       
Art  Engineering  Math  alpha.txt  letters.txt  numbers.txt  

Moving a file within the same directory is an effective way to rename it. For example, in the following example the animals.txt file is given a new name of zoo.txt:

mv animals.txt zoo.txt
sysadmin@localhost:~/Documents$ ls                                              
School           alpha-second.txt  hello.sh      newhome.txt  red.txt           
Work             alpha-third.txt   hidden.txt    os.csv                         
adjectives.txt   animals.txt       linux.txt     passwd                         
alpha-first.txt  food.txt          longfile.txt  profile.txt 
sysadmin@localhost:~/Documents$ mv animals.txt zoo.txt                          
sysadmin@localhost:~/Documents$ ls                                              
School           alpha-second.txt  hidden.txt    os.csv       zoo.txt           
Work             alpha-third.txt   linux.txt     passwd                         
adjectives.txt   food.txt          longfile.txt  profile.txt                    
alpha-first.txt  hello.sh          newhome.txt   red.txt   

Consider This

Permissions can have an impact on file management commands, such as the mv command. Moving a file requires write and execute permissions on both the origin and destination directories

Post a Comment

0Comments
Post a Comment (0)