Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Batch rename image files sequentially via perl script UNIX
I wanted to use the QuickTime "Image Sequence" feature to make rapid MPG movie out of a folder of JPGs. The problem was there were 600 pictures taken with my iSight over seven months, so I used a simple perl script to bulk-rename the files (it actually uses cp, so your original files are untouched). After running the script, open QuickTime, choose File » Open Image Sequence, and select 1.jpg, then the frame rate, etc. Here's an example of the finished product. (I used the iSight auto-capture hint I submitted earlier this year.)

Here is the perl (rename.pl):
#!/usr/bin/perl

$iteration=1;

foreach my $file (`ls *.jpg`) {
    chop($file);
    system("cp $file $iteration.jpg;");
    $iteration++;
    }
[robg adds: I haven't tested this one.]
    •    
  • Currently 3.80 / 5
  You rated: 3 / 5 (5 votes cast)
 
[18,875 views]  

Batch rename image files sequentially via perl script | 12 comments | Create New Account
Click here to return to the 'Batch rename image files sequentially via perl script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automator
Authored by: aptenergy on Aug 29, '07 08:30:58AM

You could also use an Automator script to do it. It's not as Unixy, and it's slightly slower upon first run, but I think it's easier than running a Terminal command on it. Just take the selected files, sort them, and then use the Rename Files action.



[ Reply to This | # ]
Batch rename image files sequentially via perl script
Authored by: mkomitee on Aug 29, '07 08:43:21AM
if yer going to use perl, use perl. if you're going to use shell, use shell :)
i=0; for x in *.jpg; do mv $x $i.jpg; i=$(($i+1)); done


[ Reply to This | # ]
Batch rename image files sequentially via perl script
Authored by: itistoday on Sep 02, '07 09:24:02AM

This is definitely the way to do it. Notice how simple and clean it is, and you don't need to run it through no stinkin' perl interpreter.



[ Reply to This | # ]
better script
Authored by: SOX on Aug 29, '07 08:48:20AM
here's a more compact script, you could even make it an alias. Also rather than copy it just makes a link.

\ls *.jpg | perl -nwe 'chomp; next unless -f $_; $r=sprintf  "%03d",$i++;print `ln "$_" $r.jpg`' 
improvements include

1) allows liberal file names with spaces
2) only renames files, not directories
3) embeds the decimal number in a fixed field of 3 wide, filling in leading zeros.
4) creates a hard link to the file. (add -s to create a soft link)

5) multi-threaded and won't choke on large file lists.

[ Reply to This | # ]

better script
Authored by: lar3ry on Aug 29, '07 11:12:28AM
Why not just use perl?
#! /usr/bin/perl -w

$odir = "output";
$mkdir $odir if (! -d $odir);
$count = 0;

foreach $i (<*.jpg>) {
    next if (! -f $i);
    do {
	$f = sprintf("%s/%06d", $odir, ++$count);
    } while (-e $f );

    symlink $i,$f or die "symlink $i -> $f failed: $^E";
}
  • Uses only Perl
  • Uses symlink rather than hard links
  • Puts all symlinks in "output" subdirectory of current directory
  • No piping necessary
  • Allows up to a million files (need more? Adjust the sprintf format)
  • Allows you to "continue" if you've already created some numbered files in "output"
  • Provides some meaningful error handling
  • Yup, there are probably as many Perl solutions as there are Perl programmers...!


[ Reply to This | # ]
better script
Authored by: merlyn on Aug 29, '07 11:32:00AM

That won't work. Your symlinks in "./output" will look like "foo.jpg" and not "../foo.jpg". That's the tricky thing about relative symlinks... the text of the symlink is appended (sorta) to the current path.



[ Reply to This | # ]
better script
Authored by: SOX on Aug 29, '07 06:32:24PM

Symlinks are not a great idea in this case because you can't move or delete the originals. And in this case you boofed the symlinks :-)

No piping? Only perl? Surely you jest. Your program pipes too, it just fires up a bash shell from within perl and pipes the output back. plus it opens a buffer file to collect the output in one slug before parsing it. The piping I use is multi-threaded and does not block the perl while the ls runs. Besides you can add a filter or another source of the file name list with the explictly piped arch.





[ Reply to This | # ]
Batch rename image files sequentially via perl script
Authored by: rudedog on Aug 29, '07 09:57:32AM

The Perl distribution actually comes with a perl program called rename that you can use to do all kinds of rename tasks. It isn't installed on OSX by default, but you could get a copy from the perl distribution.

Using rename to do your task is easy:

rename -n 's/^/sprintf("%03d_", $::iter++)/e' *.jpg

The -n flag means "dry run" - it only tells you what it will do without doing it. Using the rename command rather than rolling your own is much safer, because it will never overwrite existing files and with the -n switch you can easily see what's going to happen.



[ Reply to This | # ]
Batch rename image files sequentially via perl script
Authored by: regeya on Sep 03, '07 11:47:33AM

I used this a few months ago to help sanitize a tree I'd copied over from HFS+ to a Linux machine running Netatalk. Netatalk expects some characters to be encoded a certain way, and rename did just the trick.

Anyone know why the OS X Perl distribution doesn't ship with this?



[ Reply to This | # ]
Batch rename image files sequentially via perl script
Authored by: euggie on Aug 29, '07 10:41:58PM

The script will break if your filenames contains spaces or dashes, or if you have something like alias ls='ls -F' in your .bashrc. The stuff suggested by the comments are great, or you can use perl's own readdir and File::Copy or link to roll your own.



[ Reply to This | # ]
Batch rename image files sequentially via perl script
Authored by: Helge33 on Aug 30, '07 04:43:40AM
Somehow predictable that this kind of hint would alert all the *nix and script nerds with even more unreadable lines of code ;-)

Real Mac users, forget all this nonsense with scripting and grab yourself the donation ware "File List"

http://www.manytricks.com/filelist/

which does any kind of renaming with a nice, clean GUI.



[ Reply to This | # ]
Batch rename image files sequentially via perl script
Authored by: regeya on Sep 03, '07 11:45:22AM

Automator can do this, and easily. Alas, I'm not sitting in front of my OS X machine so I can't tell you how I have mine set up; however, it took me maybe a maximum of 30 seconds to set it up, save it, and drop it into the Finder well. Easy cheesy.



[ Reply to This | # ]