Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

How to rename extensions of multiple files?

New to Debian (Or Linux in general)? Ask your questions here!
Post Reply
Message
Author
User avatar
Xeno Idaltu
Posts: 47
Joined: 2019-08-15 05:36
Location: Free-Source Technocracy

How to rename extensions of multiple files?

#1 Post by Xeno Idaltu »

I recently moved pictures I had on my phone to my computer. On Dolphin File Manager I get a thumbnail/preview of each file as long as the file extension matches the image's format. I have many files that are png but have jpg/wepb as an extension and vice versa. For the thumbnail to show up I have to fix the extension name to the image's real format.
(Ex.) GreenPeppers.jpg to GreenPeppers.png

I have many files like this. Is there an easier way to rename the file extension of multiple files without changing the title of each image?

Dai_trying
Posts: 1100
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: How to rename extensions of multiple files?

#2 Post by Dai_trying »

I don't know if there is a GUI to allow this, but I would write a short bash script that would read the image format (I use mediainfo but there are other similar packages) and then check that against the file extension and alter it if necessary.

And if you save this script to either ~/bin or ~/.local/bin you should have it available as a normal command. If you need to create your bin folder you will need to log out and back in again for it to be added to your $PATH.

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: How to rename extensions of multiple files?

#3 Post by Head_on_a_Stick »

Run this command in the directory that contains all of your images:

Code: Select all

for pic in *;do mv $pic ${pic%.*}.$(file $pic|awk '{print $2}');done
Probably best to do this in a backup directory though just in case I've got it wrong. And be sure to remove any files that aren't pictures beforehand.
deadbang

User avatar
sunrat
Administrator
Administrator
Posts: 6412
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 116 times
Been thanked: 462 times

Re: How to rename extensions of multiple files?

#4 Post by sunrat »

Head_on_a_Stick wrote:Run this command in the directory that contains all of your images:

Code: Select all

for pic in *;do mv $pic ${pic%.*}.$(file $pic|awk '{print $2}');done
Nice.
Command prints suffix in capital letters and suffix for JPEG is JPEG rather than jpg. This will change to lower case and substitute jpg for jpeg:

Code: Select all

for pic in *;do mv $pic ${pic%.*}.$(file $pic|awk '{print tolower ($2)}' |sed s/jpeg/jpg/g) ; done
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: How to rename extensions of multiple files?

#5 Post by Head_on_a_Stick »

^ No need for sed:

Code: Select all

for pic in *;do mv $pic ${pic%.*}.$(file ${pic}|awk '{gsub(/jpeg/,jpg);print tolower ($2)}');done
deadbang

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: How to rename extensions of multiple files?

#6 Post by arzgi »

Neat code examples, thanks!

For lazy ones

Code: Select all

aptitude search rename

User avatar
sunrat
Administrator
Administrator
Posts: 6412
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 116 times
Been thanked: 462 times

Re: How to rename extensions of multiple files?

#7 Post by sunrat »

Head_on_a_Stick wrote:^ No need for sed:

Code: Select all

for pic in *;do mv $pic ${pic%.*}.$(file ${pic}|awk '{gsub(/jpeg/,jpg);print tolower ($2)}');done
Is there anything awk can't do? :) As file returns "JPEG" the gsub doesn't work there. Only way I could get it to work correctly is:

Code: Select all

for pic in *;do mv $pic ${pic%.*}.$(file ${pic}|awk '{gsub(/JPEG/,"jpg");print tolower ($2)}');done
@Head - can you think of a more elegant way?
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

User avatar
NFT5
df -h | grep > 20TiB
df -h | grep > 20TiB
Posts: 597
Joined: 2014-10-10 11:38
Location: Canberra, Australia
Has thanked: 10 times
Been thanked: 43 times

Re: How to rename extensions of multiple files?

#8 Post by NFT5 »

Dai_trying wrote: don't know if there is a GUI to allow this
There is.
arzgi wrote:For lazy ones
For the really lazy ones, who use KDE.................KRename.

Should work with the Exiv2 plugin to identify file type for jpeg & tiff by reading the tags.

I used it a while back to sort out a mess created by a Back In Time restore (with some help from myself) when it renamed hundreds of files. Worked like magic.

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: How to rename extensions of multiple files?

#9 Post by Head_on_a_Stick »

sunrat wrote:Is there anything awk can't do?
No. It's Turing-complete (given infinite memory & time) and so could be used to build an entire operating system.
sunrat wrote:can you think of a more elegant way?
I was trying to use find(1) instead of a shell loop but it requires a quoted shell string in the exec bit and that doesn't seem to be compatible with the quotes in awk, unfortunately.

It's possible the entire operation could be done with an awk script but that's beyond my current abilities.
deadbang

Post Reply