Move files and directories using the MV command in Linux

Home > Search > Linux commands
  by

The mv command can be used to move a file from one directory to another directory. In this example, file1 is moved from /home/john.doe to /tmp.

[john.doe@server1 ~]# mv /home/john.doe/file1 /tmp

 

In this example, file1 will no longer reside in the /home/john.doe directory, and instead will only reside in the /tmp directory.

[john.doe@server1 ~]# ls /home/john.doe

[john.doe@server1 ~]# ls /tmp
file1

 

When the target directory and new file name is provided, the new file will have the new file name. In this example, file1 is moved and the new file name is file2.

[john.doe@server1 ~]# cp /home/john.doe/file1 /tmp/file2

 


Change file name

The mv command followed by the current file name and then a new file name can be used to create a new file with a different name. In this example, file1 is moved and renamed to file2. This can be dangerous. If file2 already exists, file2 will be overwritten with no prompt or warning. 

[john.doe@server1 ~]# cp file1 file2

 

In this example, only file2 will exist. 

[john.doe@server1 ~]# ls /home/john.doe
file2

 


Interactive prompt

The -i or --interactive option can be used to display an interactive prompt. The interactive prompt can prevent a file from being overwritten. In this example, a file named file1 already exists in both the /home/john.doe and /tmp directories. When attempting to move file1 from /home/john.doe to /tmp, a prompt appears.

[john.doe@server1 ~]# cp /home/john.doe/file1 /tmp
cp: overwrite '/tmp/file1'?

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments