Skip to main content
99.99% Uptime SLA Network status
How to Rename Files in Linux - Virtarix Blog

How to Rename Files in Linux

June 6, 2026 · Blog / Technical Guide

If you want to know how to rename file in Linux, use mv old-name new-name. The same mv command also renames directories. The key is understanding that mv can either rename one path or move a path into another directory, depending on the destination you give it.

That difference matters on a VPS because a small typo can move a config file, release folder, upload directory, or script into the wrong location. This tutorial shows the safe patterns first, then explains when batch rename tools are appropriate.

Key takeaways

  • Use mv old-name new-name to rename one file.
  • Use mv project-old project-new to rename one directory.
  • If the destination is an existing directory, mv moves the source into that directory instead of renaming it to that exact path.
  • Use mv -i when you want a prompt before overwriting a destination.
  • Batch rename commands vary by distribution, so confirm which implementation is installed before using examples from another server.
  • For related command-line safety habits, read the Virtarix guides to Linux command line habits, Linux command syntax differences, and securing a VPS.

Safe rename steps

Step 1: Rename one file with mv

Use mv with the current file name first and the new file name second.

mv report-old.txt report-new.txt

After the command succeeds, the old path is gone and the new path exists. This is a rename when both paths are in the same directory and the destination is not an existing directory.

Read the command before running it: source first, destination second. Reversing those arguments is a common mistake.

Step 2: Rename one directory with mv

Directories use the same pattern.

mv project-old project-new

This renames project-old to project-new when project-new does not already exist as a directory. If project-new already exists, the behavior can be different: mv may move the source directory inside the destination directory.

That is why you should check whether the destination path already exists before renaming production folders.

Step 3: Move a file into another directory

If the last argument is an existing directory, mv moves the file into that directory using the original file name.

mv notes.txt archive/

This is not the same as renaming notes.txt to a file literally called archive. The trailing slash makes the intent easier to read, but the important point is that the destination is a directory.

Use this when you are organizing logs, deployment artifacts, backups, or old configuration copies. Do not use it when your real goal is to change the file name.

Step 4: Add overwrite protection

If you are not sure whether the destination exists, use interactive mode.

mv -i report-old.txt report-new.txt

The -i option prompts before overwriting. On a server, this can prevent a quick rename from replacing a file you still need.

For scripts, prefer an explicit check before the mv command instead of relying on an interactive prompt. Scripts should be deterministic and should fail clearly when a destination already exists, especially during unattended deploys, maintenance windows, and rollback tests.

What about the rename command?

Some Linux systems provide a rename command for batch renaming many files. On Ubuntu, the Perl-style rename documented by the manpage uses a Perl expression.

That makes it powerful, but also easy to misuse. A command copied from another distribution may refer to a different rename implementation. Before running a batch rename on a VPS, confirm which command is installed, test against throwaway files, and review the output.

For most one-file and one-directory tasks, mv is clearer and safer.

Handling spaces and special characters

Quote paths that contain spaces, parentheses, or shell-special characters.

mv "old report.txt" "new report.txt"

Quoting tells the shell to treat the spaced name as one argument. Without quotes, the shell splits the path and mv receives the wrong number of arguments.

Tab completion also helps. It lets the shell complete the actual path and often adds the right escaping automatically.

Troubleshooting rename mistakes

The file moved into a directory instead of being renamed

The destination probably already existed as a directory. Look inside that directory for the moved file or folder.

The command overwrote a file

If you used plain mv, the destination may have been replaced. Restore from backup if needed, then use mv -i or explicit existence checks for future changes.

Permission denied

You may not own the file or may not have write permission on the parent directory. Do not add elevated privileges blindly. Confirm why the path is protected first.

The rename command does not match examples online

You may have a different rename implementation. Check the local manpage before running a batch rename, especially on production servers.

VPS-safe rename checklist

Before renaming production files or directories, ask:

  1. Am I in the correct directory?
  2. Does the destination already exist?
  3. Will this affect a running service?
  4. Do I need a backup or rollback point?
  5. Am I renaming one path, or moving it into a directory?

If the path belongs to a web app, database, or deployment process, pause and check the service owner first. A rename can be just as disruptive as a delete when an application expects the old path.

If you want a disposable Linux server for command-line practice, Virtarix Cloud VPS plans give you an isolated environment where you can test file operations, deployment folders, and rollback habits before touching production.

Post-rename verification checklist

After a rename, verify the result from the application’s point of view, not only from the shell prompt. If you renamed a static asset, load the page that references it. If you renamed a deployment directory, confirm the service still points to the correct release. If you renamed a script, check the cron job, systemd unit, or documentation that calls it.

This is where small command-line changes become operationally safe. The rename may have succeeded, but another process may still depend on the old path. For production VPS work, update references and leave a short note in the change log so the next admin knows why the name changed. If the renamed path is part of a deployment, also check rollback scripts and backup exclusions. Those tools and notes often contain hard-coded paths that are easy to forget during a quick shell cleanup. A rename is safe only when both the filesystem and the dependent workflow agree on the new name.

FAQ

How do I rename a file in Linux?

Use mv old-name new-name. The first path is the current name; the second path is the new name.

How do I rename a directory in Linux?

Use the same mv pattern: mv old-directory new-directory. First confirm that the destination directory does not already exist.

Is mv the same as rename?

No. mv is the standard command for moving and renaming paths. rename is a batch rename tool, and its syntax can vary by implementation.

Summary

The safest way to rename files in Linux is to use mv deliberately: source first, destination second, and check whether the destination already exists. For one file or one directory, mv is usually the clearest command.

Batch rename tools can help when many names need the same transformation, but they should be tested on disposable files first. On a VPS, clarity is more important than clever one-liners. Prefer boring, readable commands when the path belongs to a customer site, deployment pipeline, or service configuration. This final discipline also protects rollback notes, monitoring alerts, and operator handovers.

Byline: Peter French — Updated 2026-05-18.

Peter French
About the Author Peter Frenchis the Managing Director at Virtarix, with over 17 years in the tech industry. He has co-founded a cloud storage business, led strategy at a global cloud computing leader, and driven market growth in cybersecurity and data protection.