How to Rename a File in Linux: CLI Tools & Practical Examples

How to Rename a File in Linux

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

Renaming files in Linux is an important task, whether you are managing a handful of documents or organizing multiple log files. While the desktop environment offers graphical tools for a simple meaning, the real deal is in the command line. With tools like the Linux mv command, rename, mmv, and custom bash scripts, Linux provides versatile options to rename a single or multiple files. 

In this guide, we will learn about some of the most important methods to rename a file in Linux! 

Using the mv Command to Rename a File in Linux

The Linux mv command is one of the most basic ways to rename a file in Linux. Although it is designed to move the files, it can also rename them by specifying a new filename as the destination. 

Syntax:

mv old_filename new_filename

Example:

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

mv report.txt final_report.txt

You can also use this command to move and rename the file at the same time: 

mv ~/Downloads/file.txt ~/Documents/renamed_file.txt

Renaming Multiple Files with rename

The rename command helps you with batch renaming as well. It allows you to perform the string substitutions on filenames using Perl expressions. 

Syntax:

rename ‘s/old/new/’ *.txt

Example:
To rename all .txt files to .bak:

rename ‘s/.txt$/.bak/’ *.txt

Using mmv for Advanced Bulk Renaming

The mmv command (mass move and rename) is one of the more flexible tools that uses variable patterns to rename or move multiple files at once. 

Syntax:

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

mmv ‘*.old’ ‘#1.new’

Example:
To rename all .log files to .bak:

mmv ‘*.log’ ‘#1.bak’

To install mmv:

sudo apt install mmv 

Related Article: Mastering Linux Commands: A Comprehensive Guide with Real-World Applications

Rename a File in Linux with find and exec

The find command is also extremely useful for when you need to find and rename files across directories that are based on specific criteria like the name, type, or modification. 

Example: Rename all .log files to .bak recursively:

find . -type f -name “*.log” -exec bash -c ‘mv “$0” “${0%.log}.bak”‘ {} \;

This command:

  • Searches for recursive in all the files (-type f) ending in .log
  • Uses exec to call mv with a modified filename, removing .log and adding .bak

You can customize it further with filters like -mtime, -size, or -name.

Using Bash Loops to Batch Rename Files

The bash scripting gives you the right flexibility to create loops for renaming files, which is pretty useful for custom naming schemes or transformations. 

Example: Prefix all .jpg files with “image_”:

for file in *.jpg; do

  mv “$file” “image_$file”

done

Example: Replace spaces with underscores in filenames:

for file in *\ *; do

  mv “$file” “${file// /_}”

done

The complexity of these loops depends on your use case. 

Graphical Methods to Rename a File in Linux (GUI Tools)

If you prefer not to use the terminal to rename a file in Linux, several graphical file managers and tools support batch renaming:

  1. Nautilus (Files – GNOME)

From the Files interface, Select files → Right-click → Rename. It supports numbering, search-and-replace, and case conversion.

  1. Thunar (Xfce File Manager)

Thunder includes a built-in “Bulk Rename” utility and supports regex, numbering, and metadata-based renaming.

  1. KRename (KDE)

KRename is a powerful GUI tool for advanced batch renaming and offers preview, undo, and file attribute support. 

Common Errors & How to Avoid Them

ErrorCauseHow to Avoid
mv: cannot statFile doesn’t exist or has a typo in the nameDouble-check filenames with ls or use tab-completion
Overwriting existing filesRenaming target already existsUse mv -i to prompt before overwrite or check manually
Permission deniedTrying to rename files you don’t ownUse sudo if appropriate, or change ownership with chown
Script renames unintended filesBroad pattern match in loops or findAlways test with echo or -print before running mv
Unexpected results in bulk renamingIncorrect syntax in loops or regexUse test files and run small batches to validate scripts

Best Practices To Safely Rename a File in Linux

Some of the best practices that you can follow to rename a file in Linux efficiently are: 

  • Review changes before renaming: Use echo or -print to preview the changes in files before you use the rename command. 
  • Test on copies: When you are scripting, test the changes on a copy of your file before executing on the primary file. 
  • Make backup copies: Use rsync or cp to make backups before applying changes in bulk. 
  • Dry-run for results: some tools such as rename or KRename offer dry-run methods to stimulate the renaming. 

Conclusion: Choosing the Right Renaming Method

To rename a file in Linux, you can use tools like mv command, rename command, mmv, or even bash loops that offer automation and flexibility. For a more advanced method, you can always rely on find with exec! 

Choosing the best possible method is highly dependent on your workflow! 

FAQs

What is the difference between mv and rename in Linux?

mv moves or renames a single file or directory, while rename is used for batch renaming based on patterns or expressions.

How do I rename files with spaces in their names?

Use quotes or escape spaces:
mv "old file.txt" "new file.txt"

What’s a safe way to test a bulk rename before applying it?

Use the -n or --no-act option with rename to preview changes without making them:
rename -n 's/old/new/' *.txt

Marium Fahim
Hi! I am Marium, and I am a full-time content marketer fueled by an iced coffee. I mainly write about tech, and I absolutely love doing opinion-based pieces. Hit me up at [email protected].
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!