How to use the Rename Command to rename files on Linux

October 25, 2023 / How-to Guide

Learn how to use the Rename Command to rename files on Linux in this guide. The command offers enhanced file modification capabilities and is typically included in default Linux setups. If your system does not have it, you can install it via the Terminal.

Steps-

  1. For Debian, Ubuntu, Linux Mint, and their derivatives, use the below command-
    sudo apt install rename
  2. If you are using CentOS 7 or RHEL, execute the below command-
    sudo yum install rename
  3. For Arch Linux, use the following-
    yay perl-rename ## or yaourt -S perl-rename
  4. As soon as the installation is done, “rename” command can be used. Use the below syntax-
    rename ‘s/old-name/new-name/’ files
  5. For instance, we will create a new directory called “filetorename” using the mkdir command. Then, we will move to the directory and use the touch command to generate five files within it. The commands are outlined below-
    mkdir filetorename
    cd filetorename
    touch file{1..5}.txt
    ls
  6. To view the contents of the current directory, including the five newly created files, use the ls command. If you want to rename a single file named fileA.txt, use the following command-
    rename ‘s/fileA/newfileA/’ fileA.txt
  7. To change the extensions of all files, for example, to PHP, use the following commands-
    rename ‘s/.txt/.php/’ *.txt
    ls
  8. If the file you intend to rename is situated in a separate directory, ensure to include its path in the command. The command could look something like this-
    rename ‘s/.txt/.php/’ FILE/PATH

In this manner, you can rename files on Linux with rename command. For the latest information, do visit our KB section.

 

Dominos Search