How to Rename a File in the Linux Terminal
Renaming files is one of the most common tasks in any operating system — and in Linux, it becomes especially powerful when done via the terminal. From simple name changes to complex batch operations using patterns, renaming is an essential tool for:
- Organizing files by date, type, or project
- Automating cleanup of logs, backups, or temporary data
- Standardizing filenames for scripts, deployments, or APIs
- Preprocessing data in development, research, or data science environments
- Integrating into pipelines for CI/CD, image processing, or versioning
Whether you’re a developer renaming thousands of image files, a system administrator cleaning up rotated logs, or a DevOps engineer preparing artifacts for deployment — understanding how to rename files efficiently in the terminal will save time, reduce errors, and unlock automation at scale.
Basic File Renaming with mv
The mv (move) command is the standard way to rename files:
This works for:
- Renaming a file
- Moving a file to a new directory
- Both at the same time
Example:
Rename Multiple Files — Advanced Techniques
1. 🔁 Using rename (Perl-based)
Most powerful and flexible way.
Install (if missing):
Basic usage:
This renames all .txt files replacing “old” with “new” in the filenames.
Examples:
| Goal | Command |
|---|---|
| Add prefix to all .jpg files | rename ‘s/^/IMG_/’ *.jpg |
| Remove .bak from filenames | rename ‘s/\.bak$//’ *.bak |
| Change .JPG to .jpg | rename ‘s/\.JPG$/.jpg/i’ *.JPG |
| Replace spaces with underscores | rename ‘s/ /_/g’ * |
Using mmv
Another handy tool, though less flexible than “rename”.
Install:
Example usage:
Using find + mv + bash (for complex logic)
Example: Replace dashes with underscores for .txt files recursively.
This is safe, flexible, and works in nested directories.
Rename with for loops
Example: Add prefix to all .log files
You can customize with more bash scripting logic (like substring replacement, extensions, timestamps, etc.)
Best Practices
Always test before mass renaming:
-n is dry run — shows what would happen, but makes no changes.
Quote your variables to handle filenames with spaces or special characters
Use version control or backups before renaming thousands of files
