mv (Move)
100%

Command Line · Lesson 11

mv (Move)

Learn how to rename and move files or directories while avoiding unintended overwrites.

The mv command renames a file or directory, or moves it to another location. Unlike cp, it does not leave the original pathname in place after a successful move.

The basic syntax is:

mv [OPTIONS] SOURCE DESTINATION

Renaming Files and Directories

To rename an item, put its current pathname first and its new pathname second.

To rename a file:

$ mv oldfile newfile

The same operand order renames a directory:

$ mv old_directory_name new_directory_name

Which command renames cat to dog in the current directory?

Moving Items to a Directory

When the final operand is an existing directory, mv places the source inside it:

$ mv file2 /home/pete/Documents

To move several sources, list them first and put the target directory last:

$ mv file_1 file_2 somedirectory/

GNU mv also provides -t for placing the target directory before the sources:

$ mv -t somedirectory/ file_1 file_2

Unlike cp, mv does not need a recursive option for a directory.

Which command moves both file_1 and file_2 into the existing archive/ directory?

Controlling Existing Destinations

By default, mv can replace an existing destination. Inspect source and destination pathnames before running a move, then choose an overwrite policy when necessary:

  • -i: Ask for confirmation before replacing an existing destination.

    $ mv -i source_file destination_directory
    
  • -n: Do not overwrite an existing destination.

    $ mv -n source_file destination_directory
    
  • -b: On GNU/Linux, make a backup of a destination that would otherwise be replaced. The default backup suffix is usually ~.

    $ mv -b file1 directory_with_file1
    
  • -v: Print each move as it happens.

$ mv -v file1 file2 somedirectory/

Which command moves draft.txt into finished/ only when it will not overwrite an existing destination?

Moving Directories and Wildcard Matches

A directory can be moved without -r:

$ mv project /home/pete/Documents/

Shell wildcards can select multiple sources:

$ ls *.txt
$ mv *.txt notes/

Previewing the matches with ls helps you catch an overly broad pattern before changing several pathnames.

Which command moves the project/ directory into /srv/archive/?

You plan to run mv *.txt notes/. Which command previews the pathnames selected by the same wildcard?

To practice moving and renaming items, try these hands-on labs:

  1. Linux mv Command: File Moving and Renaming - Practice using the mv command to move and rename files and directories, including understanding its various options and behaviors.
  2. Organizing Files and Directories - Apply your knowledge of mv (along with cp and rm) in a practical challenge to organize a project structure, move files, and clean up directories.

Lesson complete

You finished mv (Move)

You can now rename and move files or directories while protecting existing destinations.

  • Put the source before its new pathname.

  • Place a target directory after multiple sources.

  • Ask, skip, or back up before replacing a destination.

  • Move directories without a recursive option.

  • Preview wildcard matches before a bulk move.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Command Line