Skip to content

mv Linux Command Guide

The Linux mv command is used to move or rename files and directories. It allows users to transfer files between directories or rename files within the same directory. By specifying the source and destination paths, users can easily manipulate their files. The mv command also supports various options, such as -i to prompt before overwriting, -u to move only when the source is newer than the destination, and -v for verbose mode. Mastering the mv command can help users efficiently organize and manage their files in the Linux operating system.

mv Syntax:

Terminal window
mv [option] [source] [destination]

Options:

OptionDescription
-fDo not prompt before overwriting
-iPrompt before overwriting
-uMove only when the source file is newer or the destination file is missing
-vDisplay informative messages
-bCreate a backup of the destination file before overwriting

Parameters:

ParameterDescription
sourceThe file or directory to be moved
destinationThe destination file or directory where the source is moved

mv Command Samples:

Move a File to a Directory

Terminal window
mv file.txt /directory/

Moves the file “file.txt” to the directory specified.

Rename a File

Terminal window
mv oldfile.txt newfile.txt

Renames the file “oldfile.txt” to “newfile.txt”.

Move Multiple Files to a Directory

Terminal window
mv file1.txt file2.txt /directory/

Moves multiple files (“file1.txt” and “file2.txt”) to the directory specified.

Move a Directory to Another Location

Terminal window
mv directory1/ /path/to/new/location/

Moves the directory “directory1” to a new location specified.

Move a File with a Different Name

Terminal window
mv file1.txt /directory/file2.txt

Moves the file “file1.txt” to the directory “/directory” with a new name “file2.txt”.

Move and Rename a Directory

Terminal window
mv olddirectory/ newdirectory/

Moves and renames the directory “olddirectory” to “newdirectory”.

Move a Directory and Its Contents

Terminal window
mv -r olddirectory/ /path/to/new/location/

Moves the directory “olddirectory” and all its contents to a new location specified.

mv FAQ:

How do I use mv in Linux?

To use the mv command in Linux, execute the following command:

Terminal window
mv --option <value>

What is the purpose of the mv command in Linux?

The mv command in Linux is used to move files or directories from one location to another.

How can I rename a file using mv in Linux?

To rename a file using the mv command in Linux, specify the current filename as the source and the new filename as the destination, like this:

Terminal window
mv oldfile.txt newfile.txt

How do I move a directory and its contents to a new location in Linux?

To move a directory and its contents to a new location in Linux, use the mv command with the -r (or -R) option to recursively move all files and subdirectories, like this:

Terminal window
mv -r sourcedir/ destination/

Can the mv command be used to overwrite existing files in Linux?

Yes, the mv command in Linux can be used to overwrite existing files. When moving a file to a location where a file with the same name already exists, the existing file will be replaced by the new one.

How can I move multiple files at once using mv in Linux?

To move multiple files at once using the mv command in Linux, specify the names of the files you want to move followed by the destination directory, like this:

Terminal window
mv file1 file2 file3 directory/

Applications of the mv command

  • Renaming files
  • Moving files to a different directory
  • Overwriting existing files
  • Batch renaming or moving multiple files
  • Changing the location and name of a file
  • Renaming and moving directories