mv — move or rename
The mv command renames a file or directory and moves it elsewhere; it silently replaces an existing target.
Cheat sheet
9 commands Printable version-
mv old.txt new.txtrename -
mv report.txt ~/archive/move into a directory, same name -
mv a.txt b.txt docs/several files into a directory -
mv project ~/old-projecta directory — no options, whole -
mv -i new.conf app.confask before overwriting -
mv -n new.conf app.confnever overwrite an existing file -
mv -v *.log /tmp/logs/print each move -
mv -b new.conf app.confthe old one becomes app.conf~ -
mv -- -file.txt file.txta name that starts with a dash
Overview
mv (move) does two jobs with one command. If the destination is an existing
directory, the file moves into it. Otherwise the file gets a new name.
Within one filesystem mv doesn't copy data: only the directory entry changes, so
even a large directory "moves" instantly. Across filesystems mv copies and then
deletes the source, which takes longer.
mv moves directories without -r. It silently overwrites an existing destination
file.
Options
7The safety options are the same as in cp: -i, -n, -b. Full list — man mv.
| Option | What it does | Example |
|---|---|---|
-i |
Prompt before overwriting an existing file. | mv -i new.conf app.conf |
-n |
Never overwrite existing files. | mv -n new.conf app.conf |
-v |
Print each action — renamed 'a' -> 'b'. | mv -v *.log /tmp/logs/ |
-f |
Don't prompt — overrides -i, including one from an alias. | mv -f new.conf app.conf |
-b |
Before overwriting, keep the old file with ~ appended to its name. | mv -b new.conf app.conf |
-u |
Move only when the source is newer than the destination or it's missing. | mv -u *.conf /srv/conf/ |
-t dir |
Destination directory as the first argument — handy with find and xargs. | mv -t /tmp/logs/ a.log b.log |
Nothing found. Try a shorter query.
Recipes
4Move all files matching a pattern
The shell expands *.log (see globbing). Check what it matches first: ls
*.log.
Rename many files: .txt → .md
${f%.txt} cuts off the extension; the quotes keep names with spaces intact.
Replace a live file safely
Within one filesystem mv replaces the file atomically: a program sees either the
old file or the new one, never half of it.
Troubleshooting
5mv: cannot stat 'x': No such file or directory
There's no source at that path. Check pwd and the name with ls; mind the case.
mv: cannot move 'src' to 'dst/src': Directory not empty
dst already holds a non-empty directory with the same name. mv doesn't merge
directories. Move the contents (mv src/* dst/src/) or pick another name.
Permission denied
mv needs write permission on both directories — source and destination; the file's
own permissions don't matter. Check ls -ld on both; system directories need sudo
mv.
The file is gone after mv
The destination was not a directory but a file with that name, and mv overwrote
it. Or the destination directory didn't exist, and the file was simply renamed to
that name. Check ls -l at the destination path.
mv: invalid option -- 'l' with the name -file.txt
mv reads a name starting with a dash as a bundle of options -f -i -l … and trips
on the first unknown letter. Put -- before the names or give a path: mv
./-file.txt file.txt.
Danger zone
2Overwriting without asking
mv new.conf app.conf replaces app.conf if it exists, and the old contents are
gone. Until you're sure, use mv -i or mv -n.
A typo in the destination directory
If /tmp/lgos doesn't exist, mv *.log /tmp/lgos renames a single file to lgos,
and with several files it refuses to run. End directory paths with /
(/tmp/logs/): then mv checks that the directory exists.
See also
- cp — copy and leave the source in place.
- rm — delete a file or directory.
- ls — check the result.
- globbing —
*,?,{}patterns for many files. - files-basics — an overview of the basic file commands.
Found an inaccuracy? Report an error in the article