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


Click here to return to the 'better script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
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 | # ]