15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started
27.01.2026

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:

mv old_filename.txt new_filename.txt

This works for:

  • Renaming a file
  • Moving a file to a new directory
  • Both at the same time

Example:

mv file.txt ../archive/renamed_file.txt

Rename Multiple Files — Advanced Techniques

1. 🔁 Using rename (Perl-based)

Most powerful and flexible way.

Install (if missing):

sudo apt install rename # Debian/Ubuntu
sudo dnf install prename # RHEL/Fedora

Basic usage:

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

This renames all .txt files replacing “old” with “new” in the filenames.

Examples:

GoalCommand
Add prefix to all .jpg filesrename ‘s/^/IMG_/’ *.jpg
Remove .bak from filenamesrename ‘s/\.bak$//’ *.bak
Change .JPG to .jpgrename ‘s/\.JPG$/.jpg/i’ *.JPG
Replace spaces with underscoresrename ‘s/ /_/g’ *

Using mmv

Another handy tool, though less flexible than “rename”.

Install:

sudo apt install mmv

Example usage:

mmv "*.jpg" "photo_#1.jpg"

Using find + mv + bash (for complex logic)

Example: Replace dashes with underscores for .txt files recursively.

find . -type f -name "*.txt" | while read file; do
new=$(echo "$file" | sed 's/-/_/g')
mv "$file" "$new"
done

This is safe, flexible, and works in nested directories.

Rename with for loops

Example: Add prefix to all .log files

for f in *.log; do
mv "$f" "archived_$f"
done

You can customize with more bash scripting logic (like substring replacement, extensions, timestamps, etc.)

Best Practices

  • Always test before mass renaming:

    rename -n 's/ /_/g' *

    -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

15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started