Site icon CentLinux

Best way to Rename File in Linux CLI

Share on Social Media

Learn how to rename file in Linux CLI using mv, rename, and mmv commands. Discover step-by-step methods for single and batch file renaming with examples. #centlinux #linux #unix



How to Rename a File in Linux CLI

Renaming files in Linux using the command-line interface (CLI) is a common task for users working in a terminal environment. Whether you need to rename a single file or multiple files at once, Linux provides several methods to accomplish this efficiently. In this guide, we’ll explore different ways to rename files in Linux using commands like mv, rename, and mmv.

Renaming files is a fundamental operation in Linux. Whether you are organizing files, changing naming conventions, or batch-renaming multiple files, Linux provides several powerful tools to achieve this. In this guide, we’ll cover different ways to rename files using various commands and techniques.

How to Rename File in Linux CLI

Using the mv Command (Basic File Renaming)

The simplest way to rename a file in Linux is by using the mv command. This command is primarily used to move files, but it can also rename them.

Syntax of mv

mv old_filename new_filename

Recommended Training: Linux Administration: The Complete Linux Bootcamp in 2025 from Andrei Dumitrescu, Crystal Mind Academy

Example of Renaming a File

Imagine you have a file named file1.txt in your current directory, and you need to rename it to file2.txt for better organization or clarity. You can achieve this easily using the mv command in the Linux terminal.

mv file1.txt file2.txt

After executing the command, the original file named file1.txt will no longer exist under its previous name. Instead, it will be successfully renamed to file2.txt, reflecting the updated filename in your directory. You can verify the change by using the ls command to list the files in the directory.

Read Also: Top 100 Linux Interview Questions for DevOps

Renaming a File with Spaces in the Name

When dealing with filenames that contain spaces, it’s essential to enclose them in double quotes to prevent errors in the command line. Without quotes, the system may interpret the spaces as separate arguments, leading to unintended results. To avoid this, always wrap the entire filename in quotes when using commands like mv, rename, or cp.

mv "old file.txt" "new file.txt"

Renaming a File Without Overwriting

To avoid accidentally overwriting an existing file with the same name, you can use the -i (interactive) option with the mv command. This option prompts you for confirmation before replacing any existing file, ensuring that important data is not lost. When enabled, the system will ask whether you want to overwrite the file, allowing you to decide on a case-by-case basis.

mv -i file1.txt file2.txt

If file2.txt already exists in the directory, using the -i option with the mv command will trigger a prompt asking for confirmation before proceeding with the renaming. This safeguard allows you to review the action and choose whether to overwrite the existing file or cancel the operation to prevent data loss.


Using the rename Command (Advanced File Renaming)

The rename command is a powerful tool for batch renaming multiple files simultaneously based on specific patterns. Unlike the mv command, which requires renaming files one by one, rename allows you to modify filenames in bulk using pattern matching and substitution.

It supports regular expressions, enabling complex transformations such as replacing text, changing file extensions, adding prefixes or suffixes, and even removing unwanted characters. This makes it an ideal choice for users who need to rename a large number of files efficiently.

Syntax of rename

rename 's/old_pattern/new_pattern/' files

Example of Renaming Multiple Files

Imagine you have numerous text files in a directory, all with the .txt extension, and you need to change their extensions to .log for consistency or organizational purposes. Instead of renaming each file individually, you can use the rename command to modify all filenames in bulk.

This method is particularly useful when working with large datasets, log files, or script-generated outputs, as it saves time and minimizes the risk of human error.

rename 's/.txt/.log/' *.txt

Executing this command will systematically rename all files with the .txt extension in the current directory, changing them to .log while preserving their original filenames. This means that a file named document.txt will become document.log, and notes.txt will be renamed to notes.log.

This batch renaming process is efficient and eliminates the need for manual renaming, making it especially useful when dealing with a large number of files.


Using mmv for Bulk Renaming

The mmv command is another powerful utility for renaming multiple files simultaneously, making it an excellent choice for batch file renaming in Linux. Unlike the rename command, which relies on regular expressions for substitutions, mmv provides a more flexible and intuitive approach to pattern-based renaming.

It allows users to define complex renaming rules, such as adding prefixes or suffixes, changing extensions, and even rearranging parts of filenames based on patterns. This makes mmv particularly useful for organizing large sets of files efficiently while reducing the chances of errors in manual renaming.

Installing mmv

sudo apt install mmv  # Debian/Ubuntu  
sudo yum install mmv  # CentOS/RHEL  

Example of Batch Renaming

If you have multiple image files in a directory with the .jpg extension and need to convert their filenames to .png, you can use a batch renaming command to automate the process.

Instead of manually renaming each file one by one, which can be time-consuming and prone to mistakes, you can execute a single command that efficiently changes all .jpg extensions to .png. This approach is especially helpful when dealing with large collections of image files, such as those used in web development, graphic design, or photography.

