LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 07-28-2005, 09:51 AM   #1
XJNick
Member
 
Registered: Dec 2003
Location: Texas
Distribution: Mandriva, SuSE
Posts: 79

Rep: Reputation: 15
file renaming with shell script or ?


Hi,

Here's the situation. I use a lot of JPEG images on my website. Before I put each image online, I generate a thumbnail image for each full size image. I do a whole directory of images at a time using imagemagick with a command like:

Code:
for img in *.jpg; do convert -resize 144x109 -interlace line -quality 85 $img t$img; done
So a thumbnail of each image is created with the same filename but a "t" prefix (to create them as new files). However, I'd rather continue to use a suffix of "_t" at the end of the filename to denote that its a thumbnail (when I started my site I used a Windows app to make all the thumbnails). For example, I'd like to have image1.jpg and its thumbnail image1_t.jpg

I've yet to find a way to do this with the Linux shell. What I've been doing for now is manually renaming all the files to remove the "t" prefix and add a "_t" suffix to the filename (i.e. timage1.jpg to image1_t.jpg ). Is there a way to automate this process with a shell script or by modifying the shell command above? Or am I crazy to think that there is an easy way to remove the first letter of a filename and insert two letters between the end of the filename and the extension?

Thanks,
 
Old 07-28-2005, 10:46 AM   #2
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
First, read the "convert" man page. I wouldn't be surprised if it did the whole thing without an external loop in the shell.

Yes, you can modify your loop, as follows:
Code:
for img in `ls *.jpg | cut -d. -f1`; do
  convert -resize 144x109 -interlace line -quality 85 $img.jpg ${img}_t.jpg
done
This way, $img only contains the basename. You can also shorten the last two into ${img}{,_t}.jpg to reduce repetition.

This assumes your file names don't contain any periods before ".jpg"
 
Old 07-28-2005, 11:01 AM   #3
oneandoneis2
Senior Member
 
Registered: Nov 2003
Location: London, England
Distribution: Ubuntu
Posts: 1,460

Rep: Reputation: 46
A couple of renames should work:

rename .jpg _t.jpg t*.jpg

will add _t to the end of all the t-prefixed images, for example. . .
 
Old 07-28-2005, 02:08 PM   #4
XJNick
Member
 
Registered: Dec 2003
Location: Texas
Distribution: Mandriva, SuSE
Posts: 79

Original Poster
Rep: Reputation: 15
Hi,

Quote:
Originally posted by Quigi
Yes, you can modify your loop, as follows:
Code:
for img in `ls *.jpg | cut -d. -f1`; do
  convert -resize 144x109 -interlace line -quality 85 $img.jpg ${img}_t.jpg
done
This way, $img only contains the basename. You can also shorten the last two into ${img}{,_t}.jpg to reduce repetition.
[/B]
Cool! This seems to work just fine. I figured there was some little trick I was missing

Thanks!
 
Old 07-28-2005, 02:10 PM   #5
XJNick
Member
 
Registered: Dec 2003
Location: Texas
Distribution: Mandriva, SuSE
Posts: 79

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by oneandoneis2
A couple of renames should work:

rename .jpg _t.jpg t*.jpg

will add _t to the end of all the t-prefixed images, for example. . .
Yes, but is it possible to get rename to remove part of the filename (i.e. the "t" prefix)? I couldn't figure out how to accomplish that.

Thanks,
 
Old 07-29-2005, 02:43 PM   #6
oneandoneis2
Senior Member
 
Registered: Nov 2003
Location: London, England
Distribution: Ubuntu
Posts: 1,460

Rep: Reputation: 46
Sure: rename t "" * replaces the first "t" with nothing, i.e. deletes it
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script: File Manipulation simon_qwl Programming 2 10-29-2005 03:40 PM
shell renaming script paskari Programming 3 09-24-2005 02:15 PM
directory renaming shell script? Apostasy Linux - General 15 02-22-2005 06:50 AM
Need help with shell script - renaming multiple files NiallC Linux - Newbie 25 07-04-2004 10:45 AM
File Maintenance in a shell script? mdscudder Linux - General 1 02-20-2003 05:14 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration