mv – Move or Rename Files and Directories
The mv command moves files or directories from one location to another. It can also be used to rename files or directories.
Syntax
mv [OPTION]... SOURCE... DESTINATION
SOURCEcan be one or multiple files or directories.DESTINATIONis either a directory or the new name for a single file.
Options
| Option | Description |
|---|---|
-i |
Interactive mode: prompt before overwrite |
-f |
Force move by overwriting destination without prompt |
-n |
No clobber: do not overwrite an existing file |
-v |
Verbose: show details of the move operations |
--backup |
Make a backup of each existing destination file before overwriting |
Practical Examples
1. Move a file to another directory
mv file.txt /path/to/destination/
Moves file.txt into the /path/to/destination/ directory.
2. Rename a file
mv oldname.txt newname.txt
Renames oldname.txt to newname.txt in the current directory.
3. Move multiple files into a directory
mv file1.txt file2.txt /path/to/destination/
Moves file1.txt and file2.txt into the specified directory.
4. Prompt before overwriting files
mv -i file.txt existingfile.txt
If existingfile.txt exists, you will be prompted before overwriting it.
5. Force overwrite without prompting
mv -f file.txt existingfile.txt
Overwrites existingfile.txt without any warning.
6. Verbose output to see what is being moved
mv -v file.txt /path/to/destination/
Output:
‘file.txt’ -> ‘/path/to/destination/file.txt’
7. Move and backup existing destination files
mv --backup file.txt existingfile.txt
Creates a backup of existingfile.txt before overwriting it.
Tips
- To avoid accidentally overwriting files, consider always using
-i. - To rename directories, simply specify the directory as source and new name as destination.
- Combine with shell wildcards to move multiple files at once, e.g.:
mv *.txt /backup/textfiles/
See Also
cp– copy files and directoriesrm– remove files and directoriesrename– batch rename files (different utility, varies by system)
Summary
mv is an essential command for moving and renaming files or directories. Use options like -i for safety and -v for clarity during operations.