How to Rename Files in Linux Using mv & rename Commands

How to Rename Files in Linux Using mv & rename Commands

Introduction

Renaming files in Linux is a common task, whether you are organizing personal documents, editing configuration files, or managing system data. Unlike graphical interfaces where you right-click and rename, Linux provides powerful commands that let you rename files quickly and in bulk.

Two of the most widely used tools for this are:

  • The mv command (short for move), which is simple and ideal for renaming individual files or directories.

  • The rename command, which is more advanced and supports batch renaming using patterns and regular expressions.

This guide will explain how to rename files in Linux using mv & rename commands, with detailed examples, tips, and best practices.

Why Renaming Files in Linux Matters

Before learning the commands, let’s understand why renaming is essential:

  • Organization: Helps keep directories clean and structured.

  • Automation: Useful in scripts and batch jobs.

  • Clarity: Renaming adds meaning to files, making them easier to identify.

  • Consistency: Ensures files follow a naming convention for collaboration or deployment.

Method 1: Rename Files in Linux Using the mv Command

The mv command is primarily used to move files, but it also serves as a simple rename tool.

Basic Syntax

mv old_filename new_filename
  • old_filename → the current name of the file.

  • new_filename → the name you want to give.

Example 1: Rename a Single File

mv report.txt final_report.txt

This renames report.txt to final_report.txt.

Example 2: Rename and Move File Together

mv notes.txt /home/user/Documents/meeting_notes.txt

Here, notes.txt is renamed and moved to another directory.

Example 3: Rename a Directory

mv old_folder new_folder

This changes the folder name from old_folder to new_folder.

When to Use mv

  • Best for single file or directory renaming.

  • Simple and doesn’t require additional packages.

Method 2: Rename Files in Linux Using the rename Command

The rename command is more powerful than mv. It lets you rename multiple files at once using patterns and regular expressions.

Check if rename is Installed

Not all Linux distributions have rename preinstalled. To install:

  • On Debian/Ubuntu:

    sudo apt install rename
  • On CentOS/RHEL/Fedora:

    sudo yum install prename

Basic Syntax

rename 's/old_pattern/new_pattern/' files
  • 's/old_pattern/new_pattern/' → substitution rule using Perl-style regex.

  • files → the list of files you want to rename.

Example 1: Change File Extensions

rename 's/.txt/.md/' *.txt

This converts all .txt files in the directory to .md.

Example 2: Add Prefix to Files

rename 's/^/project_/' *.csv

This renames files like data.csvproject_data.csv.

Example 3: Remove Spaces from Filenames

rename 's/ //g' *.jpg

This removes spaces from all .jpg filenames. For example, my photo.jpgmyphoto.jpg.

Example 4: Change Uppercase to Lowercase

rename 'y/A-Z/a-z/' *

This converts all uppercase letters in filenames to lowercase.

Difference Between mv and rename

Feature mv Command rename Command
Scope Single file or directory Multiple files at once
Complexity Simple and straightforward Requires regex/pattern knowledge
Preinstalled Yes, by default May need to install separately
Use Case Quick renames Batch renaming or complex patterns

Advanced Tips for File Renaming

  1. Use Tab Completion: Avoid typing long filenames by pressing Tab.

  2. Dry Run with Rename: Test your pattern before execution.

    rename -n 's/.txt/.bak/' *.txt

    The -n option shows what would happen without making changes.

  3. Recursive Rename: Combine find with rename to process files in subdirectories.

    find . -type f -name "*.jpg" -exec rename 's/.jpg/.png/' {} +
  4. Backup Important Files: Always keep a backup before running bulk rename commands.

Common Mistakes and How to Avoid Them

  • Mistake 1: Using mv for bulk renaming → This is inefficient. Use rename.

  • Mistake 2: Forgetting quotes in rename patterns → Always wrap regex in ' '.

  • Mistake 3: Overwriting files accidentally → Use mv -i to confirm before overwriting.

  • Mistake 4: Not testing rename → Use rename -n before applying.

Practical Use Cases

  • Developers: Renaming log files (error.logerror_2025.log).

  • Photographers: Removing spaces or adding timestamps to images.

  • Students: Changing assignment file names to a consistent format.

  • System Admins: Bulk-renaming configuration or backup files.

FAQs About Renaming Files in Linux

1. Can I rename hidden files with mv or rename?

Yes. Just include the dot (.) prefix when renaming, e.g., mv .config .config_backup.

2. Is there a way to undo a rename?

Linux doesn’t have an undo option. Always test with rename -n or keep backups.

3. What’s the difference between rename and mmv?

mmv is another tool for batch renaming/moving. It uses simpler wildcards but is less common than rename.

4. Can I rename files by date or time automatically?

Yes, but you’ll need scripting (e.g., bash or Python) to append timestamps.

5. Does rename work the same on all Linux distributions?

No. Some use Perl-based rename, others use a simpler version. Check with man rename on your system.

Conclusion

Learning how to rename files in Linux using mv & rename commands makes file management faster and more efficient.

  • Use mv for quick renames of single files or directories.

  • Use rename for advanced batch operations with patterns and regular expressions.

By practicing these commands and using the tips shared in this guide, you’ll be able to rename files confidently—whether it’s one file or thousands at once.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top