0

I have a photo directory with hundreds of files named in the format:

2013-08-02 19.21.27.jpg

I wish to keep the first 10 characters (date) and append "-001", etc. being consecutive for each date.

The output would be:

2013-08-02-001.jpg
2013-08-02-002.jpg
2013-08-03-001.jpg

etc.

I then plan to resize the jpg's using something like:

for file in *.jpg; do convert $file -resize 900x900\> 900x900-$file; done
1

The easiest way to do it would probably be to use Perl's hashes:

perl -e '
    for (<*.jpg>){
        $date = substr($_,0,10);
        $counts{$date}++;
        rename $_ => "$date-".sprintf "%03d",$counts{$date}.".jpg";
    }
'

Careful, though, this code is untested. Try it on dummy files first.

  • Fantastic. This worked, but not without complaining: for (<*.jpg>){ > > $date = substr($_,0,10); > > $counts{$date}++; > > rename $_ => "$date-".sprintf "%03d",$counts{$date}; > > } > ' syntax error at -e line 3, near ">" syntax error at -e line 4, near "++;" Execution of -e aborted due to compilation errors. The file names changed as intended and I only had to add ".jpg" to get the finished product. What a time saver, Thanks very much. – user52478 Nov 16 '13 at 14:00
  • @user52478 Right, I missed the .jpg part (adding to answer) but the file names shouldn't have changed if execution were indeed aborted. Strange... Are you sure you copied the code exactly as is? – Joseph R. Nov 16 '13 at 14:58
  • After that initial complaint, I broke up your commands, entering them in three parts, a) perl -e ', b) for etc., and c) the final '. So the end result was the application of the code twice. This may explain the result. – user52478 Nov 16 '13 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.