LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   file renaming with shell script or ? (https://www.linuxquestions.org/questions/linux-general-1/file-renaming-with-shell-script-or-347655/)

XJNick 07-28-2005 09:51 AM

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? :confused:

Thanks,

Quigi 07-28-2005 10:46 AM

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"

oneandoneis2 07-28-2005 11:01 AM

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. . .

XJNick 07-28-2005 02:08 PM

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 :D

Thanks!

XJNick 07-28-2005 02:10 PM

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,

oneandoneis2 07-29-2005 02:43 PM

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


All times are GMT -5. The time now is 09:17 AM.