Command Line · Lesson 11 of 19

mv (Move)

0 / 5 checkpoints answered

Full screen ↗

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

CheckWhich 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.

CheckWhich 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/

CheckWhich 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.

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

CheckYou 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.

Summary

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

  1. Put the source before its new pathname.
  2. Place a target directory after multiple sources.
  3. Ask, skip, or back up before replacing a destination.
  4. Move directories without a recursive option.
  5. Preview wildcard matches before a bulk move.