LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-03-2006, 06:36 AM   #1
Kropotkin
Member
 
Registered: Oct 2004
Location: /usr/home
Distribution: Linux Mint, FreeBSD, Android
Posts: 355

Rep: Reputation: 32
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.
 
Old 02-03-2006, 08:03 AM   #2
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 14.1
Posts: 1,469

Rep: Reputation: 46
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...

Last edited by satinet; 02-03-2006 at 08:08 AM.
 
Old 02-03-2006, 08:36 AM   #3
esje
Member
 
Registered: Mar 2004
Location: Holland
Distribution: Debian Etch, Gentoo 2005.1/SUSE 10
Posts: 52

Rep: Reputation: 15
This may come in handy

)It's relatively easy, I just don't want to be held responsible for f-ing up your files )
 
Old 02-03-2006, 08:54 AM   #4
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware 14.1 (32 and 64 bit)
Posts: 1,594

Rep: Reputation: 124Reputation: 124
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.
 
Old 02-03-2006, 08:56 AM   #5
Kropotkin
Member
 
Registered: Oct 2004
Location: /usr/home
Distribution: Linux Mint, FreeBSD, Android
Posts: 355

Original Poster
Rep: Reputation: 32
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.
 
Old 02-03-2006, 08:59 AM   #6
Kropotkin
Member
 
Registered: Oct 2004
Location: /usr/home
Distribution: Linux Mint, FreeBSD, Android
Posts: 355

Original Poster
Rep: Reputation: 32
Thumbs up

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.
 
Old 02-03-2006, 09:03 AM   #7
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 14.1
Posts: 1,469

Rep: Reputation: 46
is my post what you were looking for?

hope it helped!

back to hp-ux....
 
Old 02-03-2006, 09:27 AM   #8
Kropotkin
Member
 
Registered: Oct 2004
Location: /usr/home
Distribution: Linux Mint, FreeBSD, Android
Posts: 355

Original Poster
Rep: Reputation: 32
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 02:03 AM
cdrecord command-line parentheses "(" ")" coolingtower Linux - Software 1 10-28-2005 12:49 PM
Starting an xterm window that is "always on top" on command line azilkie Fedora 1 10-07-2005 10:17 PM
Bash: "after" or "waitfor" command enemorales Programming 3 06-15-2005 01:04 AM
lrz won't send files, command line "gummed up" odat Linux - Software 0 05-23-2004 05:35 PM


All times are GMT -5. The time now is 12:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration