LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash question: how to do "for a in b do..." on the command line (https://www.linuxquestions.org/questions/linux-newbie-8/bash-question-how-to-do-for-a-in-b-do-on-the-command-line-411201/)

Kropotkin 02-03-2006 05:36 AM

bash question: how to do "for a in b do..." on the command line
 
For a web project, I need to generate PNG thumbnails from a large number of JPG files.

Alas,
Code:

convert -thumbnail 150x150 *.jpg *.png
doesn't quite do what I want it to do; the PNG thumbnails get generated but the filename gets mangled in the process, and I end up with a lot of files named "*-02.png".

Back in DOS-OS/2 days, I would have used something like this on the command line:
Code:

for %1 in (".jpg") do convert -thumbnail %1 %1.png
and it would have generated PNGs with the same filenames as the JPGs.

What is the quivalent in bash?

I am sure this is documented in many places in the web, but it is one of those things that is hard to search for.

satinet 02-03-2006 07:03 AM

i'm not sure what it is u are actually trying to do....
How do you convert it??

but

#! /bin/bash

for each in *.png
do
name=`echo $each|cut -d "." f1`
convert -thumbnail 150x150 $each $name.png
done

the <convert> is however you mean to do the conversion. not my field sorry..... not sure what routine u are using.

that's the syntax for a do expression though
'each' is the variable. you can use anything you want.
this is presumming u are in the same direcotry as the files...

esje 02-03-2006 07:36 AM

This may come in handy

)It's relatively easy, I just don't want to be held responsible for f-ing up your files ;))

titopoquito 02-03-2006 07:54 AM

Quote:

Originally Posted by satinet
name=`echo $each|cut -d "." f1`

An addition: This will just work if you have exactly one period in the file name. If they have all the same extension you could use

Code:

name=`basename "$each" .jpg`
which will just strip off the extension and should not care about the rest of the file name.

Kropotkin 02-03-2006 07:56 AM

Quote:

Originally Posted by satinet
How do you convert it??

using convert. "$ man convert" -->

Code:

[...]
OVERVIEW
The convert program is a member of the ImageMagick(1) suite
of tools.  Use it to convert between image formats as well as
resize an image, blur, crop, despeckle, dither, draw on, flip,
join, re-sample, and much more.
[...]

"$ rpm -q ImageMagick" -->
Code:

Name        : ImageMagick                  Relocations: (not relocatable)
Version    : 6.2.3.0                          Vendor: The KDE-RedHat Project
Release    : 0.1.fc4.kde                  Build Date: Thu 16 Jun 2005 08:00:32 PM CEST
Install Date: Thu 04 Aug 2005 10:13:03 PM CEST      Build Host: sting-rh73.unl.edu
Group      : Applications/Multimedia      Source RPM: ImageMagick-6.2.3.0-0.1.fc4.kde.src.rpm
Size        : 8227099                          License: freeware
Signature  : DSA/SHA1, Thu 16 Jun 2005 08:55:34 PM CEST, Key ID efe4780cff6382fa
Packager    : kde-redhat Developers <http://kde-redhat.sf.net/>
URL        : http://www.imagemagick.org/
Summary    : An X application for displaying and manipulating images.
Description :
ImageMagick(TM) is an image display and manipulation tool for the X
Window System. ImageMagick can read and write JPEG, TIFF, PNM, GIF,
and Photo CD image formats. It can resize, rotate, sharpen, color
reduce, or add special effects to an image, and when finished you can
either save the completed work in the original format or a different
one. ImageMagick also includes command line programs for creating
animated or transparent .gifs, creating composite images, creating
thumbnail images, and more.


Kropotkin 02-03-2006 07:59 AM

Thanks, that was what I was looking for.

Quote:

Originally Posted by esje
It's relatively easy, I just don't want to be held responsible for f-ing up your files ;))

Not to worry my friend. I test everything first on copies of my files in a temporary directory.

satinet 02-03-2006 08:03 AM

is my post what you were looking for?

hope it helped!

back to hp-ux....

Kropotkin 02-03-2006 08:27 AM

Got it
 
Ok, this does what I want:
Code:

for i in `ls *jpg`; do convert -thumbnail 150x150 $i `basename "$i" .jpg`.png; done
It creates a 150px PNG thumbnail from every JPG in the current directory.

Thanks everyone.


All times are GMT -5. The time now is 11:37 PM.