The mv command, short for "move," is a fundamental utility in any Linux environment. It serves two primary purposes: renaming files or directories and moving them to a different location. Its functionality is similar in many ways to the cp command.
Renaming Files and Directories
One of the most common uses of the mv command in linux is for renaming. The syntax is straightforward: you specify the old name and the new name.
To rename a file:
mv oldfile newfile
This same logic applies to renaming directories:
mv old_directory_name new_directory_name
Moving Files and Directories
The other core function of the mv command is to move items from one location to another.
To move a single file into a different directory:
mv file2 /home/pete/Documents
You can also move multiple files at once. Simply list all the source files followed by the target directory:
mv file_1 file_2 /somedirectory
A useful option for this is linux mv -t, which allows you to specify the target directory first. This can be clearer when moving many files.
mv -t /somedirectory file_1 file_2
Unlike the cp command, you do not need a -r flag to move a directory. The bash mv command handles directories by default. While some users search for mv -r linux, this option is not necessary for moving directories with mv.
Important Options for the mv Command
By default, if you move a file to a destination where a file with the same name already exists, mv will overwrite it without warning. To prevent accidental data loss, you can use the following options:
-
-i (interactive): This is a crucial safety feature. It will prompt you for confirmation before overwriting any existing file.
mv -i source_file destination_directory -
-b (backup): If you intend to overwrite a file but want to keep the old version, this option creates a backup of the destination file. The backup is typically renamed with a tilde (
~) suffix.mv -b file1 directory_with_file1 -
-v (verbose): This option makes the
mvcommand print out what it is doing, showing each file being moved or renamed.mv -v file1 file2 /somedirectory
Mastering the mv command is essential for efficient file management on the command line.