What went wrong with this very short shell script?

Forum rules
Before you post please read how to get help
Post Reply
AwaitingUserInput
Level 1
Level 1
Posts: 16
Joined: Tue Sep 30, 2014 6:47 pm

What went wrong with this very short shell script?

Post by AwaitingUserInput » Thu Mar 24, 2016 3:16 pm

I just wrote another post similar to this. I am trying to learn basic scripting to handle large amounts of files. For this task, I have a directory with files ending in .jpg. I want to change the file extension from .jpg to .png.

Here is the script I used:

Code: Select all

for orig_name in *.jpg ; do b=$(basename $orig_name) ; mv $orig_name $b".png" ; done
Instead of replacing .jpg with .png, it just added .png to the end of the file name.

Example:

I had foo.jpg
I wanted to rename it to foo.png
Instead it renamed it to foo.jpg.png

What did I do wrong and why?

I'm not trying to get this script to work so much as I am trying to learn the reasons it didn't work so future ones will be better.

User avatar
cholq
Level 3
Level 3
Posts: 113
Joined: Tue Jul 08, 2014 9:27 pm
Location: New Jersey, USA

Re: What went wrong with this very short shell script?

Post by cholq » Thu Mar 24, 2016 3:35 pm

If you look at what basename actually does:

Code: Select all

 basename --help
Usage: basename NAME [SUFFIX]
or: basename OPTION... NAME...
Print NAME with any leading directory components removed.
If specified, also remove a trailing SUFFIX.


So, the command without any extra parameters simply removes the path prior to the name of the file. If I type the following:

Code: Select all

 basename ~/Pictures/palemoon.png 
I get "palemoon.png"

If I type:

Code: Select all

basename ~/Pictures/palemoon.png .png
then I get the answer you were looking for, which is just "palemoon", without the file extension.

User avatar
ganamant
Level 4
Level 4
Posts: 383
Joined: Sun Mar 29, 2015 4:08 pm

Re: What went wrong with this very short shell script?

Post by ganamant » Sat Apr 16, 2016 3:42 pm

You do not need basename at all for a ting like this. You will be fine with parameter substitution.
I'm assuming you don't use spaces in your names (you shouldn't, by the way), but if you do, double quote the variable calls.

Code: Select all

for i in *.jpg; do j=${i%jpg}png; echo mv $i $j; done
When you are sure the script does what you need, remove the 'echo' and let mv do it's job.

parsma
Level 1
Level 1
Posts: 36
Joined: Sun Aug 22, 2010 3:00 am

Re: What went wrong with this very short shell script?

Post by parsma » Mon May 09, 2016 1:46 pm

In this particular case, even though you might not have any use for the info, it is easier to use the rename Perl script.
Do:

Code: Select all

which rename
...to see if you have it. After that it's just a matter of:

Code: Select all

man rename
...to get going.
It's something like:

Code: Select all

rename 's/\.jpg/\.png/' *.jpg -n
The -n option is a safety valve, so to speak. It will make the script print what would happen if you didn't include it, as opposed to actually doing it.
I use it all the time for stuff I need done.
I do not believe in supernatural beings. Therefore: http://atheism.org/

Post Reply

Return to “Scripts & Bash”