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
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)
sudo apt-get update sudo apt-get install rename
sudo apt-get remove rename
RedHat-based systems (e.g., Fedora, CentOS)
sudo dnf install prename
sudo dnf remove prename
sudo pacman -S perl-rename
sudo pacman -R perl-rename
sudo zypper install rename
sudo zypper remove rename
How to Use
1. Replace a string in filenames
rename 's/old/new/' *
Explanation: Replaces the string “old” with “new” in all filenames in the current directory.
rename 's/test/demo/' *
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
2. Add a prefix to filenames
rename 's/^/prefix_/' *
Explanation: Adds “prefix_” to the beginning of all filenames in the current directory.
rename 's/^/sample_/' *
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
3. Remove a file extension
rename 's/\.ext$//' *
Explanation: Removes the “.ext” file extension from all matching filenames in the current directory.
rename 's/\.txt$//' *
document.txt renamed as document notes.txt renamed as notes
.txt extensions from files in the current directory have been removed.
4. Convert filenames to lowercase
rename 'y/A-Z/a-z/' *
Explanation: Converts all uppercase letters in filenames to lowercase in the current directory.
rename 'y/A-Z/a-z/' *
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
rename 's/\.oldext$/.newext/' *
Explanation: Changes the file extension from “.oldext” to “.newext” for all matching filenames in the current directory.
rename 's/\.jpg$/.png/' *
image1.jpg renamed as image1.png image2.jpg renamed as image2.png
.jpg extensions from files in the current directory have been changed to
6. Remove a suffix from filenames
rename 's/suffix$//' *
Explanation: Removes “suffix” from the end of all filenames in the current directory.
rename 's/_backup$//' *
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
rename 's/ /_/' *
Explanation: Replaces all spaces with underscores in filenames in the current directory.
rename 's/ /_/' *
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
rename 's/(\.\w+)$/_suffix$1/' *
Explanation: Adds “_suffix” before the file extension for all filenames in the current directory.
rename 's/(\.\w+)$/_edited$1/' *
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:
|File System Operations||
|Search and Text Processing||
|System Information and Management||
|User and Session Management||