About the mv Command
The mv (move) command is one of the fundamental file manipulation utilities in Linux and Unix systems. It serves dual purposes: moving files and directories from one location to another, and renaming them. Unlike the cp command which creates copies, mv actually relocates the file or directory, making it an efficient operation that doesn't require duplicating data on the same filesystem.
Key characteristics: The mv command modifies directory entries rather than physically copying data when moving within the same filesystem. This makes it extremely fast regardless of file size. When moving across different filesystems, mv performs a copy and delete operation automatically.
mv [OPTIONS] SOURCE DESTINATION
mv [OPTIONS] SOURCE... DIRECTORY
Common Options:
-i, --interactive Prompt before overwrite
-f, --force Force overwrite without prompting
-n, --no-clobber Do not overwrite existing files
-v, --verbose Explain what is being done
-u, --update Move only when SOURCE is newer
-b, --backup Make backup of destination file
-t, --target-directory=DIR Move all SOURCE arguments into DIR
Detailed Examples
1Simple File Rename
The most basic use of mv is renaming a file in the current directory.
-rw-r--r-- 1 user user 1024 Nov 28 09:00 oldname.txt
$ mv oldname.txt newname.txt
$ ls -l
-rw-r--r-- 1 user user 1024 Nov 28 09:00 newname.txt
oldname.txt is renamed to newname.txt in the same directory. The file's contents, permissions, timestamps, and ownership remain unchanged. Only the filename (directory entry) is modified. This operation is instantaneous regardless of file size because no data is actually moved or copied.
2Move File to Different Directory
Moving a file from the current directory to another location.
/home/user/documents
$ ls -l report.pdf
-rw-r--r-- 1 user user 524288 Nov 28 10:15 report.pdf
$ mv report.pdf /home/user/archive/
$ ls /home/user/archive/
report.pdf
report.pdf is moved from /home/user/documents to /home/user/archive/. The trailing slash on the destination indicates it's a directory. If the destination directory doesn't exist, mv will return an error. The file maintains its original name unless you specify a different name in the destination path.
3Move and Rename Simultaneously
Combining move and rename operations in a single command.
-rw-r--r-- 1 user user 2048 Nov 28 11:30 temp_data.csv
$ mv /tmp/temp_data.csv /home/user/projects/final_report.csv
$ ls -l /home/user/projects/
-rw-r--r-- 1 user user 2048 Nov 28 11:30 final_report.csv
temp_data.csv is moved from /tmp/ to /home/user/projects/ and renamed to final_report.csv in a single operation. This is more efficient than using two separate commands (mv to move, then mv to rename). The file retains all its original attributes including the modification timestamp of 11:30.
4Interactive Mode - Safe Overwriting
Using the -i flag to prompt before overwriting existing files.
-rw-r--r-- 1 user user 1024 Nov 28 09:00 important.txt
-rw-r--r-- 1 user user 512 Nov 28 12:00 draft.txt
$ mv -i draft.txt important.txt
mv: overwrite 'important.txt'? y
$ ls -l
-rw-r--r-- 1 user user 512 Nov 28 12:00 important.txt
-i (interactive) flag causes mv to prompt for confirmation before overwriting the existing important.txt file. Responding with 'y' confirms the overwrite. The original important.txt is permanently deleted and replaced by the contents of draft.txt. Note that the file size changed from 1024 to 512 bytes, and the timestamp updated to 12:00.
5Verbose Mode for Detailed Feedback
Using -v to see exactly what mv is doing, especially useful in scripts.
renamed 'system.log' -> '/var/log/archive/system.log'
renamed 'application.log' -> '/var/log/archive/application.log'
renamed 'error.log' -> '/var/log/archive/error.log'
renamed 'access.log' -> '/var/log/archive/access.log'
-v (verbose) flag makes mv report each file it moves. This is particularly valuable when moving multiple files with wildcards, as it provides confirmation of what was moved. The output shows the full path transformation for each file. This is essential for scripts where you need to log operations or debug file movement issues.
6Move Multiple Files to Directory
Moving several files at once to a destination directory.
-rw-r--r-- 1 user user 1024 Nov 28 13:00 file1.txt
-rw-r--r-- 1 user user 2048 Nov 28 13:05 file2.txt
-rw-r--r-- 1 user user 3072 Nov 28 13:10 file3.txt
drwxr-xr-x 2 user user 4096 Nov 28 13:15 backup/
$ mv file1.txt file2.txt file3.txt backup/
$ ls backup/
file1.txt file2.txt file3.txt
file1.txt, file2.txt, and file3.txt) are moved into the backup/ directory. The destination must be a directory when moving multiple files; if it's not, mv will produce an error. This is an efficient way to organize files without multiple commands.
7No-Clobber Mode - Prevent Overwriting
Using -n to protect existing files from being overwritten.
-rw-r--r-- 1 user user 5.0M Nov 28 14:00 database.sql
-rw-r--r-- 1 user user 100K Nov 28 14:30 database.sql.backup
$ mv -n database.sql.backup database.sql
$ ls -lh database.sql
-rw-r--r-- 1 user user 5.0M Nov 28 14:00 database.sql
$ echo $?
0
-n (no-clobber) flag prevents mv from overwriting the existing database.sql file. Unlike -i which prompts, -n silently refuses to overwrite. The original 5.0M file remains untouched. The command returns exit status 0 (success) even though no move occurred. This is useful in scripts where you want to preserve existing files without user interaction.
8Backup Existing Files Before Overwriting
Creating automatic backups with the -b flag.
-rw-r--r-- 1 user user 1024 Nov 28 15:00 config.conf
-rw-r--r-- 1 user user 2048 Nov 28 15:30 config.conf.new
$ mv -b config.conf.new config.conf
$ ls -l
-rw-r--r-- 1 user user 2048 Nov 28 15:30 config.conf
-rw-r--r-- 1 user user 1024 Nov 28 15:00 config.conf~
-b (backup) flag tells mv to create a backup of any file it would overwrite. By default, the backup is created with a tilde (~) suffix. The original config.conf (1024 bytes) becomes config.conf~, while config.conf.new becomes the new config.conf (2048 bytes). This provides a safety net for configuration file updates and allows quick rollback if needed.
9Update Mode - Move Only Newer Files
Using -u to move files only if they're newer than existing destinations.
-rw-r--r-- 1 user user 1024 Nov 28 16:00 data.txt
-rw-r--r-- 1 user user 2048 Nov 28 16:30 report.txt
$ ls -l dest/
-rw-r--r-- 1 user user 1024 Nov 28 16:45 data.txt
-rw-r--r-- 1 user user 2048 Nov 28 16:15 report.txt
$ mv -uv source/* dest/
renamed 'source/report.txt' -> 'dest/report.txt'
-u (update) flag makes mv compare timestamps. Only files that are newer than their destination counterparts are moved. In this example, source/data.txt (16:00) is older than dest/data.txt (16:45), so it's skipped. However, source/report.txt (16:30) is newer than dest/report.txt (16:15), so it's moved. The -v flag confirms only one file was actually moved. This is extremely useful for synchronization tasks where you want to update destinations with only the latest versions.
10Rename Directory
Renaming directories and their entire contents.
drwxr-xr-x 5 user user 4096 Nov 28 17:00 project_old/
$ du -sh project_old/
125M project_old/
$ mv project_old/ project_production
$ ls -ld project_production/
drwxr-xr-x 5 user user 4096 Nov 28 17:00 project_production/
project_old containing 125MB of files is renamed to project_production. Despite the large size, this operation is instantaneous because mv only modifies the directory entry, not the actual data. All subdirectories, files, permissions, and timestamps within the directory remain exactly as they were. The directory's own metadata (permissions drwxr-xr-x and timestamp 17:00) is also preserved.
Complete Options Reference
| Option | Long Form | Description |
|---|---|---|
-b |
--backup |
Make a backup of each existing destination file |
-f |
--force |
Do not prompt before overwriting (overrides -i) |
-i |
--interactive |
Prompt before overwrite |
-n |
--no-clobber |
Do not overwrite an existing file (silently) |
-u |
--update |
Move only when SOURCE is newer than destination or destination is missing |
-v |
--verbose |
Explain what is being done |
-t DIR |
--target-directory=DIR |
Move all SOURCE arguments into DIR |
-T |
--no-target-directory |
Treat DEST as a normal file, not a directory |
--strip-trailing-slashes |
Remove any trailing slashes from SOURCE arguments | |
--suffix=SUFFIX |
Override the usual backup suffix (default: ~) |
Best Practices and Safety Tips
Essential Safety Guidelines
- Use -i for interactive mode: Always use
mv -iwhen moving files that might overwrite existing data. Many admins alias this as default. - Verify destinations exist: Before moving files, confirm the destination directory exists using
ls -d /path/to/dest. - Test with echo first: For complex wildcards, test with
echo mv *.txt dest/to see what will be moved before executing. - Use absolute paths for scripts: In shell scripts, use full paths to avoid confusion:
mv /home/user/file.txt /backup/ - Backup critical files: Use
mv -bor create manual backups before moving important configuration files. - Check filesystem space: When moving across filesystems (which triggers copy+delete), ensure sufficient space exists at the destination.
- Understand wildcards thoroughly: Test glob patterns carefully.
*.txtis very different from*txt(no dot). - Watch for special characters: Files with spaces, quotes, or special characters need proper quoting:
mv "my file.txt" dest/ - Preserve metadata intentionally: mv preserves timestamps and permissions. If you need to update these, use touch or chmod after moving.
- Use -v in scripts: The verbose flag helps with logging and debugging in automated processes.
Common Use Cases and Patterns
Organizing Files by Extension
$ mv *.pdf documents/pdf/
$ mv *.txt documents/txt/
$ mv *.doc documents/doc/
Batch Rename with a Loop
mv "$file" "vacation_${file}"
done
Moving Files Modified in Last 7 Days
Safe Configuration File Update
$ sudo mv -b /home/user/nginx.conf.new /etc/nginx/nginx.conf
$ sudo nginx -t && sudo systemctl reload nginx
Cross-Filesystem Moves
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 100G 45G 50G 48% /home
/dev/sdb1 500G 120G 350G 26% /mnt/backup
$ time mv large_file.iso /mnt/backup/
real 0m45.230s # Takes time because of copy operation
Troubleshooting Common Issues
This occurs when trying to move a file that's currently open by a process. Solution: Close the application using the file or use lsof to identify and stop the process:
lsof filename
You lack write permission on the source directory, destination directory, or the file itself. Solutions: Use sudo if appropriate, check permissions with
ls -l, or verify directory ownership with ls -ld
You're trying to move multiple files to what you think is a directory, but it's actually a regular file. Solution: Verify destination with
ls -ld destination and create the directory if needed: mkdir -p /path/to/destination
If you didn't use -b (backup) and overwrote an important file, recovery is difficult. Immediately stop using the system and consider data recovery tools like extundelete (ext filesystems) or photorec. Prevention is key: alias mv='mv -i' in your shell profile.
Related Commands
- cp - Copy files and directories (creates duplicates rather than moving)
- rm - Remove files or directories
- rename - Perl-based tool for complex batch renaming with regular expressions
- rsync - Remote and local file synchronization with progress and resume capability
- ln - Create links between files (hard links or symbolic links)
- install - Copy files and set attributes, often used in software installation