Overslaan naar inhoud
  • +31 653-919-302
Cafayate.Net
  • 0
  • 0
  • Aanmelden
  • Nederlands English (US) Español (AR)
  • Contact
  • Startpagina
  • Blog
  • Vacatures
  • Contact
Cafayate.Net
  • 0
  • 0
    • Startpagina
    • Blog
    • Vacatures
    • Contact
  • +31 653-919-302
  • Nederlands English (US) Español (AR)
  • Aanmelden
  • Contact

Rename – A Command Line Tool For Renaming Multiple Files in Linux

  • Alle blogs
  • Tech Blog
  • Rename – A Command Line Tool For Renaming Multiple Files in Linux
  • 5 maart 2021 in
    Administrator

    We often use “mv” command to rename a single file in Linux. However, renaming multiple or group of files quickly makes it very difficult task in a terminal.

    Linux comes with a very powerful built-in tool called rename. The rename command is used to rename multiple or group of files, rename files to lowercase, rename files to uppercase and overwrite files using perl expressions.

     

    The “rename” command is a part of Perl script and it resides under “/usr/bin/” on many Linux distributions. You can run “which” command to find out the location of rename command.

    $ which rename
    /usr/bin/rename
    The Basic Syntax of Rename Command
    rename 's/old-name/new-name/' files

    The rename command comes with few optional arguments along with mandatory perl expression that guides rename command to do actual work.
    rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
    1. -v: Print names of files successfully renamed.
    2. -n: Show what files would have been renamed.
    3. -f: Force overwrite existing files.
    4. perlexpr: Perl Expression.

    For better understanding of this utility, we’ve discussed few practical examples of this command in the article.

    1. A Basic Rename Command Example

    Suppose you’ve bunch of files with “.html” extension and you want to rename all “.html” files to “.php” at one go. For example, first do a “ls -l” to check the list of files with “.html” extension.

    # [email protected]:~$ ls -l
    total 22532
    -rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html
    -rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.html
    -rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html
    -rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html
    -rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.html
    -rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.html
    -rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.html

    Now, you want to change the extension of all these files from “.html” to “.php“. You can use the following “rename” command with perl expression as shown below.

    [email protected]:~$ rename 's/\.html$/\.php/' *.html

    Note: In the above command we’ve used two arguments.

    1. First argument is a perl expression that substitute .html with .php.
    2. Second argument tells the rename command to substitute all the files with *.php.

    Let’s verify whether all files are renamed to “.php” extension, doing ls -l on the prompt.

    [email protected]:~$ ls -l
    total 22532
    -rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.php
    -rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.php
    -rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.php
    -rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.php
    -rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.php
    -rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.php
    -rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.php

    Now you can see above that all the html files are renamed to php.

    2. Check Changes Before Running Rename Command

    While doing critical or major renaming tasks, you can always check the changes by running rename command with “-n” argument. The “-n” parameter will tell you exactly what changes would take place, but the changes are not done for real. Here, is the example of the command below.

    [email protected]:~$ rename -n 's/\.php$/\.html/' *.php
    cricket.php renamed as cricket.html
    entertainment.php renamed as entertainment.html
    health.php renamed as health.html
    lifestyle.php renamed as lifestyle.html
    news.php renamed as news.html
    photos.php renamed as photos.html
    sports.php renamed as sports.html

    Note: The above command output only displays changes, but in real the changes are not done, unless you run the command without “-n” switch.

    3. Print Rename Output

    We saw that the rename command didn’t displayed any information of changes it does. So, if you want to get the details of rename command (like we did using “-n” option), here we use “-v” option to print the complete details of all the changes done by rename command successfully.

    [email protected]:~$ rename -v 's/\.php$/\.html/' *.php
    cricket.php renamed as cricket.html
    entertainment.php renamed as entertainment.html
    health.php renamed as health.html
    lifestyle.php renamed as lifestyle.html
    news.php renamed as news.html
    photos.php renamed as photos.html
    sports.php renamed as sports.html

    4. Convert all Lowercase to Uppercase and Vise-Versa

    To batch rename all files with lower case names to upper case. For example, I want to covert all these following files from lower to upper case.

    Lower to Upper Case
    [email protected]:~$ ls -l
    total 22532
    -rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html
    -rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.html
    -rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html
    -rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html
    -rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.html
    -rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.html
    -rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.html

    Just, use the following command with perl expression.

    [email protected]:~$ rename 'y/a-z/A-Z/' *.html

    Once you’ve executed the above command, you can check the changes by doing “ls -l“.

    [email protected]:~$ ls -l
    total 22532
    -rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 CRICKET.HTML
    -rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 ENTERTAINMENT.HTML
    -rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 HEALTH.HTML
    -rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 LIFESTYLE.HTML
    -rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 NEWS.HTML
    -rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 PHOTOS.HTML
    -rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 SPORTS.HTML

    You can see that the above command actually renamed all the lower case file names (with .HTML extension) to upper case.

    Upper to Lower Case

    Similarly, you can also convert all upper case characters to lower case using the following command.

    [email protected]:~$ rename 'y/A-Z/a-z/' *.HTML
    [email protected]:~$ ls -l
    total 22532
    -rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html
    -rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.html
    -rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html
    -rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html
    -rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.html
    -rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.html
    -rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.html

    5. Overwrite Existing Files

    If you would like to forcefully overwrite existing files, use the “-f” option as shown below.

    [email protected]:~$ rename -f 's/a/b/' *.html

    If you would like to know more about rename command, type the “man rename” in the terminal.

    The rename command is very useful, if you are dealing with multiple or batch renaming of files from the command line. Do give a try and let me know, how far is useful in terms of renaming of files.

     

    in Tech Blog
    Streisand Silence censorship. Automate the effect.

    Ontworpen voor bedrijven

    We zijn een team van gepassioneerde mensen met als doel levens te verbeteren met vernieuwende producten. We ontwikkelen geweldige oplossingen voor al je zakelijke uitdagingen. Onze producten zijn ontworpen voor kleine tot middelgrote bedrijven die hun prestaties willen optimaliseren.

    Neem contact op

    Plantexel
    Pedernera
    Salta Capital 
    Argentina

    • +31 653-919-302
    • [email protected]
    Volg ons
    Copyright © Plantexel
    Nederlands | English (US) | Español (AR)