mv renames a file
Two plain names in the same directory is a rename. The first is the file
now, the second is the file after.
$ mv notes.txt notes-old.txt
$ ls
archive
logs
notes-old.txt
reports
Nothing was copied. On the same filesystem mv issues a
rename, which changes the name in the directory and leaves the inode, the
bytes, the owner and the permissions exactly where they were. A ten gigabyte
file renames in the time a small one does, because the data never moves.
-v tells you what happened if you want the receipt.
$ mv -v notes-old.txt notes.txt.final
renamed 'notes-old.txt' -> 'notes.txt.final'
The two ways this command catches people both come from the same place.
The destination can be a name or a directory, and
mv decides which one it is before you finish typing.
Moving a file into a directory
When the last argument is a directory that already exists,
mv moves the file inside it and keeps the file's name.
$ mv reports/q3.csv reports/2026/
$ ls reports/2026
q3.csv
Same command, different outcome. This is the ambiguity you have to hold
in your head. mv q3.csv reports/ puts it inside
reports, and mv draft.md published.md renames it,
and the only difference is whether the target is an existing directory. If
you mean the directory and there's a file with that name, you get an
overwrite instead. -t removes the doubt.
$ mv -t archive/ logs/app.log
$ ls archive
app.log
No output from the first command, which is how a successful
mv usually answers, and the listing proves where the file
went. The directory has to exist first, and that's the useful difference
from the plain form. Point -t at a name that isn't there and
mv stops with a missing target directory error and a status of
one, where mv file name would have renamed the file instead.
With -t the directory comes first and every later argument
goes inside it. The order flips compared with the usual form, which is the point. In a script the directory is stated once at the front and every source after it goes inside, so no argument can be mistaken for a new file name.
When the destination directory exists
This is the part people ask about, and the fear is reasonable. Does
mv q3-2026.csv reports/2026 replace the directory? It doesn't.
When the destination is an existing directory, the file goes inside it and
the directory stays exactly where it was.
$ mv q3-2026.csv reports/2026 ; echo $?
0
$ find . | sort
.
./reports
./reports/2026
./reports/2026/q3-2026.csv
$ cat reports/2026/q3-2026.csv
region,revenue
us,41000
Exit zero, the file sits inside the year directory, and the contents came
through untouched. The trailing slash changes nothing in this case.
$ mv q4-2026.csv reports/2026/ ; echo $?
0
$ ls reports/2026
q3-2026.csv
q4-2026.csv
Both files inside, both forms identical. The slash matters only when the
destination doesn't exist, and then the two forms fail in opposite
directions. This is the realistic version of that mistake, a year typed
wrong in a path you know well.
$ mv q5-2026.csv reports/2027 ; echo $?
0
$ ls -l reports/2027
-rw-rw-r-- 1 deploy deploy 2 Nov 17 21:19 reports/2027
$ ls reports
2026
2027
No error. The directory 2027 didn't exist, so
mv treated the last argument as a new name and renamed the CSV
to a two byte file called 2027 inside reports. The
listing shows a name where you expected a directory, and that's the exact
moment to stop and look before the next command.
Add the slash and the same typo becomes a refusal instead.
$ mv q6-2026.csv reports/2027/ ; echo $?
mv: cannot stat 'reports/2027/': Not a directory
1
That capture has reports/2027 already sitting there as a
file, left over from the command above. Test it with the name absent and you
get a different wording for the same failure, a can't move message instead
of can't stat, and the exit status is still one. The trailing slash promised
a directory and the kernel disagreed either way.
That's why the trailing slash is worth typing when you mean a directory.
It converts a silent rename into a loud failure.
The data dies when the destination is an existing file rather than a
directory.
$ cat summary.csv
final numbers
$ mv q7-2026.csv summary.csv ; echo $?
0
$ cat summary.csv
revised numbers
The original contents are gone, it exits zero, and it never asks first. A file with the
name you typed is the victim. A directory is only at risk when you point
-T at one, which replaces the destination outright when it's
empty and refuses when it's not. That's what the guards in the next section exist
for.
Moving several files, or a whole directory
There's no recursive flag to remember, because mv is
recursive by design. A directory moves with everything inside it, and
-r isn't a valid option at all.
$ mv -r reports archive/
mv: invalid option -- 'r'
Try 'mv --help' for more information.
$ mv reports archive/ ; echo $?
0
$ find archive | sort
archive
archive/reports
archive/reports/2026
archive/reports/2026/jan
archive/reports/2026/jan/q1.csv
The whole tree arrived under archive, keeping its own name,
with the file four levels down still where it was. That's the answer to
moving a directory into another directory. The files inside are never
enumerated, so it costs the same as renaming one file.
Several files into one directory is a list. Name them, or let the shell
name them.
$ mv in/*.csv out/ ; echo $?
0
$ ls out
f1.csv
f2.csv
f3.csv
The glob is expanded by the shell before mv runs, so
mv sees the same three names you would have typed. Two
consequences. A pattern that matches nothing arrives as the literal string
and fails with a missing file error, which is loud and fine. And a pattern
never matches hidden files, which is the quiet one.
$ ls -a export/2026
.
..
.env
q1
q2
summary.txt
$ mv export/2026/* dest/ ; echo $?
0
$ ls -a export/2026
.
..
.env
Everything visible moved and the .env stayed behind, because
* doesn't match a leading dot. If the contents really have to move, turn the option on for that shell and run it again. shopt is bash, so this line does nothing in a /bin/sh script or a busybox container, where it fails and the glob matches nothing.
$ shopt -s dotglob
$ mv export/2026/* dest/
$ ls -a export/2026
.
..
Empty, finally, and .env went with the rest. The equivalent
for a script is the -t form, which also takes many sources.
$ mv -t txt a.txt b.txt ; echo $?
0
$ ls txt
a.txt
b.txt
One trap belongs in this section because it looks like the same command.
Pointing a move at a directory that lives inside the thing being moved is
refused, which is the good news.
$ mv reports reports/2026/ ; echo $?
mv: cannot move 'reports' to a subdirectory of itself, 'reports/2026/reports'
1
mv notices and stops. The variant it doesn't notice is a
script that computes the destination from a variable and lands somewhere
unintended, which is why the guard in the caveat matters more than this
error message.
Renaming a directory
A directory is renamed the same way, and everything inside it comes
along without being touched.
$ mv reports archive-output
$ ls archive-output
2026
The paths inside the directory don't change on disk. Whatever referenced
reports/2026/q3.csv yesterday now needs
archive-output/2026/q3.csv, which is worth checking before you
rename a directory a service writes into. Anything holding the old absolute
path will fail on the next run, and the error will point at a directory that
no longer exists.
Guards before you overwrite
mv onto an existing file replaces it without asking. There's no prompt and no backup, so the safe
version of a rename starts with a flag.
$ mv -i logs/app.log archive/
mv: overwrite 'archive/app.log'? n
The answer was no, and the file stayed where it was. -i
asks whether or not the destination is writable, and it fails when the
answer isn't affirmative. Without a flag mv only asks when the
destination is unwritable and standard input is a terminal, which is why an
unattended move replaces the file silently. -n refuses the
overwrite without asking, which is what you want in anything unattended.
$ mv -n logs/app.log archive/
mv: not replacing 'archive/app.log'
$ ls archive
app.log
One line, both files still where they were. The exit status was one on
this box, and that's the part not to trust. The GNU manual describes
-n as a silent skip and marks the option deprecated because the
exit status differs between platforms, so a script that branches on it can
behave differently on another distribution. When a batch genuinely needs to
know, test the destination yourself with [ -e dest ] before the
move, which behaves the same on every version. Two
smaller rules from the same page. -n can't be combined with
-b, and only the last of -i, -f and
-n takes effect. There's a third option for the case where you
want both files. -b renames the existing destination out of the
way and appends a tilde.
$ mv -b logs/app.log archive/
$ ls archive
app.log
app.log~
Now there are two files, the new one and the old one with a tilde on the
end. That tilde convention is the same one cp and
patch use for a backup. It isn't a version control system, and
the next -b over the same name overwrites the tilde copy, so
it buys you one generation.
Are there reserved names in linux
No. A Linux filename is just bytes, so the names Windows forbids are
ordinary files here. The Windows rules only come back if you mount an NTFS,
exFAT or SMB filesystem, because then a different system is reading the
names.
Four constraints are real and none of them is a reserved word. A name
can't contain a slash, because the slash separates path components. It can't
contain a null byte, because C strings end there. . and
.. already exist in every directory. And a leading dash isn't
reserved, but every tool reads it as an option, which is what the double dash
is for.
$ touch -- -draft.csv
$ ls -- -draft.csv
-draft.csv
Names are case sensitive, so Report.csv and
report.csv are two files sitting side by side, and one name is
capped at 255 bytes rather than 255 characters.
Which one to use, at a glance
| Command | What it does | Reach for it when |
mv old new | Renames a file or directory | The destination name doesn't exist yet |
mv q3.csv reports/2026/ | Moves the file in, keeps its name | The destination is an existing directory |
mv -t dir file | Moves files into a named directory | You're scripting and want no ambiguity |
mv -i | Asks before overwriting | You're at a prompt and nervous about the name |
mv -n | Refuses to overwrite | An unattended script must not destroy a file |
mv -b | Keeps the old file with a tilde | You want both versions after the move |
mv -v | Prints what it renamed | You want proof of what happened |
git mv | Renames and stages the change | The file is tracked in git |
Verify it works
$ mv -v notes.txt notes-archive.txt
renamed 'notes.txt' -> 'notes-archive.txt'
$ ls notes*
notes-archive.txt
$ mv -n logs/app.log archive/ ; echo $?
mv: not replacing 'archive/app.log'
1
$ mv -b logs/app.log archive/ ; echo $?
0
$ ls archive
app.log
app.log~
The first two confirm the rename. The -n run is the one to
remember, because a refused overwrite exited 1 here, so
set -e stops instead of carrying on. The -b run
did the opposite and exited zero while keeping both files, which is the
contrast worth carrying into a script. I have also seen the opposite assumption
hurt, where someone wrapped mv -n in an if and
treated a refusal as a failed move rather than the point of the flag.
The honest caveat
Crossing filesystems changes the mechanism. A rename only works inside
one filesystem, so when the destination is on another mount mv
copies the file and then removes the original. Same command, same output,
different cost. Moving a large directory to another disk takes as long as
copying it, and a copy that fails halfway leaves you with a partial file at
the destination and the original still in place.
A name that starts with a dash reads as a flag, so
mv -- -draft.csv draft.csv is how you rename it.
For many files at once, mv only takes two names, so you
either loop or reach for the rename utility that ships with
most distributions and takes a pattern instead. In a rename loop over a
mounted volume I always add -v. A silent loop that moved
nothing looks exactly like a loop that worked.
The boring conclusion
Renaming a file is mv, and the command has two habits that catch
people. A directory as the destination moves instead of renames, and an
existing file at the destination gets replaced without a word. Add
-t in scripts, -i or -n when a file
must survive, and git mv when git is watching.
What's the file name you regret changing?
Frequently asked questions
How do I rename a file in Linux?
Run mv draft.md published.md. There's no separate rename step. When both arguments are plain names in the same directory, mv just renames the file and leaves the contents, owner and permissions alone.
Is there a rename command?
There's a separate rename utility on many distributions, and it's built for patterns across many files. For one file, mv does the job. For a batch rename by pattern, reach for rename or a short for loop.
How do I rename a directory?
Run mv with the directory name. Directories are renamed the same way. Anything already inside comes along, and nothing needs to be moved file by file.
How do I stop mv overwriting an existing file?
Add -n to refuse the overwrite, or -i to be asked first. Without either flag mv replaces the destination silently, which is how a rename quietly deletes a file you meant to keep.
Does mv replace a directory that already exists?
No. When the destination is an existing directory, mv puts the file inside it and leaves the directory in place, and the trailing slash makes no difference. mv only renames onto a new name when the destination doesn't exist, and only overwrites data when the destination is an existing file.
Are there reserved file names in Linux?
No. A Linux name is just bytes, so the names Windows forbids are ordinary files. A name can't hold a slash or a null byte, dot and dotdot already exist, a leading dash needs --, and one name is capped at 255 bytes.
How do I move all files from one directory to another in Linux?
Run mv with a glob, so something like mv export/2026/* dest/ moves everything visible. The glob never matches hidden files, so turn on dotglob for that shell or name them. A directory moves with its whole contents and needs no flag.
Do I need the -r flag with mv?
No, and mv rejects -r. Directories move with everything inside them by default.
Does mv keep the file permissions and owner?
Yes. A rename on the same filesystem only changes the name in the directory, so ownership, permissions and timestamps stay exactly as they were.
What happens if mv crosses filesystems?
mv copies the file and then removes the original. On the same filesystem it's a rename. Across filesystems it's a copy followed by a delete, so a large move takes as long as a copy.
How do I rename a file tracked by git?
Run git mv draft.md published.md. Git records the rename itself, and the next commit shows it as a move instead of a delete plus an add.
Resources
The two references below are the ones the command follows, so the flags
behave here the way they behave on your machine.
This builds on how to find a file on linux, since you usually rename what you just located. Next up is removing what you no longer want, which is rmdir and rm.
Next in the series. How to remove a directory on linux.
If you arrived here from a search that came back empty, start with how to find a file on linux.