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

 

 

 

Bash Script Help Please...

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
ilovepoker
Posts: 1
Joined: 2014-10-02 23:20

Bash Script Help Please...

#1 Post by ilovepoker »

Ok i need some help here people...
I have 30k+ images without extensions...
I used a program called GPRename to give each of my images a random name, because some of the names were very long so they were causing problems, and it deleted the file extension on all my pictures now I'm having difficulty opening the images on my windows computer and my phone (no prob on linux)
I was wondering if any of you know of a program/script I can use that would add the correct file extension to the end of all the images
I was thinking of a script using the bash command "file" but i'm not very good with bash so I would have no idea how to do this for multiple files


User avatar
saulgoode
Posts: 1445
Joined: 2007-10-22 11:34
Been thanked: 4 times

Re: Bash Script Help Please...

#3 Post by saulgoode »

Try the following code in a copy of one of your directories containing the extensionless files.

Code: Select all

for f in *; do mv -v "$f" "$f.$( ( file "$f" | cut -s -d\:  -f 2 | cut -s -d\  -f 2 | tr [:upper:] [:lower:] ) )";done
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian Kernighan

User avatar
aicardi
Posts: 388
Joined: 2009-11-18 01:30
Location: Chicago

Re: Bash Script Help Please...

#4 Post by aicardi »

ilovepoker wrote: I was wondering if any of you know of a program/script I can use that would add the correct file extension to the end of all the images
If all the extensions were the same as in (.JPG, .jpg, etc...) you could add that extension back easy enough.
Example of before renaming:

Code: Select all

$ ls
imagefile1.jpg imagefile2.jpg
Is now:

Code: Select all

$ ls
NEWNAME1 NEWNAME2
Within a COPY of one of the directories you could add an extension like this. Adding .jpg as an example.:

Code: Select all

for old in *; do mv $old `basename $old `.jpg; done; 
Your listing will now be:

Code: Select all

$ ls
NEWNAME1.jpg NEWNAME2.jpg
Note that the extension will be added to every file including directories.
Jessie/Xfce

Post Reply