[SOLVED] "ToLower" file extension

Archived topics about LMDE 1 and LMDE 2
Locked
Fuzzy
Level 4
Level 4
Posts: 251
Joined: Thu Jul 28, 2011 11:54 am

[SOLVED] "ToLower" file extension

Post by Fuzzy »

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 LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
catweazel
Level 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: "ToLower" file extension

Post by catweazel »

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.
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
Fuzzy
Level 4
Level 4
Posts: 251
Joined: Thu Jul 28, 2011 11:54 am

Re: "ToLower" file extension

Post by Fuzzy »

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

Re: "ToLower" file extension

Post by gm10 »

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
AZgl1800
Level 20
Level 20
Posts: 11145
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 AZgl1800 »

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.
LM21.3 Cinnamon ASUS FX705GM | Donate to Mint https://www.patreon.com/linux_mint
Image
User avatar
catweazel
Level 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: "ToLower" file extension

Post by catweazel »

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.
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
User avatar
catweazel
Level 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: "ToLower" file extension

Post by catweazel »

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.
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
gm10

Re: "ToLower" file extension

Post by gm10 »

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 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: "ToLower" file extension

Post by catweazel »

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 :)
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
Fuzzy
Level 4
Level 4
Posts: 251
Joined: Thu Jul 28, 2011 11:54 am

Re: "ToLower" file extension

Post by Fuzzy »

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 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: "ToLower" file extension

Post by catweazel »

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.
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
Fuzzy
Level 4
Level 4
Posts: 251
Joined: Thu Jul 28, 2011 11:54 am

Re: "ToLower" file extension

Post by Fuzzy »

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

Re: "ToLower" file extension

Post by gm10 »

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

Re: "ToLower" file extension

Post by gm10 »

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 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: "ToLower" file extension

Post by catweazel »

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:
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
User avatar
smurphos
Level 18
Level 18
Posts: 8501
Joined: Fri Sep 05, 2014 12:18 am
Location: Irish Brit in Portugal
Contact:

Re: "ToLower" file extension

Post by smurphos »

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
For custom Nemo actions, useful scripts for the Cinnamon desktop, and Cinnamox themes visit my Github pages.
gm10

Re: "ToLower" file extension

Post by gm10 »

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

Re: "ToLower" file extension

Post by gm10 »

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 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: "ToLower" file extension

Post by catweazel »

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:
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
Fuzzy
Level 4
Level 4
Posts: 251
Joined: Thu Jul 28, 2011 11:54 am

Re: "ToLower" file extension

Post by Fuzzy »

...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
Locked

Return to “LMDE Archive”