Page 1 of 1

[SOLVED] "ToLower" file extension

Posted: Fri Sep 07, 2018 10:37 pm
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

Re: "ToLower" file extension

Posted: Fri Sep 07, 2018 10:39 pm
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.

Re: "ToLower" file extension

Posted: Fri Sep 07, 2018 11:07 pm
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.

Re: "ToLower" file extension

Posted: Fri Sep 07, 2018 11:25 pm
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.

Re: "ToLower" file extension

Posted: Fri Sep 07, 2018 11:29 pm
by AZgl1500
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.

Re: "ToLower" file extension

Posted: Fri Sep 07, 2018 11:30 pm
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 

Re: "ToLower" file extension

Posted: Fri Sep 07, 2018 11:30 pm
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.

Re: "ToLower" file extension

Posted: Fri Sep 07, 2018 11:36 pm
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

Re: "ToLower" file extension

Posted: Fri Sep 07, 2018 11:42 pm
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 :)

Re: "ToLower" file extension

Posted: Fri Sep 07, 2018 11:56 pm
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.

Re: "ToLower" file extension

Posted: Fri Sep 07, 2018 11:59 pm
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.

Re: "ToLower" file extension

Posted: Sat Sep 08, 2018 12:11 am
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

Re: "ToLower" file extension

Posted: Sat Sep 08, 2018 12:13 am
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:

Re: "ToLower" file extension

Posted: Sat Sep 08, 2018 12:19 am
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.

Re: "ToLower" file extension

Posted: Sat Sep 08, 2018 12:20 am
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:

Re: "ToLower" file extension

Posted: Sat Sep 08, 2018 12:22 am
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

Re: "ToLower" file extension

Posted: Sat Sep 08, 2018 12:22 am
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:

Re: "ToLower" file extension

Posted: Sat Sep 08, 2018 12:24 am
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.

Re: "ToLower" file extension

Posted: Sat Sep 08, 2018 12:26 am
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:

Re: "ToLower" file extension

Posted: Sat Sep 08, 2018 10:15 am
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