opscape

Files and directories

mv

Move or rename files and directories.

mv [OPTION...] SOURCE... DEST

12 of 12 examples run in Debian 13.7 on 2026-10-31 · coreutils 9.7-3 · manual: mv(1)

mv does two jobs with one mechanism. Within one filesystem, moving is just renaming: the directory entry changes, the data stays where it is, so even a huge directory moves instantly and keeps its timestamps, owner and inode. Across filesystems, mv has to copy everything and then delete the original, which takes time and can fail halfway.

mv old new renames. mv file... dir/ moves files into an existing directory. Directories need no -r. Like cp, mv overwrites an existing destination without asking unless you add -i (ask) or -n (never overwrite).

Renaming many files by a pattern is not something mv does itself; use a shell loop with parameter expansion, shown below.

Examples

Rename a file

mv todo.txt tasks.txt
ls *.txt
output
file with spaces.txt
notes.txt
tasks.txt

todo.txt no longer exists; the same file is now called tasks.txt.

Move files into a directory

mkdir archive
mv logs/app.log.1 logs/error.log archive/
ls archive logs
output
archive:
app.log.1
error.log

logs:
app.log

With a directory as the last argument, every source moves into it and keeps its name.

Rename a directory

mv photos images
ls -d images photos
outputexit 2
ls: cannot access 'photos': No such file or directory
images

Directories move like files, no -r needed. The ls confirms photos is gone and images exists.

A rename keeps the same file

ls -i notes.txt
mv notes.txt meeting.txt
ls -i meeting.txt
output
9825349 notes.txt
9825349 meeting.txt

The inode number is unchanged: on the same filesystem, mv only rewrites the directory entry. That is also why a process that has the file open keeps writing to it after a rename.

mv overwrites silently

mv todo.txt notes.txt
cat notes.txt
output
buy milk
fix login bug
write tests
fix logout bug
deploy v2.4

notes.txt already existed and has been replaced by the contents of todo.txt, with no warning. The old notes are gone.

Refuse to overwrite with -n

mv -n todo.txt notes.txt
ls todo.txt notes.txt
output
notes.txt
todo.txt

-n leaves both files alone because notes.txt exists.

Ask before overwriting

mv -i todo.txt notes.txt
head -1 notes.txt
output
mv: overwrite 'notes.txt'? Meeting notes, March

-i prompts when the target exists; the answer n (from standard input here) keeps the original notes.txt.

See each move

mv -v data/*.csv .
explain
output
renamed 'data/sales.csv' -> './sales.csv'
renamed 'data/users.csv' -> './users.csv'

-v prints 'from' -> 'to' for each file, a useful record when a glob is involved.

Rename many files by pattern

Setup run before this example
touch report-1.txt report-2.txt report-3.txt
for f in report-*.txt; do
  mv -- "$f" "${f%.txt}.md"
done
ls report-*
output
report-1.md
report-2.md
report-3.md

${f%.txt} strips the .txt suffix, and the loop renames each file. Quoting "$f" keeps names with spaces intact, and -- protects against names starting with a dash.

Keep the file you are replacing

mv -b todo.txt notes.txt
ls notes.txt*
output
notes.txt
notes.txt~

-b renames the existing notes.txt to notes.txt~ before the move replaces it.

Moving a file that does not exist

mv draft.txt final.txt
explain
outputexit 1
mv: cannot stat 'draft.txt': No such file or directory

mv reports the missing source and exits with 1.

A trailing slash catches typos in the destination

mv todo.txt archive/
explain
outputexit 1
mv: cannot move 'todo.txt' to 'archive/': Not a directory

archive does not exist. With the trailing slash, mv knows you meant a directory and fails instead of renaming todo.txt to a file called archive.

Common pitfalls

  • mv a b where b is an existing directory moves a into it; where b does not exist, it renames. Use mv -T a b to always mean rename, or a trailing slash to always mean into.

  • A cross-filesystem mv of a big tree that gets interrupted leaves a partial copy and an intact original. For large moves between disks, rsync --remove-source-files can resume.

  • Moving a file within the same filesystem keeps its original permissions and owner; it does not pick up the new directory's defaults.

GNU vs BSD and macOS

The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).

  • -n, -i, -f and -v exist on both. -T, -t DIR, --backup/-b and --exchange are GNU options; check your system's man page on BSD/macOS.

Options worth knowing

A selection, described in our own words. For the full list, see mv(1) or mv --help.

OptionWhat it does
-i, --interactiveAsk before overwriting.
-n, --no-clobberNever overwrite an existing file.
-f, --forceDo not prompt before overwriting.
-v, --verbosePrint each move.
-uMove only when the source is newer or the destination is missing.
-bBack up each destination file that would be replaced.
--backup[=CONTROL]Back up replaced files: numbered, existing, simple or none.
-S, --suffix SUFFIXUse SUFFIX for backups instead of ~.
-t, --target-directory DIRMove all sources into DIR.
-T, --no-target-directoryTreat DEST as a name, never a directory to move into.
--exchangeSwap SOURCE and DEST atomically (needs filesystem support).

Related