FreeKB - Linux Commands mv (move)
Linux Commands - mv (move)

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

mv /home/john.doe/foo.txt /tmp


foo.txt 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


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, foo.txt is moved and renamed to bar.txt. This can be dangerous. If bar.txt already exists, bar.txt will be overwritten with no prompt or warning. 

mv foo.txt bar.txt


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 foo.txt 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.

mv -i /home/john.doe/foo.txt /tmp
mv: overwrite '/tmp/foo.txt'?


Last modified date and time

Moving a file will not update the last modified date and time of the file. On the other hand, the cp (copy) command is used, the last modified date and time of the new file will be the current date time.

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 18bc3 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |