mv
- Move/Rename Files
1. Introduction
The mv
command is a fundamental utility in Linux, used for moving or renaming files and directories. Unlike the cp
command, which creates copies, mv
relocates or renames the original item, making it efficient for file management within the same filesystem. Understanding its behavior, particularly with overwrites and cross-filesystem moves, is essential to avoid data loss and ensure effective operations.
mv
stands for move. This guide covers its syntax, common use cases, key options, and critical considerations for safe usage.
2. Basic Syntax
The mv
command supports several syntax patterns:
2.1 Rename SOURCE to DEST, or move SOURCE into directory DEST:
$ mv [OPTION]... [-T] SOURCE DEST
- If
DEST
does not exist,SOURCE
is renamed toDEST
. - If
DEST
is an existing file, it is overwritten bySOURCE
(unless options like-i
or-n
are used). - If
DEST
is an existing directory,SOURCE
is moved intoDEST
.
2.2 Move multiple SOURCEs into a DIRECTORY:
$ mv [OPTION]... SOURCE... DIRECTORY
- All
SOURCE
files/directories are moved into the existingDIRECTORY
, which must exist.
2.3 Alternative syntax for moving multiple SOURCEs into a DIRECTORY:
$ mv [OPTION]... -t DIRECTORY SOURCE...
Equivalent to the second form, specifying the target directory first with
-t
.[OPTION]...
: Flags modifying behavior (e.g.,-i
for interactive,-u
for update).SOURCE
: The file or directory to move/rename.DEST
: The new name or destination path.SOURCE...
: Multiple source files/directories.DIRECTORY
: The target directory for moves.
3. Core Use Cases with Examples
To demonstrate mv
’s functionality, let’s set up a test environment:
# Create a new directory named 'mv_test_dir' for testing the mv command
$ mkdir mv_test_dir
# Change the current working directory to 'mv_test_dir'
$ cd mv_test_dir
# Create a file named 'report_draft.txt' with the content "Report content."
$ echo "Report content." > report_draft.txt
# Create a file named 'data.csv' with the content "Data for analysis."
$ echo "Data for analysis." > data.csv
# Create an empty file named 'temp_file.tmp'
$ touch temp_file.tmp
# Create a new directory named 'archive'
$ mkdir archive
# Create a new directory named 'documents'
$ mkdir documents
# List the contents of the current directory in long format with a trailing slash for directories
$ ls -lp
total 12
drwxr-xr-x 2 user user 4096 May 3 18:00 archive/
-rw-r--r-- 1 user user 19 May 3 18:00 data.csv
drwxr-xr-x 2 user user 4096 May 3 18:00 documents/
-rw-r--r-- 1 user user 16 May 3 18:00 report_draft.txt
-rw-r--r-- 1 user user 0 May 3 18:00 temp_file.tmp
Note on Example Output: In the following ls examples, directory names might be shown with a trailing slash (/) for clarity, even if the specific ls command used in the example doesn’t include -F or -p. To reliably see this indicator in your terminal, use ls -F or ls -p
3.1 Renaming a File
To rename report_draft.txt
to report_final.txt
:
# Rename the file 'report_draft.txt' to 'report_final.txt'
$ mv report_draft.txt report_final.txt
# List the contents of the current directory to verify the rename
$ ls
archive/ data.csv documents/ report_final.txt temp_file.tmp
Note: The original file (report_draft.txt
) is replaced by report_final.txt
.
3.2 Moving a File into a Directory
To move report_final.txt
into the documents
directory:
# Move the file 'report_final.txt' into the 'documents' directory
$ mv report_final.txt documents/
# List the contents of the current directory to confirm the file is no longer here
$ ls
archive/ data.csv documents/ temp_file.tmp
# List the contents of the 'documents' directory to verify the file was moved
$ ls documents/
report_final.txt
3.3 Moving Multiple Files into a Directory
To move data.csv
and temp_file.tmp
into the archive
directory:
# Move the files 'data.csv' and 'temp_file.tmp' into the 'archive' directory
$ mv data.csv temp_file.tmp archive/
# List the contents of the current directory to confirm the files are gone
$ ls
archive/ documents/
# List the contents of the 'archive' directory to verify the files were moved
$ ls archive/
data.csv temp_file.tmp
3.4 Moving a Directory
To move the documents
directory into the archive
directory:
# Move the 'documents' directory into the 'archive' directory
$ mv documents archive/
# List the contents of the current directory to confirm 'documents' is no longer here
$ ls
archive/
# List the contents of the 'archive' directory to verify 'documents' was moved
$ ls archive/
data.csv documents/ temp_file.tmp
# List the contents of 'archive/documents' to confirm the file inside is still present
$ ls archive/documents/
report_final.txt
3.5 Renaming a Directory
To rename the archive
directory to backup
:
# Rename the 'archive' directory to 'backup'
$ mv archive backup
# List the contents of the current directory to see the new directory name
$ ls
backup/
# List the contents of the 'backup' directory to confirm it contains the same items
$ ls backup/
data.csv documents/ temp_file.tmp
4. Key Options Explained (with Examples)
The mv
command offers options to control its behavior, particularly for overwrites, verbosity, and backups.
-v
or --verbose
: Explain What Is Being Done
4.1 Displays a message for each move/rename operation.
# Create a new file named 'file_to_move.txt' to use in the example
$ touch file_to_move.txt
# Create a new directory named 'target_dir' as the destination
$ mkdir target_dir
# Move 'file_to_move.txt' into 'target_dir' and display a verbose message about the action
$ mv -v file_to_move.txt target_dir/
renamed 'file_to_move.txt' -> 'target_dir/file_to_move.txt'
-i
or --interactive
: Prompt Before Overwrite
4.2 Prompts for confirmation before overwriting an existing file, enhancing safety. Many systems alias mv
to mv -i
.
# Create a file named 'destination.txt' with some initial content
$ echo "Original content" > destination.txt
# Create a file named 'source.txt' with different content to move
$ echo "New content" > source.txt
# Attempt to move 'source.txt' to 'destination.txt' with a prompt before overwriting
$ mv -i source.txt destination.txt
mv: overwrite 'destination.txt'? y
Note: Typing ‘n’ prevents overwriting, leaving source.txt
intact.
-n
or --no-clobber
: Do Not Overwrite Existing Files
4.3 Silently prevents overwriting existing files.
# Create a file named 'target_file.txt' with content to protect from overwrite
$ echo "Existing data" > target_file.txt
# Create a file named 'source_file.txt' with different content to move
$ echo "Source data" > source_file.txt
# Attempt to move 'source_file.txt' to 'target_file.txt' without overwriting the existing file
$ mv -n source_file.txt target_file.txt
mv: not replacing 'target_file.txt'
# Verify that 'target_file.txt' still contains its original content
$ cat target_file.txt
Existing data
# Confirm that 'source_file.txt' still exists and was not moved
$ ls source_file.txt
source_file.txt
-u
or --update
: Move Only When Source is Newer or Destination is Missing
4.4 Moves only if the source is newer than the destination or the destination does not exist.
# Create a file named 'file.txt' as the destination with some content
$ echo "Destination content" > file.txt
# Create a file named 'source.txt' as the source with different content
$ echo "Source content" > source.txt
# Set the timestamp of 'source.txt' to an older date to make it older than 'file.txt'
$ touch -t 202501010000 source.txt
# Attempt to move 'source.txt' to 'file.txt' with update option; no move occurs because source is older
$ mv -uv source.txt file.txt
# No output
# Update the timestamp of 'source.txt' to make it newer than 'file.txt'
$ touch source.txt
# Attempt to move 'source.txt' to 'file.txt' again with update option; move occurs because source is now newer
$ mv -uv source.txt file.txt
renamed 'source.txt' -> 'file.txt'
-f
or --force
: Overwrite Without Prompting
4.5 Forces overwrites without prompting, overriding -i
or -n
. Use cautiously to avoid data loss.
# Create a file named 'target.txt' with initial content
$ echo "Target data" > target.txt
# Create a file named 'source.txt' with different content to overwrite with
$ echo "Source data" > source.txt
# Forcefully move 'source.txt' to 'target.txt', overwriting without prompting
$ mv -f source.txt target.txt
# Verify that 'target.txt' now contains the content from 'source.txt'
$ cat target.txt
Source data
# Confirm that 'source.txt' no longer exists after the move
$ ls source.txt
ls: cannot access 'source.txt': No such file or directory
-b
or --backup[=CONTROL]
: Create Backups Before Overwriting
4.6 Creates a backup of the destination file before overwriting, typically with a ~
suffix.
# Create a file named 'config.cfg' with initial content
$ echo "Version 1" > config.cfg
# Create a file named 'new_config.cfg' with updated content to move
$ echo "Version 2" > new_config.cfg
# Move 'new_config.cfg' to 'config.cfg' while creating a backup of the original 'config.cfg'
$ mv -b new_config.cfg config.cfg
# List the directory contents to see the backup file with the '~' suffix
$ ls
config.cfg config.cfg~
# Verify that 'config.cfg' now contains the updated content
$ cat config.cfg
Version 2
# Verify that the backup file 'config.cfg~' contains the original content
$ cat config.cfg~
Version 1
Explanation: The CONTROL argument (e.g., numbered
, simple
) can customize backup naming; see man mv.
-S SUFFIX
or --suffix=SUFFIX
: Specify Backup Suffix
4.7 Overrides the default ~
suffix for backups.
# Create a file named 'data.txt' with initial content
$ echo "Original data" > data.txt
# Create a file named 'new_data.txt' with updated content to move
$ echo "Updated data" > new_data.txt
# Move 'new_data.txt' to 'data.txt' with a backup using a custom '.bak' suffix
$ mv -b -S .bak new_data.txt data.txt
# List the directory contents to see the backup file with the '.bak' suffix
$ ls
data.txt data.txt.bak
-T
or --no-target-directory
: Treat DEST as a Normal File
4.8 Ensures DEST
is treated as a file, not a directory, preventing unintended moves into directories.
# Create a directory named 'existing_dir' to test moving behavior
$ mkdir existing_dir
# Create a file named 'some_file' to move
$ touch some_file
# Move 'some_file' into 'existing_dir' without the -T option, treating 'existing_dir' as a directory
$ mv some_file existing_dir
# List the contents of 'existing_dir' to confirm the file was moved inside
$ ls existing_dir/
some_file
# Create another file named 'some_file' to test with -T
$ touch some_file
# Attempt to move 'some_file' to 'existing_dir' with -T, treating 'existing_dir' as a file, which fails because it's a directory
$ mv -T some_file existing_dir
mv: cannot overwrite directory 'existing_dir' with non-directory 'some_file'
# Create a new file named 'another_file' to rename using -T
$ touch another_file
# Move 'another_file' to 'new_name' with -T, treating 'new_name' as a file for renaming
$ mv -T another_file new_name
# List the file 'new_name' to verify the rename was successful
$ ls new_name
new_name
5. Important Considerations
- Permissions: Moving/renaming requires write permission on the parent directories of both source and destination. Write permission on the source file is typically not needed unless moving across filesystems.
- Moving Across Filesystems: Cross-filesystem moves (e.g., from
/home
to/media/usb
) involve copying and deleting, potentially losing metadata like ownership. Read permission on the source file and write permission on the source directory are required. - Atomicity: Moves within the same filesystem are atomic, ensuring no partial states. Cross-filesystem moves are not atomic.
- Overwriting: Overwrites are silent unless
mv -i
is used. Use-i
or-n
to prevent accidental data loss. - Spaces/Special Characters: Quote or escape filenames with spaces/special characters:
# Rename with spaces $ mv "old name.txt" "new name.doc" # OR $ mv old name.txt new name.doc
6. Frequently Asked Questions (FAQ)
cp
and mv
?
6.1 What’s the difference between cp
copies, leaving originals intact.mv
moves/renames, removing originals.
6.2 How do I rename a file?
$ mv old_filename new_filename
6.3 How do I move a file without overwrite prompts?
$ mv -f source destination
Caution: This can cause data loss.
6.4 How do I move a file only if the destination doesn’t exist?
$ mv -n source destination
6.5 How do I move all files, including hidden ones?
- Best: Move directory contents:
# Move the entire 'source_dir' into 'destination_dir', creating 'destination_dir/source_dir/' # This includes all files and hidden files within 'source_dir', but they remain inside the subdirectory $ mv source_dir/. destination_dir/
- Bash-specific: Enable dotglob:
# Enable the dotglob shell option to make '*' match hidden files $ shopt -s dotglob # Move all files (including hidden ones) from 'source_dir' directly into 'destination_dir' $ mv source_dir/* destination_dir/ # Disable the dotglob shell option to restore default behavior $ shopt -u dotglob
mv
fail?
6.6 Can Yes, due to:
- Insufficient permissions.
- Moving a directory into itself.
- Non-existent source.
- Incompatible overwrite attempts.
- Filesystem errors.
6.7 Is moving faster than copying?
- Same filesystem: Yes, as it updates metadata only.
- Different filesystems: No, as it copies and deletes.
7. Conclusion
The mv
command is a powerful tool for efficient file and directory management in Linux. Its atomic moves within the same filesystem and versatile options (-i
, -n
, -f
, -u
, -b
) make it indispensable. Always consider permissions and overwrite risks to ensure safe operations.
mv
Command: Reference Table of Key Options
8. (Move or rename files and directories)
Option(s) | Description | Example Command | Use Case |
---|---|---|---|
-v , --verbose | Explain what is being done | mv -v old.txt new.txt | See confirmation for each move/rename operation |
-i , --interactive | Prompt before overwriting an existing destination file | mv -i file.txt target_dir/ | Prevent accidental overwrites by requiring confirmation |
-n , --no-clobber | Do not overwrite an existing file | mv -n source.txt dest.txt | Ensure existing files are never replaced |
-u , --update | Move only when source is newer or destination is missing | mv -u *.patch backup/ | Update files in a destination, avoiding older source files |
-f , --force | Force overwrite without prompting (overrides -i , -n ) | mv -f source.txt dest.txt | Automate overwrites in scripts (use with extreme caution) |
-b , --backup[=CONTROL] | Create a backup of overwritten files (default suffix ~ ) | mv -b config.ini old_config/ | Keep a copy of the original file before overwriting |
-S SUFFIX , --suffix=SUFFIX | Specify a custom backup Suffix (used with -b ) | mv -bS .bak file.txt dir/ | Use a specific suffix (e.g., .bak ) for backups |
-t DIR , --target-directory=DIR | Move all SOURCE arguments into specified DIRECTORY | mv -t backup/ *.log *.txt | Specify destination directory first when moving many files |
-T , --no-target-directory | Treat DEST as a normal file, not a directory | mv -T source.txt name | Ensure renaming occurs, preventing move into existing dir |