📦 mv Command

Move and Rename Files and Directories in Linux

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.

Basic Syntax:
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.

$ ls -l
-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
What's happening: The file 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.
When renaming files, the mv command preserves all file attributes including modification times, which is often desired behavior for maintaining file history.

2Move File to Different Directory

Moving a file from the current directory to another location.

$ pwd
/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
What's happening: The file 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.
Always verify the destination directory exists before moving files. If you accidentally specify a non-existent directory without the trailing slash, mv will try to rename the file instead, potentially causing confusion.

3Move and Rename Simultaneously

Combining move and rename operations in a single command.

$ ls -l /tmp/
-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
What's happening: The file 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.

$ ls -l
-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
What's happening: The -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.
The -i flag is crucial when working with important files. Many system administrators alias 'mv' to 'mv -i' in their shell configuration to make interactive mode the default behavior, preventing accidental data loss.

5Verbose Mode for Detailed Feedback

Using -v to see exactly what mv is doing, especially useful in scripts.

$ mv -v *.log /var/log/archive/
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'
What's happening: The -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.
Combining -v with -i gives you both visibility and safety: mv -iv shows what's happening and prompts before any overwrites.

6Move Multiple Files to Directory

Moving several files at once to a destination directory.

$ ls -l
-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
What's happening: When mv receives multiple source arguments, it interprets the last argument as the destination directory. All the preceding files (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.

$ ls -lh
-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
What's happening: The -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.
Note that -n operates silently. If you need to know whether files were actually moved or skipped, combine it with -v: mv -nv will report "not replacing" for protected files.

8Backup Existing Files Before Overwriting

Creating automatic backups with the -b flag.

$ ls -l
-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~
What's happening: The -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.
You can customize the backup suffix using --suffix=SUFFIX. For example: mv -b --suffix=.bak would create config.conf.bak instead of config.conf~

9Update Mode - Move Only Newer Files

Using -u to move files only if they're newer than existing destinations.

$ ls -l source/
-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'
What's happening: The -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.

$ ls -ld project_old/
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/
What's happening: The directory 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.
When renaming directories, be careful with trailing slashes. Both "mv dir1/ dir2" and "mv dir1 dir2" work, but the behavior can differ if dir2 already exists: without the slash, dir1 might be moved INTO dir2, creating dir2/dir1.

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 -i when 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 -b or 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. *.txt is 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

$ mkdir -p documents/{pdf,txt,doc}
$ mv *.pdf documents/pdf/
$ mv *.txt documents/txt/
$ mv *.doc documents/doc/
Create a directory structure and organize files by type using wildcards and multiple mv commands.

Batch Rename with a Loop

$ for file in *.jpg; do
mv "$file" "vacation_${file}"
done
Add a prefix to all JPEG files in the current directory. The quotes around $file handle filenames with spaces.

Moving Files Modified in Last 7 Days

$ find /source -type f -mtime -7 -exec mv {} /destination/ \;
Combine find and mv to move files based on modification time. The -exec flag runs mv for each file found.

Safe Configuration File Update

$ sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
$ sudo mv -b /home/user/nginx.conf.new /etc/nginx/nginx.conf
$ sudo nginx -t && sudo systemctl reload nginx
Create a manual backup, move the new config with automatic backup, then test before reloading. This is a production-safe approach.

Cross-Filesystem Moves

Important behavior difference: When you move files within the same filesystem (e.g., from /home/user/docs to /home/user/archive), mv simply updates directory entries. This is instantaneous regardless of file size. However, when moving across different filesystems (e.g., from /home to /mnt/external), mv must copy the entire file to the new location and then delete the original. This takes time proportional to the file size and requires sufficient space on both filesystems during the operation.
$ df -h /home /mnt/backup
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
For large files moving across filesystems, consider using rsync instead of mv. It provides progress indication, can be resumed if interrupted, and offers more control over the transfer process.

Troubleshooting Common Issues

Error: "Device or resource busy"
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
Error: "Permission denied"
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
Error: "Not a directory"
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
Accidentally overwrote a file
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