How to rename extensions of multiple files?

New to Debian (Or Linux in general)? Ask your questions here!

How to rename extensions of multiple files?

Postby Xeno Idaltu » 2020-04-06 06:45

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?
User avatar
Xeno Idaltu
 
Posts: 33
Joined: 2019-08-15 05:36
Location: Free-Source Technocracy

Re: How to rename extensions of multiple files?

Postby Dai_trying » 2020-04-06 09:10

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.
Dai_trying
 
Posts: 941
Joined: 2016-01-07 12:25

Re: How to rename extensions of multiple files?

Postby Head_on_a_Stick » 2020-04-06 09:13

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.
Black Lives Matter

Debian buster-backports ISO image: for new hardware support
User avatar
Head_on_a_Stick
 
Posts: 12612
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: How to rename extensions of multiple files?

Postby sunrat » 2020-04-06 11:34

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
sunrat
 
Posts: 3225
Joined: 2006-08-29 09:12
Location: Melbourne, Australia

Re: How to rename extensions of multiple files?

Postby Head_on_a_Stick » 2020-04-07 06:51

^ No need for sed:
Code: Select all
for pic in *;do mv $pic ${pic%.*}.$(file ${pic}|awk '{gsub(/jpeg/,jpg);print tolower ($2)}');done
Black Lives Matter

Debian buster-backports ISO image: for new hardware support
User avatar
Head_on_a_Stick
 
Posts: 12612
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: How to rename extensions of multiple files?

Postby arzgi » 2020-04-07 15:54

Neat code examples, thanks!

For lazy ones
Code: Select all
aptitude search rename
arzgi
 
Posts: 654
Joined: 2008-02-21 17:03
Location: Finland

Re: How to rename extensions of multiple files?

Postby sunrat » 2020-04-07 23:56

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
sunrat
 
Posts: 3225
Joined: 2006-08-29 09:12
Location: Melbourne, Australia

Re: How to rename extensions of multiple files?

Postby NFT5 » 2020-04-08 08:01

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
NFT5
 
Posts: 401
Joined: 2014-10-10 11:38
Location: Canberra, Australia

Re: How to rename extensions of multiple files?

Postby Head_on_a_Stick » 2020-04-08 08:30

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.
Black Lives Matter

Debian buster-backports ISO image: for new hardware support
User avatar
Head_on_a_Stick
 
Posts: 12612
Joined: 2014-06-01 17:46
Location: /dev/chair


Return to Beginners Questions

Who is online

Users browsing this forum: No registered users and 11 guests

fashionable