Web Hosting Talk







View Full Version : Any idea?


Matt2001
08-29-2006, 10:15 PM
I've got a folder full of 1600 images, with filenames such as 123456_123_11.jpg

I need to turn that filename into 123456_1.jpg

How do I truncate the filename in bash? Here's what i've got so far:


ls *.jpg | awk '{print("cp -f "$i" imagetest/"$i".jpg")}' | /bin/sh

How do I truncate $i ?

thewarrior
08-29-2006, 11:22 PM
Do u want the code in Perl or PHP?

Domainitor
08-29-2006, 11:46 PM
How about

for i in *_*_*.jpg; do echo ${i}; mv ${i} `awk -F"_" '{ print $1 "_" $3 }'`; done

Throw an echo in front of the mv to see what it'll do before you run it for real.... I just keyed that without testing....

In case it's hard to read on your screen, the mark before awk is a tick (to the left of the 1 on the keyboard). The {...} section is quoted with half-quotes (next to the Enter key) and there's another tick after the half quote that follows the }. The double quotes are just plain double quotes.

Good luck!

thewarrior
08-29-2006, 11:48 PM
was the code in php?

Domainitor
08-30-2006, 12:51 AM
Mat2001 said he wanted something in bash....

BostonGuru
08-30-2006, 10:55 AM
Save this script to a php file, then run from a shell using 'php scriptname.php'.


<?php
////Insert path to image folder
$imageDir = '/path/to/file';
////Get array of files in directory
$d = dir("$imageDir");
while (false !== ($entry = $d->read()))
{
$images[] = $entry;
}
$d->close();
////for each file truncate to 8 characters and rename
for($i=0;$i<sizeof($images);$i++)
{
$oldFile = $images[$i];
$newFile = substr("$oldFile",0,8);
$newFile .= ".jpg";
$copyCommand = 'mv '.$imageDir.'/'.$oldFile.' '.$imageDir.'/'.$newFile;
exec("$copyCommand");
}
?> This should work. Can somebody else double check this for errors?

osteoarthitis
08-30-2006, 04:22 PM
if these files are physically present then just use acdsee the simplest way to do this .:eek:

discobean
08-30-2006, 07:22 PM
eiww... why would u do it in php if the bash D provided need no programming!

BostonGuru
08-30-2006, 09:00 PM
Because all I know is PHP....