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?
- Xeno Idaltu
- Posts: 47
- Joined: 2019-08-15 05:36
- Location: Free-Source Technocracy
How to rename extensions of multiple files?
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?
(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?
-
- Posts: 1100
- Joined: 2016-01-07 12:25
- Has thanked: 5 times
- Been thanked: 16 times
Re: How to rename extensions of multiple files?
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.
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.
- 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?
Run this command in the directory that contains all of your images:
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.
Code: Select all
for pic in *;do mv $pic ${pic%.*}.$(file $pic|awk '{print $2}');done
deadbang
- sunrat
- 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?
Nice.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
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!
Those who have lost data
...and those who have not lost data YET ” Remember to BACKUP!
- 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?
^ 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
- sunrat
- 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?
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: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
Code: Select all
for pic in *;do mv $pic ${pic%.*}.$(file ${pic}|awk '{gsub(/JPEG/,"jpg");print tolower ($2)}');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!
Those who have lost data
...and those who have not lost data YET ” Remember to BACKUP!
- NFT5
- 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?
There is.Dai_trying wrote: don't know if there is a GUI to allow this
For the really lazy ones, who use KDE.................KRename.arzgi wrote:For lazy ones
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.
- 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?
No. It's Turing-complete (given infinite memory & time) and so could be used to build an entire operating system.sunrat wrote:Is there anything awk can't do?
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.sunrat wrote:can you think of a more elegant way?
It's possible the entire operation could be done with an awk script but that's beyond my current abilities.
deadbang