Linux Move command  “mv” - To move files & directories

Linux Move command “mv” - To move files & directories

The “mv” command:

The “mv” command is used to move a directory or file from one location to another in Linux system. “mv” command is like “cp” command but it does not make any copy of a file in linux system. the “mv” command is installed in linux system by default. you can use the “mv” command in many fields such as moving file or directories, rename directories etc .

Usages of “mv” command:

1. Move file:

If you want to move a file from one destination to another then you can use the following command. The requirement of moving file is the destination file location where source file is moved. For example we want to move a file file1.txt to another directory.

Syntax:

$ mv file_1.txt /home/pungki/office.


When you move the file from one directory to another directory than the file is deleted from source directory.

2. For Moving one or more file:

With the help of “mv” command you can also move the one or more files one directory to another. You can move the multiple files using the “mv” commands in single line by using spaces.

Syntax:

$ mv file2.txt file3.txt file4.txt /home/pungki/office.

OR

$ mv *.txt /home/pungki/office.

3. Move Directory:

You can move the directory by simply using the mv commands without any option.

Syntax:

$ mv directory_1/ /home/pungki/office

4. Rename File or Directory:

You can also use the “mv” command for renaming the file or directory. For renaming the file you should have same destination location and source location but the name of file should be different. For example you are in /home/pungki/office directory and you want to rename the abc.txt file into xyz.txt file then use the given command.

Syntax:

$ mv /home/pungki/documents/abc.txt /home/pungki/documents/xyz.txt

If you have not absolute path then run the given commands.

$ mv abc.txt xyz.txt.

If you want to rename the directory than use the given command

Syntax:

$ mv directory_1/directory_2.

5. Print Output:

If you want to see the result of rename or move files without see the destination location than you can use – v option with mv command.

Syntax:

$ mv –v *.txt /home/pungki/office.

6. Interactive mode:

In some situation, you moved a file into another then if a file is already exit with same name then it overwrite the file without any notification. If you want to some alert for already existing file than you can use the – I option with mv command.

Syntax:

$ mv –i file1.txt /home/pungki/office.

After run this command a notification will display in front of you about existence of file. If you press ‘y’ then it move the file otherwise not.

7. No overwrite the existing file:

The –i option is used to allow you overwrite the file than –n option is not allow you to overwrite the existing file. Use the given commands

Syntax:

$ mv – vn *.txt /home/pungki/office.

8. Backup of Moving File:

In case when you are moving a file and it is a wrong file and destination file is already overwritten by another new file. You can retrieve the old destination file before overwritten by using the – b option. This option keeps the backup of destination file before overwritten.

Syntax:

$ mv –bv *.txt /home/pungki/office.

If the files have a name with (~) sign than it means those files are backup file.
Author
Jaishree
Views
5,504
First release
Last update
Rating
0.00 star(s) 0 ratings
Top