How to Use the ‘rename’ Command in Linux

Get hands-on with Linux's 'rename' command for efficient bulk file management.

The rename Linux command is designed to change the names of files and directories according to specified rules. It’s not just a simple renaming utility; it allows users to perform bulk renaming operations using regular expressions, making it an essential command for system administrators and developers alike.

Similar to the mv command, which is used to move or rename files, the rename command offers more flexibility and control. It’s commonly used by IT professionals, such as system administrators, programmers, and data analysts, to manage large sets of files efficiently.

How to Install the rename Command

The availability of the rename command may vary depending on the Linux distribution. Below, I’ll outline how to install and uninstall the rename command for some common distributions:

Debian-based systems (e.g., Ubuntu)

Install:

sudo apt-get update
sudo apt-get install rename

Uninstall:

sudo apt-get remove rename

RedHat-based systems (e.g., Fedora, CentOS)

Install:

sudo dnf install prename

Uninstall:

sudo dnf remove prename

Arch Linux

Install:

sudo pacman -S perl-rename

Uninstall:

sudo pacman -R perl-rename

openSUSE

Install:

sudo zypper install rename

Uninstall:

sudo zypper remove rename

How to Use rename

1. Replace a string in filenames

Syntax: rename 's/old/new/' *

Explanation: Replaces the string “old” with “new” in all filenames in the current directory.

Example: rename 's/test/demo/' *

Output:

testfile1.txt renamed as demofile1.txt
testfile2.txt renamed as demofile2.txt

All files in the current directory that had test in their name now have that replaced with demo.

2. Add a prefix to filenames

Syntax: rename 's/^/prefix_/' *

Explanation: Adds “prefix_” to the beginning of all filenames in the current directory.

Example: rename 's/^/sample_/' *

Output:

file1.txt renamed as sample_file1.txt
file2.txt renamed as sample_file2.txt

All files in the current directory now start with the prefix sample_.

3. Remove a file extension

Syntax: rename 's/\.ext$//' *

Explanation: Removes the “.ext” file extension from all matching filenames in the current directory.

Example: rename 's/\.txt$//' *

Output:

document.txt renamed as document
notes.txt renamed as notes

All .txt extensions from files in the current directory have been removed.

4. Convert filenames to lowercase

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

Explanation: Converts all uppercase letters in filenames to lowercase in the current directory.

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

Output:

FILE1.TXT renamed as file1.txt
FILE2.TXT renamed as file2.txt

All filenames in the current directory that had uppercase letters are now in lowercase.

5. Change file extension

Syntax: rename 's/\.oldext$/.newext/' *

Explanation: Changes the file extension from “.oldext” to “.newext” for all matching filenames in the current directory.

Example: rename 's/\.jpg$/.png/' *

Output:

image1.jpg renamed as image1.png
image2.jpg renamed as image2.png

All .jpg extensions from files in the current directory have been changed to .png.

6. Remove a suffix from filenames

Syntax: rename 's/suffix$//' *

Explanation: Removes “suffix” from the end of all filenames in the current directory.

Example: rename 's/_backup$//' *

Output:

file1_backup.txt renamed as file1.txt
file2_backup.txt renamed as file2.txt

All files in the current directory that ended with _backup now have that suffix removed.

7. Replace spaces with underscores in filenames

Syntax: rename 's/ /_/' *

Explanation: Replaces all spaces with underscores in filenames in the current directory.

Example: rename 's/ /_/' *

Output:

my file.txt renamed as my_file.txt
another file.txt renamed as another_file.txt

All filenames in the current directory that had spaces now have those spaces replaced with underscores.

8. Add a suffix to filenames without changing the extension

Syntax: rename 's/(\.\w+)$/_suffix$1/' *

Explanation: Adds “_suffix” before the file extension for all filenames in the current directory.

Example: rename 's/(\.\w+)$/_edited$1/' *

Output:

file1.txt renamed as file1_edited.txt
file2.jpg renamed as file2_edited.jpg

All files in the current directory now have _edited added before the file extension.

More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail