Welcome to the ultimate guide on renaming files in Linux! Navigating the intricacies of file renaming is a crucial skill, and this article is your comprehensive companion. You want to rename a file in Linux? Use mv. That's it — there's no dedicated "rename" command for a single file because the move command handles it: renaming is really just changing a file's path. From fundamental commands like mv to specialized techniques for batch renaming, handling special characters, and undoing operations, we'll explore everything you need to know.

mv oldname.txt newname.txt

The first argument is the source (the file that exists now). The second is the destination (what you want it called). Run this from inside the file's directory, or use full paths if you're somewhere else.

Before renaming anything important, confirm the destination name doesn't already exist — or use mv -i / mv -n. If a file with the new name is already there, mv can quietly overwrite it.
Stylized terminal flow showing draft.txt renamed to report.txt with the Linux mv command.

📂 Understanding the Linux File System

When it comes to managing files in Linux, understanding the file system is crucial. Linux organizes files in a hierarchical structure, with the root directory at the top. Directories contain files or other directories, creating a tree-like structure. Each file or directory is located within a specific path. In Linux, file names are case-sensitive, meaning "file.txt" and "File.txt" are considered two different entities — this trips up people coming from Windows all the time.

If you're managing multiple files on a Linux VPS, understanding how to rename them efficiently is crucial. Learn more about optimizing your Linux VPS setup in our guide on The Best Linux VPS.

📋 How File Renaming Works in Linux

When you run mv, you're changing the file's pathname — not rewriting its contents. The bytes stay put. Only the name changes. That's why the same command handles both moving and renaming; from the system's point of view, they're the same operation.

Source and Destination Paths

You can use relative or absolute paths:

mv draft.txt report.txt
mv /home/user/draft.txt /home/user/report.txt

The source and destination must be distinct. And remember — the directory needs write permission for the rename to succeed.

🔄 How to Use Rename File in Linux

How to Use Rename File in Linux command line interface

Renaming files in Linux is a straightforward process, and the command-line interface provides powerful tools for this task. The mv command, short for "move," is also used for renaming files.

mv Command to Rename File in Linux

The mv command is the go-to tool. Its basic syntax:

mv oldfilename.txt newfilename.txt

mv command example renaming a file in Linux terminal

This simple command effectively changes the name of the file. The mv command can also be used to move files between directories.

Linux terminal showing successful file rename operation

If you need to locate files before renaming them, check out our guide on how to find a file in Linux. It provides helpful tips for efficiently searching your system.

Command Purpose Example
mv Rename or move a single file or directory mv oldfile.txt newfile.txt
rename Batch rename multiple files using patterns rename 's/old/new/' *.txt

How to Rename a File in Linux in the Same Directory

Renaming a file in the same directory using Linux commands

  1. Open the terminal.
  2. Navigate to the directory containing the file using cd.
  3. Use mv followed by the current file name and the desired new name.
  4. Press Enter, and the file will be renamed.

How to Copy and Rename File in Linux

Renaming a file while copying it to another directory can be done with the cp command:

cp oldfilename.txt /path/to/destination/newfilename.txt

Rename File in Linux CentOS

If you are using CentOS, the process of renaming files remains the same as in other Linux distributions. The mv command is universally available, ensuring consistency in file management across different flavors of Linux. For CentOS 7 users, you can confidently use the mv command to rename files. If you're running CentOS on a CentOS server, these file management commands work exactly the same way with full root access.

🛡️ Rename Files Safely Without Overwriting

This is where people get burned. Plain mv old new will replace an existing destination without a word of warning.

Command Behavior
mv old new Renames or moves; may replace an existing destination
mv -i old new Prompts before replacing anything
mv -n old new Refuses to overwrite an existing destination
mv --backup=numbered old new Keeps a numbered backup of the replaced file

Handle Filenames Beginning with a Hyphen

mv -- -oldname.txt newname.txt
mv ./-oldname.txt newname.txt

📝 Rename Files with Spaces and Special Characters

Spaces are the classic gotcha. Without quotes, the shell splits the name into separate arguments:

mv 'old report.txt' 'final report.txt'

📦 Rename Multiple Files in Linux

Batch renaming usually means the rename tool. But there are two completely different programs named rename, and they don't share syntax.

Which rename Command Is Installed?

rename --version
man rename
Side-by-side table comparing Perl rename and util-linux rename syntax, regex, dry run, and distributions.
Feature Perl rename util-linux rename
Syntax rename 's/old/new/' *.txt rename old new *.txt
Uses regex Yes (Perl expressions) No (literal string swap)
Dry run Often -n Not available
Common on Debian, Ubuntu Fedora, CentOS, Arch

Batch Rename with Perl rename

rename -n 's/oldpart/newpart/' -- *.txt   # preview
rename 's/oldpart/newpart/' -- *.txt      # execute

