The mv
command in Linux is used to move or rename files and directories. It stands for “move,” but it can also be used to rename files and directories within the same location. The mv
command is a powerful tool that allows you to manipulate files and directories efficiently.
Here’s a detailed explanation of how to use the mv
command:
Syntax:
mv [options] source destination
Options:
-i
: Interactive mode. The command will prompt you before overwriting files.-u
: Update mode. Move only when the source file is newer than the destination file or when the destination file is missing.-v
: Verbose mode. Display detailed information about the files being moved.-b
: Backup mode. Create a backup of the destination file before overwriting it.-f
: Force mode. Overwrite the destination file if it already exists without prompting.
Examples:
Moving a File:
To move a file from one location to another, you provide the source file path and the destination directory path:
mv /path/to/source/file.txt /path/to/destination/
This will move the file.txt
from the source location to the destination directory.
Renaming a File:
To rename a file, you provide the source file path and the destination path with the new filename:
mv /path/to/oldfile.txt /path/to/newfile.txt
This will rename oldfile.txt
to newfile.txt
in the same directory.
Moving and Renaming:
You can also move a file to a new location while renaming it:
mv /path/to/source/file.txt /path/to/destination/newfile.txt
This will move file.txt
to the destination
directory and rename it to newfile.txt
.
Moving Directories:
The mv
command can also be used to move directories. Just like with files, you provide the source directory path and the destination directory path:
mv /path/to/source/directory /path/to/destination/
This will move the entire directory
to the destination.
Interactive Mode:
If you want to be prompted before overwriting a file, you can use the -i
option:
mv -i /path/to/source/file.txt /path/to/destination/
This will ask for confirmation before overwriting any existing files in the destination directory.
Verbose Mode:
To see detailed information about the files being moved, you can use the -v
option:
mv -v /path/to/source/file.txt /path/to/destination/
This will display each file as it’s moved.
These are just a few examples of how the mv
command can be used. It’s a versatile tool that can help you manage your files and directories efficiently in the Linux command line. Always be cautious when using the mv
command, especially with the -f
(force) option, as it can lead to data loss if not used carefully.
Additional Examples:
Move Multiple Files:
You can move multiple files at once by providing multiple source file paths and a destination directory:
mv file1.txt file2.txt /path/to/destination/
This will move both file1.txt
and file2.txt
to the destination directory.
Move and Merge Directories:
You can move the contents of one directory into another, effectively merging them:
mv /path/to/source/* /path/to/destination/
This will move all files and subdirectories from source
to the destination
directory.
Move with Wildcards:
You can use wildcards to move multiple files that match a specific pattern:
mv *.txt /path/to/destination/
This will move all files with the .txt
extension to the destination directory.
Move Hidden Files:
Hidden files (those starting with a dot) can be moved using the mv
command as well:
mv ~/.config /path/to/backup/
This moves the .config
directory from the home directory to a backup
directory.
Moving with Renaming and Subdirectories:
You can create new subdirectories while moving files:
mv file1.txt file2.txt /path/to/destination/new_subdir/
This will move file1.txt
and file2.txt
to a new subdirectory new_subdir
inside the destination directory.
Move and Overwrite:
You can use the -f
option to forcefully move and overwrite existing files without prompting:
mv -f /path/to/source/file.txt /path/to/destination/
This will overwrite file.txt
in the destination directory if it already exists.
Move Files with Specific Extension:
You can use find
and xargs
to move files with a specific extension to a new directory:
find /path/to/source -type f -name "*.jpg" | xargs -I {} mv {} /path/to/destination/
This example finds all .jpg
files in the source directory and moves them to the destination directory.
Move and Preserve Attributes:
To move files while preserving their attributes, such as permissions and timestamps, use the -p
option:
mv -p /path/to/source/file.txt /path/to/destination/
This will move file.txt
to the destination directory while preserving its attributes.
Move with Progress Bar:
You can use the rsync
command to move files with a progress bar:
rsync -ah --progress /path/to/source/ /path/to/destination/
This will move the contents of the source directory to the destination while displaying a progress bar.
These examples showcase various ways to use the mv
command in different scenarios. Remember to exercise caution when using the mv
command, especially when overwriting or deleting files, to prevent unintended data loss.