Linux mv Command: Move and Rename Files and Directories

This guide explains how to move and rename files or directories with Linux mv, avoid unwanted overwrites, clarify destination paths, and handle cross-filesystem moves. It focuses on practical Linux usage, predictable automation, and the cases where a seemingly small path or option mistake can change the result.

The examples use GNU Coreutils behavior. Check mv --help, man mv, and your distribution documentation when portability or a version-specific option matters.

What Is the mv Command?

mv moves or renames files and directories. Within one filesystem it usually performs a fast rename. Across filesystems, GNU mv can copy the data and remove the source after the copy succeeds, so the operation can take much longer and may partially complete across a list of operands.

Basic Syntax

mv [OPTION]... [-T] SOURCE DESTINATION
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...

Quote paths that contain spaces. When supported, place -- before operands that begin with a hyphen so they are not interpreted as options.

Basic mv Usage

Rename a file

Specify the current path and the new path in the same directory.

mv draft.txt final.txt

Move a file into a directory

If the destination is an existing directory, the original basename is retained inside it.

mv report.pdf archive/

Move several files

When multiple sources are supplied, the final operand must be a destination directory.

mv *.log old-logs/
mv -t old-logs app.log access.log error.log

Checking the Results of Moving or Renaming

If mv is successful, it generally produces no output. After renaming, you can check whether the original path has disappeared and a new path has been created.

printf 'demo\n' > before.txt
mv before.txt after.txt
test -f after.txt && test ! -e before.txt
echo $?

An example output of 0 means that both conditions are true. When moving to a different file system, it may internally copy and delete files, so for large operations, also verify the size and contents of the resulting files.

Key Options

Option Purpose
-i Prompt before overwriting.
-n Do not overwrite an existing destination.
-f Attempt replacement without prompting.
-u Move according to update conditions.
-v Report processed paths.
-b Back up an existing destination before replacement.
-T Treat the destination as an exact path.
-t DIR Specify the target directory before sources.

Long options often make reviewed scripts easier to understand. When a script must run on non-GNU systems, verify every non-POSIX option on the target platform.

Practical Examples

Confirm or back up replacements

Interactive moves can use -i. Use -n when replacement is forbidden, or -b when the previous destination should be retained.

mv -iv new.conf /etc/myapp/config.conf
mv -bv new.conf /etc/myapp/config.conf

Make the destination unambiguous

An unexpectedly existing directory can change where a source is placed. -T treats the destination as the exact path, while -t clearly marks a target directory.

mv -T staging current
mv -t archive -- file1 file2

Plan a cross-filesystem move

Moving a large item to another mounted filesystem requires real copying and enough free space. Verify the destination and handle interruption before deleting any manually retained source copy.

df -h source-dir destination-dir
mv -v large-image.raw /mnt/archive/

mv and Related Commands

Command or method Primary role Best fit
mv Move or rename The source path should disappear
cp Create an independent copy Both source and copy should remain
rename Rule-based batch renaming Renaming many files with a known implementation
rsync --remove-source-files Transfer files and remove sources Controlled large or remote moves

Important Cautions

A move is not a trash operation

Overwriting a destination can destroy its previous contents without a desktop trash entry. Use confirmation, no-clobber behavior, or backups for important paths.

Cross-filesystem moves can partially succeed

In a multi-item move, earlier copies may finish before a later error. Inspect both source and destination after any failure.

Avoid a trailing slash on a directory symlink source

Systems can differ in how a symlink-to-directory with a trailing slash is handled. Use an unambiguous path when the link itself should move.

FAQ

Does mv copy data within the same filesystem?

Usually it performs a quick rename. Crossing a filesystem boundary can require copying and source removal.

How do I avoid overwriting a destination?

Use mv -n SOURCE DESTINATION, or -i for a prompt.

Are rename and move different commands?

Both use mv SOURCE DESTINATION; the destination's type and location determine the outcome.

How can I clearly move many files into one directory?

Use mv -t DIRECTORY -- FILE....

Related Resources

More in This Category
Linux uptime Command: Check Uptime and Load Averages

Linux uptime Command: Check Uptime and Load Averages

Learn how to use the Linux uptime command to check how long the system has been running and interpret load averages, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux grep Command: Search Text and Filter Command Output

Linux grep Command: Search Text and Filter Command Output

Learn how to use the Linux grep command to search files for text or regular expressions and filter command output, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux awk Command: Analyze Text by Fields

Linux awk Command: Analyze Text by Fields

Learn how to analyze Text by Fields with the Linux awk command, including practical examples, key options, and important precautions.

Linux kill Command: Send a Signal to a Process ID

Linux kill Command: Send a Signal to a Process ID

Learn how to send a Signal to a Process ID with the Linux kill command, including practical examples, key options, and important precautions.

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Understand standard input, output, and error; practice file redirection and pipes; and learn why overwrite risk and redirection order matter.

Linux tar Command: Create and Extract Archives

Linux tar Command: Create and Extract Archives

Learn how to create and Extract Archives with the Linux tar command, including practical examples, key options, and important precautions.

Linux history Command: Review and Reuse Shell Command History

Linux history Command: Review and Reuse Shell Command History

Learn how to use the Linux history command to review, search, reuse, and manage shell command history, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux printenv Command: Print Environment Variable Values

Linux printenv Command: Print Environment Variable Values

Learn how to use the Linux printenv command to print all environment variables or selected variable values, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux top Command: Monitor Processes in Real Time

Linux top Command: Monitor Processes in Real Time

Learn how to monitor Processes in Real Time with the Linux top command, including practical examples, key options, and important precautions.

Linux man Command: Read Command Manual Pages

Linux man Command: Read Command Manual Pages

Learn how to use the Linux man command to find and read command manuals, sections, options, and related pages, with essential options, practical examples, output interpretation, and common troubleshooting tips.