Batch Rename with util-linux rename

rename oldpart newpart *.txt

This variant is common on Fedora, CentOS, Arch, and RHEL-family distributions. If you're running a Rocky Linux VPS or AlmaLinux VPS, this is the rename version you'll typically have installed by default.

Rename Multiple Files with a Bash Loop

for file in *.jpeg; do
  [ -e "$file" ] || continue
  mv -i -- "$file" "${file%.jpeg}.jpg"
done

Common Batch-Renaming Examples

  • Change extensions: rename -n 's/\.jpeg$/.jpg/' -- *.jpeg
  • Replace part of a name: rename -n 's/draft_/final_/' -- draft_*
  • Add a prefix: rename -n 's/^/archive_/' -- *.log
  • Convert to lowercase: rename -n 'y/A-Z/a-z/' -- *
Three-step Perl rename workflow showing version check, dry-run preview, and real batch execution.

Rename All File Extensions in a Folder

rename 's/.old/.new/' *.old

For those managing servers or large-scale file operations, Linux Dedicated Servers offer robust performance and control, making advanced tasks like batch file renaming even more efficient.

🐍 Rename File in Linux Using Python

Rename File in Linux Using Python script example

Sometimes a script is cleaner than a shell one-liner. If you're running Python scripts for automation or security testing on a Kali Linux VPS, the same file-renaming logic applies:

import os
old_filename = "oldfile.txt"
new_filename = "newfile.txt"
os.rename(old_filename, new_filename)

Modern Python uses pathlib:

from pathlib import Path
source = Path("oldfile.txt")
destination = Path("newfile.txt")
source.rename(destination)

📁 Rename a Directory in Linux

Same command — directories rename exactly like files:

mv old-directory new-directory

If new-directory already exists, mv doesn't rename — it moves old-directory inside it. Check first. To clean up afterward, you may need to remove directories in Linux or create and organize directories with mkdir.

🖥️ Rename a File in Ubuntu or Another Linux Desktop

Prefer a mouse? Every desktop file manager can do this:

  1. Open Files/Nautilus (GNOME), Dolphin (KDE), or your installed file manager.
  2. Click the file to select it.
  3. Press F2, or right-click and choose Rename.
  4. Type the new name and confirm.
Stylised Ubuntu file manager with draft.txt selected and the Rename option highlighted beside F2.

↩️ Can You Undo a File Rename?

Linux has no universal "undo" command for mv or rename. Once it's done, it's done.

Renaming a file does not send anything to Trash. Here's what actually works:

Reverse a Known Rename

mv newname.txt oldname.txt

Restore an Overwritten File

If your rename overwrote an existing file, that data is gone unless you have a backup, a filesystem snapshot, or version control. In some cases specialized recover deleted Linux files tooling can help — but recovery is never guaranteed.

Why Command History Isn't Undo

Shell history shows you what you ran. It doesn't reverse it. History is a record, not a rewind button.

⚠️ Common Errors and Troubleshooting

Error or Symptom Likely Cause Fix
No such file or directory Wrong path, case mismatch, or quoting issue Check pwd and ls -la; quote the name
Permission denied No write access to the parent directory Check Linux file and directory permissions before reaching for sudo
Destination became a nested file Destination is an existing directory Use the full intended destination path
Batch rename changed nothing Pattern didn't match or wrong rename variant Preview the glob; check rename --version
Filename starts with - Treated as an option Use -- or prefix with ./
Existing file was replaced Destination already existed Use -i, -n, or backups

To perform certain file operations that require elevated permissions, understanding how to use sudo in Linux is essential. But sudo is not the default fix for "Permission denied" — understand why you lack access before elevating.

📋 Linux File-Renaming Cheat Sheet

Task Command
Rename one file mv old.txt new.txt
Rename a directory mv old-dir new-dir
Prompt before overwrite mv -i old new
Never overwrite mv -n old new
Rename with spaces mv 'a b.txt' 'c d.txt'
Leading hyphen mv -- -old.txt new.txt
Batch (Perl rename, preview) rename -n 's/old/new/' -- *.txt
Change extensions in a loop for f in *.jpeg; do mv -i -- "$f" "${f%.jpeg}.jpg"; done

New to the terminal generally? Brush up on the essential Linux terminal commands and the create an empty practice file with touch guide.

🏁 Final Words

In conclusion, mastering the art of file renaming in Linux opens the door to efficient and organized file management. Renaming files comes down to a few reliable moves: use mv for a single file or directory, reach for rename or a Bash loop when handling many files at once. Protect yourself from silent overwrites with -i, -n, or backups. Preview batch operations before running them. Quote filenames with spaces, and use -- for names starting with a hyphen.

For those looking to host their Linux projects online, consider buying Linux VPS hosting to get the resources and flexibility to manage your applications efficiently.

People also read: