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

Updated:   |  Linux Commands articles

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

 


Move a directory

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

mv /tmp/foo /home/john.doe

 


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.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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