How to Rename a File in Linux: A Hands-On Guide with mv and rename
So you need to rename a file in Linux and you’ve gone looking for a `rename` command — only to find things are a little weirder than you expected. Here’s the thing nobody tells you up front: Linux doesn’t have a single, simple “rename” command the way you might assume. Instead, the workhorse you’ll reach for is `mv` — short for *move*. It does double duty: moving files around *and* renaming them.
In this guide we’re going to do it together, step by step. I’ll show you the exact commands, what output to expect, the one gotcha that can silently destroy a file, and how to batch-rename a whole folder at once. Open a terminal and follow along.
Key Takeaways
• `mv oldname newname` renames a single file in Linux — there’s no dedicated basic “rename” command; `mv` handles both moving and renaming.
• `mv` overwrites the destination silently if a file with the new name already exists. Use `mv -i` to be prompted first, or `mv -n` to refuse the overwrite entirely.
• Wrap filenames containing spaces in quotes, or escape the space, so the shell doesn’t split them.
• `mv` works on directories exactly like it does on files.
• For bulk renames, use the Perl-based `rename` command (`rename ‘s/old/new/’ *.txt`) or a simple `for` loop.
How do you rename a single file in Linux?
Let’s start with the basics. The syntax is simple:
“`bash mv oldname newname “`
Say you have a file called `report.txt` and you want to call it `report-final.txt`. Run this:
“`bash mv report.txt report-final.txt “`
Now run `ls` to confirm:
“`bash $ ls report-final.txt “`
That’s it — `report.txt` is gone and `report-final.txt` is in its place. Notice that `mv` doesn’t print anything on success. No “renamed successfully” message. In the Unix world, silence means it worked. If something *had* gone wrong (a typo in the source name, a permissions issue), you’d see an error instead:
“`bash $ mv reprot.txt report-final.txt mv: cannot stat ‘reprot.txt’: No such file or directory “`
So if `mv` says nothing, you’re good. Verify with `ls` and move on.
Why does mv overwrite files without warning?
Here’s the gotcha that bites people — including experienced admins — so pay attention. `mv` will overwrite the destination file silently if it already exists. No prompt. No warning. The old contents are just *gone*.
Let me show you. Suppose you have two files:
“`bash $ ls notes.txt archive.txt “`
If you run:
“`bash mv notes.txt archive.txt “`
…then `archive.txt` is now silently replaced with the contents of `notes.txt`. Whatever was in the original `archive.txt` is permanently overwritten. You get no second chance.
This is the single most important habit to build when renaming files in Linux: `mv` is both “move” and “rename,” and it clobbers the destination without asking. Whenever you rename — especially inside a script where a name collision could quietly destroy data — get in the habit of adding `-i` (interactive). It costs you one keystroke and saves you from data loss you might not notice for weeks.
How to make mv ask before overwriting
Add the `-i` (interactive) flag and `mv` will prompt you whenever it’s about to overwrite something:
“`bash $ mv -i notes.txt archive.txt mv: overwrite ‘archive.txt’? “`
Type `y` to proceed or `n` to cancel. This is your safety net.
How to make mv refuse to overwrite entirely
If you never want `mv` to overwrite — no prompt, just a flat refusal — use `-n` (no-clobber):
“`bash $ mv -n notes.txt archive.txt $ “`
Here `mv` silently *skips* the operation because `archive.txt` already exists. Your data stays safe. This is especially handy in scripts where you can’t sit there answering prompts.
There’s also a complementary habit: pass `-v` (verbose) so `mv` tells you what it did:
“`bash $ mv -v report.txt report-final.txt renamed ‘report.txt’ -> ‘report-final.txt’ “`
Combining them — `mv -iv` — gives you both the prompt and the confirmation message.
How do you rename files with spaces in the name?
Spaces are where a lot of people trip up, because the shell uses spaces to separate arguments. If you naively run this:
“`bash mv my report.txt my-report.txt “`
…the shell thinks you’re trying to move *two* files (`my` and `report.txt`) into a destination called `my-report.txt`, and you’ll get an error. The fix is to quote the names:
“`bash mv “my report.txt” “my report final.txt” “`
Now the shell treats each quoted string as a single argument. You can also escape the space with a backslash:
“`bash mv my\ report.txt my-report.txt “`
Both work. I prefer quotes — they’re easier to read. Run `ls` afterward and you’ll see the renamed file exactly as expected.
Can you rename a directory with mv too?
Yes — and this is the beauty of `mv` doing double duty. Renaming a directory is identical to renaming a file. No special flag, no different command:
“`bash mv old-project new-project “`
Verify it:
“`bash $ ls -d */ new-project/ “`
The directory and everything inside it keep their contents; only the folder name changes. If you want to be cautious about an existing target directory, the same `-i` and `-n` safety flags apply.
How do you rename multiple files at once in Linux?
Renaming one file is easy. But what about renaming fifty? This is where bulk renaming comes in, and you’ve got two solid approaches.
Option 1: The rename command (Perl-based)
The `rename` command is purpose-built for pattern-based mass renames. A quick but important heads-up: there are two different `rename` commands floating around in the Linux world.
- The Perl-based `rename` (sometimes called `prename` or `file-rename`) — this is the powerful one that takes a Perl substitution expression. It’s the default on Debian and Ubuntu.
- The util-linux `rename` — a simpler version found on some other distributions, with different syntax (`rename old new files`).
We’ll focus on the Perl-based one because it’s the most flexible and the most commonly referenced. Its syntax uses a Perl substitution: `’s/old/new/’`.
Say you have a pile of `.txt` files and you want them all to become `.bak`:
“`bash $ ls a.txt b.txt c.txt $ rename ‘s/\.txt$/.bak/’ *.txt $ ls a.bak b.bak c.bak “`
Let’s break down `’s/\.txt$/.bak/’`:
- `s/` — start a substitution
- `\.txt$` — match a literal `.txt` at the end of the name (the `$` anchors it; the `\.` means a literal dot)
- `/.bak/` — replace it with `.bak`
A hugely useful flag is `-n` (dry run), which shows you what *would* happen without actually doing it:
“`bash $ rename -n ‘s/\.txt$/.bak/’ *.txt rename(a.txt, a.bak) rename(b.txt, b.bak) rename(c.txt, c.bak) “`
Always run with `-n` first when you’re doing a big batch. Confirm the output looks right, then drop the `-n` and run it for real.
Option 2: A for loop
If `rename` isn’t installed, or you want full control, a `for` loop with `mv` does the job in pure Bash:
“`bash for f in *.txt; do mv “$f” “${f%.txt}.bak” done “`
Here `${f%.txt}` strips the `.txt` suffix from the filename, and we append `.bak`. Run it, then check:
“`bash $ ls a.bak b.bak c.bak “`
Note the quotes around `”$f”` — that’s the spaces-safety habit again, protecting you if any filename contains a space.
mv vs rename: which should you use?
Here’s a side-by-side to keep it straight:
| Task | Command | Best for |
|---|---|---|
| Rename a single file | `mv oldname newname` | Quick one-off renames |
| Rename + confirm overwrite | `mv -i old new` | Safety when target may exist |
| Rename, refuse overwrite | `mv -n old new` | Scripts, automation |
| Rename a directory | `mv olddir newdir` | Same as files |
| Bulk rename by pattern | `rename ‘s/old/new/’ *.ext` | Renaming many files at once |
| Bulk rename (no rename cmd) | `for f in *; do mv … done` | Portable, pure Bash |
The short version: `mv` for single files and directories, `rename` (or a loop) for bulk pattern jobs.
Where DarazHost fits in
Once you’re comfortable renaming and organizing files from the command line, you’ll want a server where you actually have that level of control. On a DarazHost VPS or dedicated server, you get full SSH root access — which means every command in this guide (`mv`, `rename`, batch loops, the safety flags) works exactly as shown, on your own machine, with no restrictions.
Prefer a graphical approach? DarazHost shared hosting plans include cPanel’s File Manager, where you can rename files and folders through a simple GUI — right-click, choose Rename, done — no terminal required. It’s a friendly on-ramp if you’re still getting comfortable with the command line.
Either way you get reliable, well-maintained hosting and 24/7 support standing by if you ever rename something into a corner and need a hand. Whether you live in the terminal or prefer point-and-click, you’ve got the tools to manage your files your way.
Frequently Asked Questions
Q: Is there a dedicated “rename” command in Linux like in Windows? A: Not as a basic built-in. The standard way to rename a single file is `mv oldname newname`. There *is* a separate `rename` command, but it’s designed for bulk, pattern-based renaming and isn’t installed by default on every distribution.
Q: Will mv ask me before overwriting an existing file? A: No — by default `mv` overwrites silently. Use `mv -i` to be prompted before overwriting, or `mv -n` to refuse the overwrite altogether. Building the `-i` habit is the best way to avoid accidental data loss.
Q: How do I rename a file that has spaces in its name? A: Quote the names: `mv “old file.txt” “new file.txt”`. Alternatively, escape each space with a backslash: `mv old\ file.txt new\ file.txt`.
Q: How do I rename hundreds of files at once? A: Use the Perl-based `rename` command with a substitution pattern — for example `rename ‘s/\.txt$/.bak/’ *.txt` — or a `for` loop in Bash. Always test `rename` with the `-n` dry-run flag first.
Q: Does mv work on directories? A: Yes. Renaming a directory is identical to renaming a file: `mv old-dir new-dir`. The directory’s contents are untouched; only the name changes.
Wrapping up
Renaming files in Linux comes down to one core idea: `mv` is your rename tool, and it pulls double duty as both move and rename. Remember the safety habit — `mv -i` so it prompts before clobbering — because that silent overwrite is the one thing that catches people out. For big jobs, reach for `rename` with a Perl pattern (dry-run with `-n` first) or a tidy `for` loop. Now go rename something, run `ls`, and watch it work.