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 directory and navigate into it
$ mkdir mv_test_dir
$ cd mv_test_dir
# Create some files
$ echo "Report content." > report_draft.txt
$ echo "Data for analysis." > data.csv
$ touch temp_file.tmp
# Create directories
$ mkdir archive
$ mkdir documents
# Verify the setup
$ 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
$ mv report_draft.txt report_final.txt
# Verify the change
$ 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
$ mv report_final.txt documents/
# Verify
$ ls
archive/ data.csv documents/ temp_file.tmp
$ 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 multiple files
$ mv data.csv temp_file.tmp archive/
# Verify
$ ls
archive/ documents/
$ ls archive/
data.csv temp_file.tmp
3.4 Moving a Directory
To move the documents
directory into the archive
directory:
# Move the directory
$ mv documents archive/
# Verify
$ ls
archive/
$ ls archive/
data.csv documents/ temp_file.tmp
$ ls archive/documents/
report_final.txt
3.5 Renaming a Directory
To rename the archive
directory to backup
:
# Rename the directory
$ mv archive backup
# Verify
$ ls
backup/
$ 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.
4.1 -v
or --verbose
: Explain What Is Being Done
Displays a message for each move/rename operation.
# Create a file to move
$ touch file_to_move.txt
$ mkdir target_dir
# Move verbosely
$ mv -v file_to_move.txt target_dir/
renamed 'file_to_move.txt' -> 'target_dir/file_to_move.txt'
4.2 -i
or --interactive
: Prompt Before Overwrite
Prompts for confirmation before overwriting an existing file, enhancing safety. Many systems alias mv
to mv -i
.
# Create destination file
$ echo "Original content" > destination.txt
# Create source file
$ echo "New content" > source.txt
# Attempt move with prompt
$ mv -i source.txt destination.txt
mv: overwrite 'destination.txt'? y
Note: Typing ‘n’ prevents overwriting, leaving source.txt
intact.
4.3 -n
or --no-clobber
: Do Not Overwrite Existing Files
Silently prevents overwriting existing files.
# Ensure destination exists
$ echo "Existing data" > target_file.txt
# Create source
$ echo "Source data" > source_file.txt
# Attempt move with no-clobber
$ mv -n source_file.txt target_file.txt
mv: not replacing 'target_file.txt'
# Verify target_file.txt was not changed
$ cat target_file.txt
Existing data
$ ls source_file.txt
source_file.txt
4.4 -u
or --update
: Move Only When Source is Newer or Destination is Missing
Moves only if the source is newer than the destination or the destination does not exist.
# Create destination, then make source older
$ echo "Destination content" > file.txt
$ echo "Source content" > source.txt
$ touch -t 202501010000 source.txt
# Try update (source is older, move doesn't happen)
$ mv -uv source.txt file.txt
# No output
# Make source newer
$ touch source.txt
# Try update again (source is newer, move happens)
$ mv -uv source.txt file.txt
renamed 'source.txt' -> 'file.txt'
4.5 -f
or --force
: Overwrite Without Prompting
Forces overwrites without prompting, overriding -i
or -n
. Use cautiously to avoid data loss.
# Create files
$ echo "Target data" > target.txt
$ echo "Source data" > source.txt
# Force overwrite
$ mv -f source.txt target.txt
# Verify target.txt was overwritten
$ cat target.txt
Source data
$ ls source.txt
ls: cannot access 'source.txt': No such file or directory
4.6 -b
or --backup[=CONTROL]
: Create Backups Before Overwriting
Creates a backup of the destination file before overwriting, typically with a ~
suffix.
# Create files
$ echo "Version 1" > config.cfg
$ echo "Version 2" > new_config.cfg
# Move with backup
$ mv -b new_config.cfg config.cfg
# Verify backup
$ ls
config.cfg config.cfg~
$ cat config.cfg
Version 2
$ cat config.cfg~
Version 1
Explanation: The CONTROL argument (e.g., numbered
, simple
) can customize backup naming; see man mv .
4.7 -S SUFFIX
or --suffix=SUFFIX
: Specify Backup Suffix
Overrides the default ~
suffix for backups.
# Create files
$ echo "Original data" > data.txt
$ echo "Updated data" > new_data.txt
# Move with custom suffix
$ mv -b -S .bak new_data.txt data.txt
# Verify backup
$ ls
data.txt data.txt.bak
4.8 -T
or --no-target-directory
: Treat DEST as a Normal File
Ensures DEST
is treated as a file, not a directory, preventing unintended moves into directories.
# Create a directory
$ mkdir existing_dir
# Create a file
$ touch some_file
# Without -T (moves file INTO directory)
$ mv some_file existing_dir
$ ls existing_dir/
some_file
# With -T (fails if dir exists)
$ touch some_file
$ mv -T some_file existing_dir
mv: cannot overwrite directory 'existing_dir' with non-directory 'some_file'
# With -T (renames to new_name)
$ touch another_file
$ mv -T another_file new_name
$ 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)
6.1 What’s the difference between cp
and mv
?
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:
$ mv source_dir/. destination_dir/
- Bash-specific: Enable dotglob:
$ shopt -s dotglob $ mv source_dir/* destination_dir/ $ shopt -u dotglob
6.6 Can mv
fail?
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.
8. mv
Command: Reference Table of Key Options
(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 |