The mv command in Linux is one of the simple but powerful tools used to either move or rename files and directories. Whether you are organizing documents, transferring files, or renaming documents, mv is a crucial part of the Linux toolkit. Unlike copying, mv does not leave a duplicate, it physically relocates the file.
In this guide, we will break down the mv command in Linux, its syntax, best use cases, and practical examples.
Basic Syntax of mv
The basic syntax of the mv command in Linux is straightforward:
mv [options] source destination
- source: The file or directory you want to move or rename
- destination: The new location or new name for the file or directory
The mv command in Linux works whether you are moving the file to a new destination or just renaming it. You can also use wildcards to move multiple files together.
How to Move Files Using mv
To move a file from one directory to another destination, use the command:
Get exclusive access to all things tech-savvy, and be the first to receive
the latest updates directly in your inbox.
mv file.txt /path/to/destination/
Example:
mv report.txt ~/Documents/reports/
This command moves report.txt from the current directory to the ~/Documents/reports/ directory. The file will no longer exist in the original location unless you copy it instead.
To move more than one file, use the command:
mv file1.txt file2.txt /path/to/destination/
This command moves both file1.txt and file2.txt to the specified folder.
How to Rename Files and Directories
The mv command in Linux can also be used to rename the files or folders. If the destination is not a directory, then Linux will treat it as its new name.
mv oldname.txt newname.txt
Example:

mv draft.docx final_report.docx
This command renames the file from draft.docx to final_report.docx.
You can also rename directories:
mv old_folder new_folder
This is helpful for organizing the files and cleaning up naming conventions.
Useful mv Command Options
Here are some handy options you can use with the mv command:
Option | Description |
-i | Prompts before overwriting an existing file |
-f | Forces the move without prompting (even if it overwrites) |
-n | Prevents overwriting an existing file |
-v | Displays a message for each file moved (verbose mode) |
Example with options:
mv -iv file.txt backup_folder/
This command moves file.txt to backup_folder/, and it will prompt before overwriting and show what it did.
Related Article: Fixing ‘vim command not found’ Error in Linux & macOS: Quick Solutions
Examples of mv Command in Linux
To understand the mv command in Linux better, here are a few examples that will help you understand real-world situations.
- Move a file to another directory
mv notes.txt ~/Documents/
- Move multiple files at once
mv *.jpg ~/Pictures/
- Rename a file
mv resume_draft.pdf resume_final.pdf
- Rename a directory
mv old_project new_project
- Move and overwrite without prompt
mv -f data.csv backup/data.csv
- Move with confirmation (interactive mode)
mv -i report.doc report_backup.doc
- Verbose output to see what’s happening
mv -v *.log ~/logs/
These examples cover the most common scenarios you’ll face when organizing files via the terminal.
Common Errors and How to Avoid Them
Error Message | Cause | How to Fix |
mv: cannot stat ‘file’: No such file or directory | The source file doesn’t exist or the name is incorrect | Check for typos and verify the file path |
mv: cannot move ‘file’ to ‘directory’: Permission denied | You don’t have permission to move files to the destination | Use sudo if appropriate or adjust file permissions |
mv: target ‘folder/’ is not a directory | The destination is expected to be a folder but isn’t | Check the destination or ensure it ends with a / if it’s a folder |
Overwriting files without warning | Default behavior without -i or -n | Use -i for prompts or -n to skip overwriting |
Understanding these common errors will allow you to avoid sticky situations, such as file loss or confusion during file transfers.
Safety Tips When Using mv
- Use -i when unsure
If you are working with crucial files, add -i prompt before overwriting anything.
- Avoid using wildcards (*) blindly
Using wildcards will allow you to match more files than intended. Use echo *.txt first to preview the result.
- Double-check your paths
Always double check the paths, since typos can move files to unexpected or wrong locations.
- Use versioned filenames
Instead of overwriting, rename the versions like report_v1.txt, report_v2.txt to keep backups.
- Use –no-clobber or -n for extra caution
Using –no-clobber or -n will stop the files from being overwritten.
- Backup before bulk moves
Always create backups before bulk moves or copy them to a backup directory to avoid accidental loses.
Conclusion
The mv command in Linux is a powerful and multi-use tool that helps you move, rename, and shift files with ease. No matter what the task at hand is, from organizing files to automating tasks, understanding how the mv works gives you much more control over your systems.
FAQs
Does mv
overwrite files by default?
Yes, if a file with the same name exists in the destination, mv
will overwrite it without warning unless you use the -i
(interactive) or -n
(no-clobber) options.
How can I see what files are being moved?
Use the -v
option for verbose output. Example: mv -v *.txt /backup/
will display each move action in the terminal.
What does the “Permission denied” error mean when using mv
?
This error usually means you don’t have the rights to move files to the destination directory. You may need to use sudo
or change file permissions.