[SOLVED] "ToLower" file extension

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read how to get help

LMDE 2 support ends on 1-1-2019
Post Reply
Fuzzy
Level 3
Level 3
Posts: 146
Joined: Thu Jul 28, 2011 11:54 am

[SOLVED] "ToLower" file extension

Post by Fuzzy » Fri Sep 07, 2018 10:37 pm

I've had a problem in LMDE2 for a while, but never bothered to ask about it, and always just work around it.

However, I'd like to find an answer:

I have a folder full of a mix of .jpg and .JPG files. While trying to create a quick bash script to run on *.jpg, I found that when I try to mv or rename a .JPG file to a .jpg file - I receive an error that the file already exists. I can't find a way to rename the capital JPG extensions to lowercase jpg extensions without specifying a different destination filename. (Even using the GUI)

I'm sure the answer is obvious, but I haven't been successful in my on-line queries to find an answer of how "tolower" the file extensions. (To be clear, I've found scripts which claim to perform the renaming of the extensions, but they fail with the same error.)

Again, I've always just worked around this by renaming the destination files, but surely I'm missing an easier way.

Thanks,
Fuzzy
Last edited by Fuzzy on Sat Sep 08, 2018 10:16 am, edited 1 time in total.

User avatar
catweazel
Level 17
Level 17
Posts: 7741
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: "ToLower" file extension

Post by catweazel » Fri Sep 07, 2018 10:39 pm

Fuzzy wrote:
Fri Sep 07, 2018 10:37 pm
I found that when I try to mv or rename a .JPG file to a .jpg file - I receive an error that the file already exists.
Post an example command that you use when you get this message, along with some file names it applies to.

Please enclose the results between [ⅽode] and [/ⅽode] code markers by selecting </> from the mini toolbar above the textbox where you type your reply.
¡uʍop ǝpısdn sı buıɥʇʎɹǝʌǝ os ɐıןɐɹʇsnɐ ɯoɹɟ ɯ,ı

Fuzzy
Level 3
Level 3
Posts: 146
Joined: Thu Jul 28, 2011 11:54 am

Re: "ToLower" file extension

Post by Fuzzy » Fri Sep 07, 2018 11:07 pm

Thanks for the quick response!

Literally, using the GUI I can right-click a file named File.JPG and change the name to File.jpg, and I will receive an error.

If I use a command line like:

Code: Select all

rename 's/\.JPG$/.jpg/' *.JPG
I receive the following errors:

Code: Select all

DSC_0396.JPG not renamed: DSC_0396.jpg already exists
DSC_0409.JPG not renamed: DSC_0409.jpg already exists
DSC_0421.JPG not renamed: DSC_0421.jpg already exists
DSC_0530.JPG not renamed: DSC_0530.jpg already exists
DSC_0545.JPG not renamed: DSC_0545.jpg already exists
DSC_0559.JPG not renamed: DSC_0559.jpg already exists
DSC_0561.JPG not renamed: DSC_0561.jpg already exists
This causes some problems in my scripts, as they perform loops based on the number of .jpg files - and I realize there are ways around this, but it is odd to me that I'm unable to rename the file extension to a lower-case version of the same thing.

EDIT: It is important to note that the bash scripts are sensitive about lowercase vs uppercase extensions. For instance, I'll run a script which starts with something like

Code: Select all

for i in *.jpg
It then skips any .JPG files. So my plan was to rename the JPG to jpg, then run the script.

gm10
Level 12
Level 12
Posts: 4092
Joined: Thu Jun 21, 2018 5:11 pm

Re: "ToLower" file extension

Post by gm10 » Fri Sep 07, 2018 11:25 pm

Fuzzy wrote:
Fri Sep 07, 2018 10:37 pm
I found that when I try to mv or rename a .JPG file to a .jpg file - I receive an error that the file already exists.
Because you already have files of the same name but with the lower case extension.

User avatar
AZgl1500
Level 9
Level 9
Posts: 2722
Joined: Thu Dec 31, 2015 3:20 am
Location: Oklahoma where the wind comes sweeping down the plains
Contact:

Re: "ToLower" file extension

Post by AZgl1500 » Fri Sep 07, 2018 11:29 pm

The Sledge Hammer approach would be to copy all of the *.JPG files to a separate HDD while renaming them in the process.

then delete the *.JPG files, and copy the renamed *.jpg files back

you would have to sort by Extension to make this easier though, to delete the JPG files.

User avatar
catweazel
Level 17
Level 17
Posts: 7741
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: "ToLower" file extension

Post by catweazel » Fri Sep 07, 2018 11:30 pm

Fuzzy wrote:
Fri Sep 07, 2018 11:07 pm
This causes some problems in my scripts
That's because you're using the wrong perl expression in the rename. It's years since I last looked at perl but I think you need the y rather than the s operator.

rename 'y/\.JPG$/.jpg/' *.JPG

Anyway, this will do it for you using mv:

Code: Select all

#!/bin/bash
for fn in *.JPG
do
  mv "$fn" "${fn%.JPG}.jpg"
done 
Last edited by catweazel on Fri Sep 07, 2018 11:32 pm, edited 2 times in total.
¡uʍop ǝpısdn sı buıɥʇʎɹǝʌǝ os ɐıןɐɹʇsnɐ ɯoɹɟ ɯ,ı

User avatar
catweazel
Level 17
Level 17
Posts: 7741
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: "ToLower" file extension

Post by catweazel » Fri Sep 07, 2018 11:30 pm

AZgl1500 wrote:
Fri Sep 07, 2018 11:29 pm
The Sledge Hammer approach would be to copy all of the *.JPG files to a separate HDD while renaming them in the process.

then delete the *.JPG files, and copy the renamed *.jpg files back

you would have to sort by Extension to make this easier though, to delete the JPG files.
That's completely impractical.
¡uʍop ǝpısdn sı buıɥʇʎɹǝʌǝ os ɐıןɐɹʇsnɐ ɯoɹɟ ɯ,ı

gm10
Level 12
Level 12
Posts: 4092
Joined: Thu Jun 21, 2018 5:11 pm

Re: "ToLower" file extension

Post by gm10 » Fri Sep 07, 2018 11:36 pm

catweazel wrote:
Fri Sep 07, 2018 11:30 pm
That's because you're using the wrong perl expression in the rename. It's years since I last looked at perl but I think you need the y rather than the s operator.

rename 'y/\.JPG$/.jpg/' *.JPG
Doesn't make a difference. y/ is a transform and s/ is a replace, same result in this case. I'll agree y/ is preferable for this kind of stuff insofar as you can mess up less if you make a mistake. :D

User avatar
catweazel
Level 17
Level 17
Posts: 7741
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: "ToLower" file extension

Post by catweazel » Fri Sep 07, 2018 11:42 pm

gm10 wrote:
Fri Sep 07, 2018 11:36 pm
catweazel wrote:
Fri Sep 07, 2018 11:30 pm
That's because you're using the wrong perl expression in the rename. It's years since I last looked at perl but I think you need the y rather than the s operator.

rename 'y/\.JPG$/.jpg/' *.JPG
Doesn't make a difference. y/ is a transform and s/ is a replace, same result in this case. y/ is preferable insofar as you can rainbows up less if you make a mistake. :D
I'm sure you're probably correct. I can't be bothered to re-learn perl :)

The mv example will work though.

EDIT: I tested rename "y... and you are indeed wrong.

Before:

Code: Select all

-rw-rw-r-- 1 boot boot 448208 Sep  8 13:38 'DSCN0008 (1).JPG'
-rw-rw-r-- 1 boot boot 448208 Sep  8 13:38 'DSCN0008 (2).JPG'
-rw-rw-r-- 1 boot boot 448208 Sep  8 13:38 'DSCN0008 (3).JPG'
-rw-rw-r-- 1 boot boot 448208 Sep  8 13:38 'DSCN0008 (4).JPG'
-rw-rw-r-- 1 boot boot 448208 Sep  8 13:38 'DSCN0008 (5).JPG'
-rw-rw-r-- 1 boot boot 448208 Sep  8 13:38  DSCN0008.JPG
After rename 'y/\.JPG$/.jpg/' *.JPG:

Code: Select all

-rw-rw-r-- 1 boot boot 448208 Sep  8 13:38 'DSCN0008 (1).jpg'
-rw-rw-r-- 1 boot boot 448208 Sep  8 13:38 'DSCN0008 (2).jpg'
-rw-rw-r-- 1 boot boot 448208 Sep  8 13:38 'DSCN0008 (3).jpg'
-rw-rw-r-- 1 boot boot 448208 Sep  8 13:38 'DSCN0008 (4).jpg'
-rw-rw-r-- 1 boot boot 448208 Sep  8 13:38 'DSCN0008 (5).jpg'
-rw-rw-r-- 1 boot boot 448208 Sep  8 13:38  DSCN0008.jpg
I frighten myself with my genius sometimes :)
¡uʍop ǝpısdn sı buıɥʇʎɹǝʌǝ os ɐıןɐɹʇsnɐ ɯoɹɟ ɯ,ı