mmv "*.jpg" "#1.png"

This command effectively renames all files with the .jpg extension in the current directory, changing them to .png while preserving the original filenames. For example, if you have a file named photo1.jpg, it will be renamed to photo1.png, and image_sample.jpg will become image_sample.png.

The core filename remains unchanged, ensuring that file organization remains intact. This method is particularly useful when working with large batches of image files that require format standardization, such as preparing assets for web use or image processing tasks.


Renaming Files with Wildcards

Wildcards, such as * (asterisk) and ? (question mark), are powerful tools in Linux that can be used to rename multiple files at once without manually specifying each filename. The * wildcard represents any number of characters, making it useful for selecting groups of files with similar naming patterns.

For example, *.txt targets all text files in a directory. The ? wildcard, on the other hand, represents a single character, which is useful for renaming files with slight variations in their names. By leveraging wildcards with commands like mv, rename, or mmv, you can efficiently rename large sets of files in a structured and automated manner.

Using mv with Wildcards

If you need to rename multiple .txt files in a directory by adding a prefix to their filenames, you can achieve this efficiently using command-line tools. Instead of renaming each file manually, you can use a loop or batch renaming command to automate the process. Adding a prefix is useful for categorization, organizing project files, or distinguishing between different versions of documents.

For instance, if you have files named report.txt, notes.txt, and summary.txt, and you want to add the prefix “backup_”, they will be renamed as backup_report.txt, backup_notes.txt, and backup_summary.txt. This method saves time and ensures consistency across multiple files.

for file in *.txt; do mv "$file" "new_$file"; done

Using rename with Wildcards

If you have files with spaces in their names, it’s a good practice to replace those spaces with underscores or hyphens to improve compatibility with scripts and command-line operations. Spaces in filenames can cause issues when working with Linux commands, as they are often interpreted as separate arguments.

To avoid potential errors and make file handling more efficient, you can use the rename command or a simple loop in Bash to remove spaces from all filenames in a directory. This is particularly useful for organizing files, preparing them for web use, or ensuring seamless integration with automated scripts and software applications.

rename 's/ //g' *.txt

6. Using Bash Scripting for File Renaming

If you find yourself repeatedly renaming multiple files, writing a Bash script can help automate the process and save time. Instead of manually executing renaming commands each time, a script allows you to define a set of rules and apply them consistently across multiple files. This is especially useful for bulk renaming tasks, such as changing file extensions, adding prefixes or suffixes, removing spaces, or numbering files sequentially.

By using loops and pattern matching within the script, you can rename large batches of files with minimal effort while reducing the risk of human errors. Automating renaming with a Bash script is ideal for system administrators, developers, and anyone who frequently manages large collections of files.

Writing a Simple Script

#!/bin/bash
for file in *.txt; do
  mv "$file" "${file%.txt}.bak"
done

This script renames all .txt files to .bak.

Running the Script

Save it as rename.sh, then run:

chmod +x rename.sh  
./rename.sh

Renaming Files Based on Patterns

Changing Extensions of Multiple Files

rename 's/.jpeg/.jpg/' *.jpeg

Removing Spaces from Filenames

rename 's/ /_/g' *

Renaming Multiple Files with a For Loop

Using a for loop, you can rename files sequentially.

i=1
for file in *.jpg; do
  mv "$file" "image_$i.jpg"
  ((i++))
done

This renames all .jpg files to image_1.jpg, image_2.jpg, etc.


Renaming Files with Regular Expressions

Example: Replacing Digits in Filenames

rename 's/[0-9]+/_/' *

This replaces all numbers in filenames with an underscore.


Handling Errors and Avoiding Overwriting


Using GUI-Based Methods for Renaming

If you prefer a GUI, you can rename files using:


Comparison of Different Methods

MethodProsCons
mvSimple and fastLimited for bulk renaming
renamePowerful with regexSlightly complex
mmvFlexible bulk renamingRequires installation

Best Practices for File Renaming in Linux


Conclusion

Renaming files in Linux can be done using various methods, from simple mv commands to advanced bulk renaming with rename or mmv. Understanding these tools will help you efficiently manage files in a Linux environment.

Need expert AWS and Linux system administration? From cloud architecture to server optimization, I provide reliable and efficient solutions tailored to your needs. Hire me on Fiverr today!


FAQs

What is the easiest way to rename a file in Linux?
Using mv: mv old_name new_name.

How can I rename multiple files at once?
Use rename or a loop with mv.

Can I rename files using a GUI?
Yes, using file managers like Nautilus or Dolphin.

What if I accidentally overwrite a file?
Use mv -i to prevent accidental overwrites.

How do I rename files without spaces?
Use rename 's/ /_/g' * to replace spaces with underscores.


Exit mobile version