How to rename file extension to lowercase?

9,225

Solution 1

It is really simple:

  1. Rename to something else than the same value with different case

    rename 's/\.JPG$/\.jpgaux/' *.JPG
    
  2. Now rename that something else to .jpg back again, but lowercase this time

    rename 's/\.jpgaux$/\.jpg/' *.jpgaux
    

Demo: http://paste.ubuntu.com/8853245/

Source: How to change extension of multiple files from command line? Thanks to Chakra!

Solution 2

really easy with mmv:

sudo apt install mmv

mmv \*.JPEG \#1.jpeg

Solution 3

If αғsнιη is right in his comment, and I think he is, OP's problem is that a similarly named file already exists. If that is the case, the script will have to check if the targeted file name (lowercase) already exists, and (only) if so, rename the original file additionally (not only lowercase extension) to prevent the name error, e.g.

image1.JPG

to

renamed_image1.jpg

since image1.jpg would raise an error

If so, a python solution to rename could be:

#!/usr/bin/env python3

import os
import shutil
import sys

directory = sys.argv[1]
for file in [f for f in os.listdir(directory) if f.endswith(".JPG")]:
        newname = file[:file.rfind(".")]+".jpg"
        if os.path.exists(directory+"/"+newname):
                newname = "renamed_"+newname
        shutil.move(directory+"/"+file, directory+"/"+newname)

The script renames:

image1.JPG -> image1.jpg

but if image1.jpg already exists:

image1.JPG -> renamed_image1.jpg

###How to use

Copy the script into an empty file, save it as rename.py, make it executable and run it by the command:

<script> <directory_of_files>

Solution 4

This works best I think, as perl supports running code in the regex

rename -n 's/(\.[A-Z]+$)/lc($1)/ge' *.*[A-Z]*

remove the -n to actually rename the files

Share:
9,225

Related videos on Youtube

littlevache
Author by

littlevache

Updated on September 18, 2022

Comments

  • littlevache
    littlevache over 1 year

    I know this question has been asked (and answered) before, but it seems my situation is unique, because I cannot have any of the solution to work.

    Running, I need to rename all my photos from *.JPG to *.jpg.

    Let's say I don't need recursive, just all the pictures in the same folder.

    The problem I meet is this one:

    mv: ‘P1010521.JPG’ and ‘p1010521.jpg’ are the same file
    

    Same problem using rename, with that kind of command:

    rename 's/\.JPG$/.jpg/' *.JPG
    P1020558.JPG not renamed: P1020558.jpg already exists
    
    • Lucio
      Lucio over 9 years
      Came on, you are at one step to make it! What script do you have by now?
    • Rmano
      Rmano over 9 years
      Which filesystem the files are on? I guess it's not ext4...
  • littlevache
    littlevache over 9 years
    Smart ... or stupid of me, depends on the point of view.Tx
  • Lucio
    Lucio over 9 years
    None of both.. Remember to mark it as accepted to close the question :)
  • Rmano
    Rmano over 9 years
    @Lucio,...I guess that the files are on a case-insensitive filesystem (vfat, ntfs); otherwise the "plain" rename would work.
  • Lucio
    Lucio over 9 years
    @Rmano absolutely.
  • αғsнιη
    αғsнιη over 9 years
    @Lucio I think you misunderstood the OP's question. He/She wants to rename P1020558.JPG to P1020558.jpg but he/she has another file which is that exist with same name P1020558.jpg. Then gets the error: P1020558.JPG not renamed: P1020558.jpg already exists. Also your solution is wrong because the name of files are the same and the solution doesn't effect any changes and still he/she will have the error. for example test on touch Image{1..5}.JPG;touch Image{1..5}.jpg your own answer.
  • gniourf_gniourf
    gniourf_gniourf over 9 years
    Did you read that the question mentions the following error? mv: ‘P1010521.JPG’ and ‘P1010521.jpg’ are the same file. Are the same file…
  • Jacob Vlijm
    Jacob Vlijm over 9 years
    @gniourf_gniourf that seems in contradiction with the fact that renaming is impossible in his command (.JPG and .jpg)
  • Jacob Vlijm
    Jacob Vlijm over 9 years
    @gniourf_gniourf when I try his command with a bunch of JPGs, they are simply renamed, so the targeted name must already exist in his case.
  • gniourf_gniourf
    gniourf_gniourf over 9 years
    Very likely, OP is on a case-insensitive filesystem (as was already mentioned in several comments). As an example, try it yourself on a pendrive with a FAT32 partition ;).
  • gniourf_gniourf
    gniourf_gniourf over 9 years
    And to get the error are the same file, do: touch gniourf; mv gniourf Gniourf.
  • αғsнιη
    αғsнιη over 9 years
    @gniourf_gniourf No it's case-sensitive. according to P1020558.JPG not renamed: P1020558.jpg already exists. both files P1020558.JPG and P1020558.jpg already exist on his partition and also according to mv command that said both file are "‘P1010521.JPG’ and ‘P1010521.jpg’" are the same file. then they both are exist and that's not case-insensitive.
  • gniourf_gniourf
    gniourf_gniourf over 9 years
    @KasiyA do you understand what are the same file means?
  • αғsнιη
    αғsнιη over 9 years
    @gniourf_gniourf Yes I know if the file is copy of the same file with different name. and also I understand P1020558.JPG not renamed: P1020558.jpg already exists what is the meaning of this command. If you are not sure. test it for yourself all of these files are the same file with different names. And the OP nowhere said he/she use FAT32 file system.
  • gniourf_gniourf
    gniourf_gniourf over 9 years
    @KasiyA you know what? try it first on your own on a pendrive with a FAT32 partition. Then you'll see…
  • that other guy
    that other guy over 9 years
    +1, but escape the .s so they won't match any character. (I'd just edit, but 2 char changes are not accepted and I didn't want to write four more chars of garbage)
  • Lucio
    Lucio over 9 years
    @KasiyA before your edit, that was not the case. My answer solves the original OP issue.
  • Lucio
    Lucio over 9 years
    @thatotherguy I don't understand your problem but feel free to edit the answer :-)
  • αғsнιη
    αғsнιη over 9 years
    @Lucio If author use Fat32 file system then there is no different between P1010521.JPG’ or ‘p1010521.jpg’. So my edit doesn't cause any change on OP's Q. yes your solution is right if OP use fat32 file system but no where he/she said that +1 :)
  • Lucio
    Lucio over 9 years
    No where he/she said that it was not the file system :)
  • Pablo Bianchi
    Pablo Bianchi over 4 years
    How could we rename on a case-sensitive filesystem? rename 'y/a-z/A-Z/' /tmp/*.jpg -> Can't rename /tmp/*.jpg /TMP/*.JPG: No such file or directory