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 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 uname Command: Check Kernel and System Architecture

Linux uname Command: Check Kernel and System Architecture

Learn how to check Kernel and System Architecture with the Linux uname 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 cd Command: Change the Current Directory

Linux cd Command: Change the Current Directory

Learn how to use the Linux cd command to move between directories using absolute, relative, home, and previous paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux blkid Command: Find Block-Device UUIDs and Filesystem Types

Linux blkid Command: Find Block-Device UUIDs and Filesystem Types

Learn how to find Block-Device UUIDs and Filesystem Types with the Linux blkid command, including practical examples, key options, and important precautions.

Linux Tutorial / Navigate Directories with pwd, ls, cd, and tree

Linux Tutorial / Navigate Directories with pwd, ls, cd, and tree

Use pwd, ls, cd, and tree to find your current location, inspect directories, navigate paths, and check where each command takes you.

Linux whoami Command: Show the Effective User Name

Linux whoami Command: Show the Effective User Name

Learn how to show the Effective User Name with the Linux whoami command, including practical examples, key options, and important precautions.

Linux htop Command: Explore Processes Interactively

Linux htop Command: Explore Processes Interactively

Learn how to explore Processes Interactively with the Linux htop command, including practical examples, key options, and important precautions.

Linux fold Command: Wrap Long Lines at a Fixed Width

Linux fold Command: Wrap Long Lines at a Fixed Width

Learn how to wrap Long Lines at a Fixed Width with the Linux fold command, including practical examples, key options, and important precautions.

Linux date Command: Display and Format the Current Time

Linux date Command: Display and Format the Current Time

Learn how to display and Format the Current Time with the Linux date command, including practical examples, key options, and important precautions.