Introduction
The mv
command is a UNIX utility for renaming and relocating files and directories in a filesystem. While desktop operating systems also offer a GUI method for file manipulation, there are cases in which using mv
in a terminal can be a more efficient solution.
This article shows you how to use the mv
command in Linux, provides a list of options, and offers illustrative examples.
Prerequisites
- Command-line access.
- An account with sudo privileges for moving root-owned files.
mv Command Syntax
The mv
command works with files and directories, yielding different results depending on the used arguments. There are four basic ways to execute an mv
command:
- Rename a file. Type the source filename and the destination filename:
mv [options] [source-file] [destination-file]
- Rename (or move) a directory. Type the source directory and the destination directory. If a destination directory exists,
mv
moves the source directory to the specified destination.
mv [options] [source-directory] [destination-directory]
- Move a file, preserving the filename. Type the filename followed by the destination directory.
mv [options] [file] [destination-directory]
- Move a file and change its name. Type the filename followed by the destination directory and a new filename.
mv [options] [file1] [directory/file2]
mv Command Options
According to the POSIX standard, the mv
command has only two options: -f
(force remove) and -i
(interactive mode). However, depending on the OS and the version of the mv
command, more useful options are available as arguments.
Below is a list of the most common mv
options.
Option | Description |
---|---|
-b , --backup |
Create a backup of files that will be overwritten or removed. |
-f , --force |
Overwrite destination files without prompting the user. |
-i , --interactive |
Prompt the user to confirm if the mv action should overwrite a file. |
-S , --suffix= |
Provide a suffix for a backup file. The default suffix is ~ . |
-u , --update |
Perform the mv action only if the source file is newer or the destination file does not exist. |
-v , --verbose |
Show an output describing the action taken. |
--help |
Output the command help and exit. |
--version |
Show the command version and exit. |
mv Command Examples
As seen above, the mv
command is a simple but versatile file management utility. The sections below provide examples of the most common mv
operations.
Rename File
Rename the file name1 to name2 with the following command:
mv name1 name2
The command produces no output, but the ls command shows the operation was successful.
View Command Output
Rename the file name1 to name2 and print the output:
mv -v name1 name2
The command produces an output containing the list of the performed actions.
Move Multiple Files
Move the files name1, name2, and name3 to dir2 in the home directory and show the verbose output:
mv -v name1 name2 name3 test-dir
The output shows that the files have been moved successfully.
Ask Before Overwriting
Rename name1 to name2, but show a prompt to overwrite if the file name2 already exists.
mv -v -i name1 name2
In the example below, the file name2 was already in the directory. The mv
command prompted to overwrite the file and replaced its contents with the contents of name1.
Note: If you do not provide the -i
option, mv
assumes you want to overwrite the destination file.
Create File Backup
Move the file name1 to the directory test-dir. If the file of the same name exists at the destination, create a backup with the extension .bak:
mv -S .bak -b name1 test-dir/name1
After the command executes, the directory contains two files:
- name1 is the moved file.
- name1.bak is a backup of the file initially present in the directory.
Batch Rename Files
Combine mv
with the Bash for loop to rename multiple files in one command. For example, to add the prefix test_ to all the files in a directory, type:
for file in *; do mv "$file" "test_$file"; done
Listing the directory contents shows that all the filenames start with test_:
Conclusion
After reading this article, you should know how to use the mv
command to move and rename files and directories in Linux. The article included the most common command options and provided examples of the frequently performed operations.
To learn more about Linux CLI fundamentals, read our ultimate list of Linux Commands All Users Should Know.
Đăng ký liền tay Nhận Ngay Bài Mới
Subscribe ngay
Cám ơn bạn đã đăng ký !
Lỗi đăng ký !
Add Comment