How to Rename a File in the Linux Terminal: Complete Guide for Beginners and Pros
Renaming files is one of the most fundamental tasks in any operating system — and in Linux, it becomes exceptionally powerful when executed directly from the terminal. Whether you need to change a single filename or batch-rename thousands of files using complex patterns, mastering file renaming in Linux is an essential skill for developers, system administrators, and DevOps engineers alike.
From organizing project assets to automating log cleanup and preparing deployment artifacts, knowing the right tools and techniques will save you significant time, reduce human error, and unlock true automation at scale.
Why Rename Files from the Linux Terminal?
Before diving into commands, it's worth understanding why the terminal approach is preferred over graphical file managers for many use cases:
- Organizing files by date, type, project, or naming convention
- Automating cleanup of rotated logs, temporary data, or backup archives
- Standardizing filenames for scripts, deployments, or API integrations
- Preprocessing datasets in development, research, or data science pipelines
- Integrating into CI/CD workflows, image processing pipelines, or versioning systems
Whether you're a developer renaming thousands of image files, a sysadmin cleaning up rotated logs on a VPS Hosting server, or a DevOps engineer preparing build artifacts for deployment — the techniques below will make your workflow dramatically more efficient.
Method 1: Basic File Renaming with mv
The mv (move) command is the standard, built-in Linux utility for renaming files. It requires no additional installation and works on every Linux distribution.
Syntax
mv old_filename.txt new_filename.txtWhat mv Can Do
| Task | Command Example |
|---|---|
| Rename a file in place | mv report.txt final_report.txt |
| Move a file to another directory | mv report.txt /home/user/docs/ |
| Rename and move simultaneously | mv report.txt ../archive/final_report.txt |
Practical Example
mv file.txt ../archive/renamed_file.txtThis single command renames file.txt to renamed_file.txt and moves it to the ../archive/ directory in one step.
> Important: Unlike some operating systems, Linux does not warn you if the destination file already exists. The original file will be silently overwritten. Use the -i (interactive) flag to prompt for confirmation:
mv -i old_filename.txt new_filename.txtMethod 2: Batch Renaming with rename (Perl-Based)
For renaming multiple files at once, the Perl-based rename utility is the most powerful and flexible tool available. It uses Perl-compatible regular expressions (PCRE), giving you fine-grained control over complex renaming patterns.
Installation
# Debian / Ubuntu
sudo apt install rename
# RHEL / Fedora / CentOS
sudo dnf install prenameBasic Syntax
rename 's/old_pattern/new_pattern/' *.txtThis renames all .txt files in the current directory, replacing every occurrence of old_pattern with new_pattern in the filename.
Common rename Use Cases
| Goal | Command |
|---|---|
Add a prefix to all .jpg files | rename 's/^/IMG_/' *.jpg |
Remove .bak extension from filenames | rename 's/.bak$//' *.bak |
Convert .JPG to lowercase .jpg | rename 's/.JPG$/.jpg/i' *.JPG |
| Replace spaces with underscores | rename 's/ /_/g' * |
| Add a numeric suffix pattern | rename 's/file/file_v2/' *.txt |
Dry Run Before Executing
This is arguably the most important best practice when batch renaming. Use the -n flag to preview changes without actually modifying any files:
rename -n 's/ /_/g' *The output will show exactly what would be renamed, allowing you to verify the pattern is correct before committing to the operation.
Method 3: Batch Renaming with mmv
The mmv (mass move) utility provides a pattern-based approach to renaming multiple files using wildcard syntax rather than regular expressions. It's slightly less flexible than rename but easier to read for simpler tasks.
Installation
sudo apt install mmvExample Usage
mmv "*.jpg" "photo_#1.jpg"This renames every .jpg file in the current directory, prepending photo_ to each filename. The #1 placeholder captures the wildcard match from the source pattern.
Method 4: Recursive Renaming with find + mv + Bash
For complex renaming logic that spans nested directories, combining find, mv, and bash scripting gives you maximum control and flexibility.
Example: Replace Dashes with Underscores in All .txt Files (Recursively)
find . -type f -name "*.txt" | while read file; do
new=$(echo "$file" | sed 's/-/_/g')
mv "$file" "$new"
doneHow it works:
find . -type f -name "*.txt"— locates all.txtfiles starting from the current directory, including subdirectorieswhile read file— iterates over each resultsed 's/-/_/g'— generates the new filename by replacing all dashes with underscoresmv "$file" "$new"— performs the actual rename
This approach is safe, highly flexible, and works seamlessly across deeply nested directory structures — making it ideal for use on Dedicated Servers where you may be managing large, complex file hierarchies.
Method 5: Renaming Files with for Loops
For straightforward batch operations within a single directory, a simple for loop in bash is often the quickest solution.
Example: Add a Prefix to All .log Files
for f in *.log; do
mv "$f" "archived_$f"
doneExample: Add a Timestamp to All .txt Files
for f in *.txt; do
mv "$f" "$(date +%Y%m%d)_$f"
doneExample: Replace a Substring in Filenames
for f in *report*; do
mv "$f" "${f/report/summary}"
doneThe ${variable/pattern/replacement} syntax is a native bash parameter expansion feature — no external tools required.
Comparison: Which Tool Should You Use?
| Tool | Best For | Regex Support | Recursive | Requires Install |
|---|---|---|---|---|
mv | Single file renames | No | No | No (built-in) |
rename | Complex batch renames | Yes (PCRE) | No (use with find) | Yes |
mmv | Simple wildcard batch renames | No (wildcards) | Limited | Yes |
find + mv | Recursive, complex logic | Via sed/awk | Yes | No (built-in) |
for loop | Simple batch in one directory | Via bash expansion | No | No (built-in) |
Best Practices for Renaming Files in Linux
Following these guidelines will protect your data and prevent costly mistakes, especially when performing mass rename operations on production servers.
1. Always Do a Dry Run First
Before executing any batch rename, preview the changes:
rename -n 's/ /_/g' *The -n flag shows what *would* happen without making any actual changes. This is non-negotiable for large-scale operations.
2. Always Quote Your Variables
Filenames can contain spaces, special characters, or newlines. Always wrap variables in double quotes to prevent unexpected word splitting:
mv "$old_file" "$new_file"3. Back Up Before Bulk Operations
Before renaming thousands of files, create a backup or snapshot. If you're working on a managed hosting environment, check whether your provider offers automated snapshots. AlexHost's VPS with cPanel plans include easy backup management directly from the control panel.
4. Use Version Control for Code and Config Files
If you're renaming files that are tracked in a Git repository, use git mv instead of plain mv to preserve history:
git mv old_script.sh new_script.sh5. Test with a Small Subset First
When working with thousands of files, test your rename command on a small sample directory before running it on the full dataset:
mkdir test_rename && cp *.jpg test_rename/ && cd test_rename
rename -n 's/IMG_/PHOTO_/' *.jpg6. Handle Filenames with Newlines Carefully
When using find with pipes, filenames containing newlines can cause issues. Use -print0 and read -d '' for maximum safety:
find . -type f -name "*.txt" -print0 | while IFS= read -r -d '' file; do
new=$(echo "$file" | sed 's/-/_/g')
mv "$file" "$new"
doneReal-World Use Cases
Standardizing Log File Names on a Server
System administrators managing log rotation on a Linux server often need to rename archived logs to follow a consistent naming convention:
for f in /var/log/nginx/*.log.*; do
mv "$f" "${f/access/nginx_access}"
donePreparing Image Assets for a Web Application
Developers deploying a web application to Shared Web Hosting often need to normalize image filenames before upload:
# Convert all image filenames to lowercase and replace spaces with hyphens
rename 's/ /-/g' *.jpg *.png *.gif
rename 'y/A-Z/a-z/' *.jpg *.png *.gifBatch Renaming Files for a Domain Migration
When migrating a website to a new domain and restructuring content, you may need to update hundreds of configuration or template files. Combined with proper Domain Registration and DNS management, a clean file structure makes migrations far smoother:
find . -type f -name "*.conf" | while read file; do
new=$(echo "$file" | sed 's/olddomain/newdomain/g')
mv "$file" "$new"
doneFrequently Asked Questions
Can I undo a rename operation in Linux?
Linux does not have a native undo function for terminal commands. This is why dry runs and backups are critical. If you use Git, git mv operations can be reverted with git checkout.
What is the difference between mv and rename?
mv is a built-in Unix utility designed for moving and renaming individual files. rename is a Perl-based tool specifically designed for batch renaming using regular expressions. For single files, use mv. For bulk operations with patterns, use rename.
Does rename work the same on all Linux distributions?
There are two common versions of rename: the Perl-based version (common on Debian/Ubuntu) and a simpler C-based version (common on some RHEL-based systems). The Perl version supports full regular expressions; the C version uses simpler string substitution. Always verify which version is installed with rename --version.
How do I rename files with spaces in their names?
Always quote your filenames and variables:
mv "my old file.txt" "my_new_file.txt"Or use rename with a global substitution:
rename 's/ /_/g' *Conclusion
Renaming files in the Linux terminal is a skill that scales from simple one-off changes to fully automated batch operations across thousands of files. The key tools to master are:
mv— for quick, individual renamesrename— for powerful regex-based batch operationsfind+mv— for recursive, multi-directory workflowsforloops — for simple, readable batch scripts
Always preview changes with dry runs, quote your variables, and back up critical data before executing mass rename operations.
If you're managing files on a Linux server and looking for a reliable, high-performance hosting environment, explore AlexHost's range of solutions — from VPS Hosting and Dedicated Servers to fully managed VPS Control Panels — all optimized for Linux workloads.
on All Hosting Services