mv command

Linux mv command practical examples

In Linux and UNIX flavors, mv command has multiple advantages.

  • It can be used for moving the files from one directory to another directory. Like cut & paste option, we usually have on word documents.
  • It can also be used for renaming a file in a given directory.

 

Note: 

  • There is no other command in Linux/Unix for renaming the files & directories.
  • mv command can’t rename a directory.

 

Syntax:

mv  source-file-name   target-file-name

 

mv  source-file-name   target-directory

 

Let us understand this command practical use cases with below examples.

 

Example-1:

Rename a file in a directory:

[rreddy@abclearn abclearn_dir2]$ ls
abc_lab1.txt

 

[rreddy@abclearn abclearn_dir2]$ mv abc_lab1.txt newfile.txt
[rreddy@abclearn abclearn_dir2]$ ls
newfile.txt

 

Move a file from one directory to another directory

This mv command will remove the file from present directory and will made available in new directory.

[rreddy@abclearn abclearn_dir1]$ mv abc_lab1_copy.txt /home/rreddy/ebclearn_dir2/

 

If we check under abclearn_dir1 then file should be removed,

[rreddy@abclearn abclearn_dir1]$ ls
abc_lab1.txt abc_lab2.txt abc_lab3.txt  abc_lab4.txt

 

the same file is available under abclearn_dir2.

[rreddy@abclearn abclearn_dir1]$ ls /home/rreddy/abclearn_dir2/
abc_lab1_copy.txt newfile.txt

 

Note: 

If we know the directory path, then check directory content using ls command, without changing into it.

 

Move one directory as subdirectory to another

Couple of more options to try

Promoting options

-u, --update

Move only when the SOURCE file is newer than the destination file or when the destination file is missing

-b like --Backup but does not accept an argument

 

Topics Summary