Command Line · Lesson 11 of 19
mv (Move)
0 / 5 checkpoints answered
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?
press 1/2/3 to answer · → next lesson
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?
press 1/2/3 to answer · → next lesson
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?
press 1/2/3 to answer · → next lesson
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/?
press 1/2/3 to answer · → next lesson
CheckYou plan to run mv *.txt notes/. Which command previews the pathnames selected by the same wildcard?
press 1/2/3 to answer · → next lesson
To practice moving and renaming items, try these hands-on labs:
- Linux mv Command: File Moving and Renaming - Practice using the
mvcommand to move and rename files and directories, including understanding its various options and behaviors. - Organizing Files and Directories - Apply your knowledge of
mv(along withcpandrm) 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.
- 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.
Lesson complete
You finished mv (Move). Nice work — keep the momentum going.