The mv
command is used to rename files or directories in Linux. It can also be used to move files or directories from one location to another. This command is essential for managing files and directories in a Linux system.
Overview
The syntax for using the mv
command is as follows:
mv [OPTION]... [-T] source destination
mv [OPTION]... source... directory
mv [OPTION]... -t directory source...
Here, source
refers to the file or directory that needs to be renamed or moved, while destination
is the new name for the file or directory. If you want to move the file or directory to a new location, destination
should be the path to the new location.
Examples
- Renaming a file:
mv old_file_name.txt new_file_name.txt
This command will rename old_file_name.txt
to new_file_name.txt
.
- Moving a file to a new directory:
mv file.txt /home/user/new_directory/
This command will move file.txt
to the new_directory
directory located in the home directory of the user
.
- Renaming a directory:
mv old_directory_name/ new_directory_name/
This command will rename old_directory_name
to new_directory_name
.
- Moving a directory to a new location:
mv directory/ /home/user/new_location/
This command will move the directory
to the new_location
directory located in the home directory of the user
.
Options
Here is a table of available options for the mv
command:
Option | Description |
---|---|
-f | Force move, even if the destination file already exists |
-i | Prompt before overwriting a destination file |
-v | Verbose mode, print the name of each file as it is moved |
-n | Do not overwrite an existing file |
-u | Move only when the source file is newer than the destination file |
-T | Treat the destination as a normal file, even if it is a directory |
–help | Display help information for the command |
Troubleshooting Tips
- If you receive a “Permission denied” error when trying to move a file, make sure you have the necessary permissions to write to the destination directory.
- If you receive a “No such file or directory” error, double-check that you have entered the correct file or directory name and path.
Notes
- Be careful when using the
mv
command, as it can overwrite existing files without warning. - It is always a good idea to make a backup of important files before renaming or moving them.