mv
Move or rename files and directories
By CMD Script Team · 4 min read · Last updated
mv [OPTIONS] SOURCE... DESTOptions
| Flag | Description |
|---|---|
-i | Prompt for confirmation before overwriting an existing destination file |
-n | Never overwrite an existing destination file |
-f | Force the move, overwriting destination files without prompting |
-v | Print a message for each file moved or renamed |
-u | Move only when the source is newer than the destination or the destination is missing |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
mv moves files or directories to a new location, or renames them if the destination
is within the same directory. Unlike cp, it doesn't leave a duplicate behind — the
source path stops existing once the move completes. Renaming is really just a special
case of moving: giving a new path in the same directory.
Beginner examples
mv oldname.txt newname.txt— rename a filemv file.txt /var/backups/— move a file into another directory, keeping its namemv -i draft.txt final.txt— prompt for confirmation before overwritingfinal.txtif it existsmv old_project/ new_project/— rename or relocate an entire directory in one command
mv draft.txt final.txt
Advanced examples
- Move several files into a directory at once:
mv *.log /var/backups/moves every matching file, keeping each one's original name inside the destination directory. - Avoid clobbering existing files during a bulk move:
mv -n *.conf /etc/app/conf.d/skips any file that already exists at the destination instead of overwriting it. - Move only newer files, useful for simple one-way syncs:
mv -u *.csv /data/processed/. - Understand the cross-filesystem cost: moving a large file between different mounted
filesystems (
mv /mnt/diskA/bigfile /mnt/diskB/) is a full copy-then-delete internally, unlike a same-filesystem move, which is a near-instant rename.
mv -vn *.log /var/backups/
Common mistakes
- Assuming a move across filesystems (e.g. between two different mounted drives) is instant like a same-filesystem rename — it's actually a full copy followed by deleting the original, so it takes time and requires enough free space at the destination.
- Not noticing that if the destination is an existing directory, the source is moved
into it rather than replacing it —
mv report.txt existing_dirresults inexisting_dir/report.txt, notexisting_dirbeing replaced byreport.txt. - Overwriting an important file by mistake because
mvdoesn't prompt by default — always consider-ior-nfor anything not fully scripted and verified. - Forgetting a trailing slash mismatch when moving multiple sources into a destination
that doesn't yet exist, causing
mvto treat the last argument as a rename target instead of a directory to move into.
Tips
- Use
-iinteractively whenever overwriting the wrong file would be costly, and-nin scripts where you want a guaranteed no-clobber guarantee without prompts. - Remember that a same-filesystem
mvis nearly instantaneous regardless of file size, since it's just a metadata change — useful to know when moving very large files. - When moving a directory, double check whether the destination already exists: if it does, your source becomes a subdirectory of it rather than replacing it.
Best practices
- Use
-nin automated or scripted moves to avoid silently overwriting files that may already exist at the destination, especially in shared directories. - Prefer
mvovercp+rmwhen relocating files — it's atomic on the same filesystem (no window where both copies exist, or where a crash mid-copy leaves partial data) and avoids the extra step of manually deleting the source. - Be explicit about trailing slashes and destination existence when scripting moves of multiple files, to avoid ambiguity about whether the last argument is a directory or a rename target.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Renaming a draft file to its final name once work on it is complete:
mv draft.md README.md. - Archiving log files into a dated backup directory as part of a log-rotation script.
- Reorganizing a project's directory structure by moving files and folders into new locations without duplicating data.
Common interview questions
- What's the difference between
mvandcp?mvrelocates or renames a file, removing it from its original path;cpcreates a separate duplicate and leaves the original untouched. - Why is moving a file within the same filesystem much faster than moving it across
filesystems? A same-filesystem move is just a directory-entry rename — an
essentially instant metadata operation. A cross-filesystem move requires
mvto copy the actual data to the new filesystem and then delete the original, which scales with file size. - What happens when you
mva file into a path that is an existing directory versus one that isn't? If the destination is an existing directory, the file is moved into it under its original name; if the destination doesn't exist as a directory,mvtreats it as the new full name/path for the file, effectively renaming it.
Frequently Asked Questions
What's the difference between mv and cp?
mv relocates or renames a file — the original path stops existing and the data appears at the new path, typically with no data duplication if staying on the same filesystem (it's often just a metadata rename). cp creates a genuinely separate duplicate, leaving the original untouched.
Why is mv sometimes slow across directories but instant within one directory?
Moving a file within the same filesystem is usually just a directory-entry rename — fast regardless of file size. Moving across filesystems (e.g. from one mounted disk/partition to another) requires mv to actually copy the data and then delete the original, which takes time proportional to file size.
What happens if the destination is an existing directory versus a new name?
If DEST is an existing directory, mv moves the source into it, keeping the original filename (e.g. mv file.txt existing_dir/ results in existing_dir/file.txt). If DEST doesn't exist as a directory, mv treats it as the new name/path for the file, effectively renaming it.
Does mv overwrite files by default?
Yes, like cp, a plain mv silently overwrites an existing destination file with no warning. Use -i to be prompted, or -n to skip existing destinations entirely.
Cheat sheet
Download a quick-reference cheat sheet for mv.