Fuzzy
Level 3
Level 3
Posts: 146
Joined: Thu Jul 28, 2011 11:54 am

Re: "ToLower" file extension

Post by Fuzzy » Fri Sep 07, 2018 11:56 pm

Hi folks. Thanks for your time and input.

I've ended up (short term) simply running a script to rename them to .png, then to .jpg. Not a solution, but a work-around.

Catweazel - I copy/pasted your script into a bash named catweazel.bash, but when I run it, I get the same results as I posted above.

Code: Select all

bash catweazel.bash
(which contains:

Code: Select all

#!/bin/bash
for fn in *.JPG
do
  mv "$fn" "${fn%.JPG}.jpg"
done
and outputs:

Code: Select all

mv: ‘DSC_0200.JPG’ and ‘DSC_0200.jpg’ are the same file
mv: ‘DSC_0209.JPG’ and ‘DSC_0209.jpg’ are the same file
mv: ‘DSC_0211.JPG’ and ‘DSC_0211.jpg’ are the same file
mv: ‘DSC_0230.JPG’ and ‘DSC_0230.jpg’ are the same file
mv: ‘DSC_0238.JPG’ and ‘DSC_0238.jpg’ are the same file
As evidenced by my post, I'm not good at these things, so if there is something obvious which I could have missed which would cause these problems, start there because I probably missed it. ;^)>>>

Thanks again for your efforts to help me.

User avatar
catweazel
Level 17
Level 17
Posts: 7741
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: "ToLower" file extension

Post by catweazel » Fri Sep 07, 2018 11:59 pm

Fuzzy wrote:
Fri Sep 07, 2018 11:56 pm
Catweazel - I copy/pasted your script into a bash named catweazel.bash, but when I run it, I get the same results as I posted above.
How bizarre. Both examples I gave were tested and work fine for me.
¡uʍop ǝpısdn sı buıɥʇʎɹǝʌǝ os ɐıןɐɹʇsnɐ ɯoɹɟ ɯ,ı

Fuzzy
Level 3
Level 3
Posts: 146
Joined: Thu Jul 28, 2011 11:54 am

Re: "ToLower" file extension

Post by Fuzzy » Sat Sep 08, 2018 12:11 am

How bizarre. Both examples I gave were tested and work fine for me.
I agree. It has me scratching my head!

For now, I'll continue using my work-around of renaming the files to png and then back to jpg, as this project is fairly small, and just for my own personal website...but over the last year or two, I've had to work around the issue a handful of times, so I thought I'd throw it out here for you smart folks and see if anyone else had the problem, or knew of a solution.

Thanks again for the help!
Fuzzy

gm10
Level 12
Level 12
Posts: 4092
Joined: Thu Jun 21, 2018 5:11 pm

Re: "ToLower" file extension

Post by gm10 » Sat Sep 08, 2018 12:13 am

catweazel wrote:
Fri Sep 07, 2018 11:42 pm
EDIT: I tested rename "y... and you are indeed wrong.
How am I wrong? I told you y/ would work. I'm so confused that I suddenly felt the urge to have a signature. :shock:

gm10
Level 12
Level 12
Posts: 4092
Joined: Thu Jun 21, 2018 5:11 pm

Re: "ToLower" file extension

Post by gm10 » Sat Sep 08, 2018 12:19 am

Fuzzy wrote:
Fri Sep 07, 2018 11:56 pm
and outputs:

Code: Select all

mv: ‘DSC_0200.JPG’ and ‘DSC_0200.jpg’ are the same file
mv: ‘DSC_0209.JPG’ and ‘DSC_0209.jpg’ are the same file
mv: ‘DSC_0211.JPG’ and ‘DSC_0211.jpg’ are the same file
mv: ‘DSC_0230.JPG’ and ‘DSC_0230.jpg’ are the same file
mv: ‘DSC_0238.JPG’ and ‘DSC_0238.jpg’ are the same file
You are doing this on a non-case sensitive file system like FAT32 is the only logical explanation I can think of.
Last edited by gm10 on Sat Sep 08, 2018 12:20 am, edited 1 time in total.

User avatar
catweazel
Level 17
Level 17
Posts: 7741
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: "ToLower" file extension

Post by catweazel » Sat Sep 08, 2018 12:20 am

gm10 wrote:
Sat Sep 08, 2018 12:13 am
catweazel wrote:
Fri Sep 07, 2018 11:42 pm
EDIT: I tested rename "y... and you are indeed wrong.
How am I wrong? I told you y/ would work. I'm so confused that I suddenly felt the urge to have a signature. :shock:
You mean "wouldn't", yet both examples I gave the OP work fine for me here. As for your .sig, mine gets me out of any predicament :mrgreen:
¡uʍop ǝpısdn sı buıɥʇʎɹǝʌǝ os ɐıןɐɹʇsnɐ ɯoɹɟ ɯ,ı

User avatar
smurphos
Level 8
Level 8
Posts: 2007
Joined: Fri Sep 05, 2014 12:18 am
Location: Britisher...

Re: "ToLower" file extension

Post by smurphos » Sat Sep 08, 2018 12:22 am

Fuzzy wrote:
Fri Sep 07, 2018 11:07 pm
For instance, I'll run a script which starts with something like

Code: Select all

for i in *.jpg
It then skips any .JPG files. So my plan was to rename the JPG to jpg, then run the script.
This works without having to rename any extension...

for i in *.jpg *.JPG *.jpeg *.JPEG

gm10
Level 12
Level 12
Posts: 4092
Joined: Thu Jun 21, 2018 5:11 pm

Re: "ToLower" file extension

Post by gm10 » Sat Sep 08, 2018 12:22 am

catweazel wrote:
Sat Sep 08, 2018 12:20 am
You mean "wouldn't", yet both examples I gave the OP work fine for me here. As for your .sig, mine gets me out of any predicament :mrgreen:
No U. ;) I even agreed with you that y/ is preferable, so I'm really confused about this misunderstanding.

