Moving files and directories: mv

To move files and directories from one place to another use the mv (move) command:

   mv [option] filename1 filename2
               directory1 directory2
               filename directory

You can also change the name of a file or directory by moving it.

**Caution**

Take care what name you give to the file or directory that the file or directory is being moved to. If you choose the name of a file or directory that already exists it will be replaced by the file or directory that has been moved to its name! To avoid this, use the mv command together with its -i option. This will prompt you for confirmation whenever the moved file or directory would overwrite an existing file or directory.

Examples

To rename a file:

   mv junk precious

This renames the file junk as the file precious.

To be prompted when using the mv command:

    mv -i draft1.txt part1.txt
    remove part1.txt? n

This command prompts you to confirm that you really want to move the file draft1.txt to the file part1.txt; a file with that name already exists. Entering n cancels this action: a y confirms it.

To move a file to another directory:

    mv precious treasure

This moves the file precious from the current directory to the directory treasure.

To move a file to another directory and rename it:

   mv jewel casket/amethyst

This moves the file jewel to the filename amethyst in the subdirectory casket.

To move and rename a directory:

   mv vr1.1 ../environments/virtual_rooms

This moves the directory vr1.1 from the current directory to the directory ../environments and renames it as virtual_rooms.