How to Rename Files on Linux Using the Rename Command

September 27, 2023 / Command Line

In this article, we will explain how to rename files on Linux using the Rename Command.

The rename command provides greater file modification flexibility and it is commonly included in default Linux configurations. If your system lacks it, install it using Terminal.

  1. Use the following command for Debian, Ubuntu, Linux Mint, and their derivatives.
    sudo apt install rename
  2. Use this command if you are using CentOS 7 or RHEL-
    sudo yum install rename
  3. Run this command, in Arch Linux.
    yay perl-rename ## or yaourt -S perl-rename
  4. Once installation is complete, you can use the rename command. Here’s its fundamental syntax:
    rename 's/old-name/new-name/' files
  5. As an illustration, we’ll generate a new “filetorename” folder using the mkdir command. Subsequently, we will navigate to the directory and employ the touch command to craft five files inside it. Below are the commands:
    mkdir filetorename
    
    cd filetorename
    
    touch file{1..5}.txt
    
    ls
  6. The ls command will display the contents of the current directory, which includes the five new files. To rename a single file named file1.txt, the command would appear as follows:
    rename ‘s/file1/newfile1/’ file1.txt
  7. To alter the extensions of all files, for instance, to PHP, employ the following commands:
    rename ‘s/.txt/.php/’ *.txt
    
    ls
  8. If the file you wish to rename is located in a different directory, include its path in your command. The command might appear as follows:
    rename ‘s/.txt/.php/’ FILE/PATH

By following these steps, you can rename files on Linux using the rename command.

Spread the love