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-nameto rename one file. - Use
mv project-old project-newto rename one directory. - If the destination is an existing directory,
mvmoves the source into that directory instead of renaming it to that exact path. - Use
mv -iwhen you want a prompt before overwriting a destination. - Batch
renamecommands 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.