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
  • move a directory
  • rename a file
  • rename a directory


Move a file

In this example, /home/john.doe/foo.txt is moved to the /tmp directory.

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


Or, this would have the same result.

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


Rename a file

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


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