At the same time I really don't care one bit, so let's carry on. You can now point to FAT32 and then say I denied that previously. :twisted: :lol:

gm10
Level 12
Level 12
Posts: 4092
Joined: Thu Jun 21, 2018 5:11 pm

Re: "ToLower" file extension

Post by gm10 » Sat Sep 08, 2018 12:24 am

smurphos wrote:
Sat Sep 08, 2018 12:22 am
This works without having to rename any extension...

for i in *.jpg *.JPG *.jpeg *.JPEG
So does find -lname, which is nearly always superior to these for loops IMHO.

User avatar
catweazel
Level 17
Level 17
Posts: 7741
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: "ToLower" file extension

Post by catweazel » Sat Sep 08, 2018 12:26 am

gm10 wrote:
Sat Sep 08, 2018 12:22 am
No U. ;) I even agreed with you that y/ is preferable, so I'm really confused about this misunderstanding.
Doesn't make a difference. y/ is a transform and s/ is a replace, same result in this case.
:lol:
¡uʍop ǝpısdn sı buıɥʇʎɹǝʌǝ os ɐıןɐɹʇsnɐ ɯoɹɟ ɯ,ı

Fuzzy
Level 3
Level 3
Posts: 146
Joined: Thu Jul 28, 2011 11:54 am

Re: "ToLower" file extension

Post by Fuzzy » Sat Sep 08, 2018 10:15 am

...if there is something obvious which I could have missed which would cause these problems, start there because I probably missed it.
and
You are doing this on a non-case sensitive file system like FAT32 is the only logical explanation I can think of.
Yep! It is an old SAMBA share on an old machine that has been around far too long. Not sure of the filesystem being used, but that's the cause. I tried to copy some of the photos/files in question to my local machine, and tried again - everything works fine.

Sorry for wasting everyone's time, but I sure appreciate the help, and I'll remember this next time. (I'll also be checking/changing some configurations around to avoid this issue in the future.)

Marking as SOLVED.

Thanks,
Fuzzy

Post Reply

Return to “Newbie Questions”