Penggu has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I wrote a small piece of code which ideally (to my understanding anyway) be working. Basically what it does (supposed to) is recurse folders from the current dir, making a symlink in each one to a file given on the command line. It's working at the moment, but it wont go deeper then one level, although output says otherwise.
Here's the code:

#!/usr/bin/perl use File::Find; if (!-f $ARGV[0]) { die "File \"$ARGV[0]\" does not exist"; } sub wanted { if (length $_ == 1) { return; } if (-d) { $temp = '../' x (split(/\//,$File::Find::name) - 1).$ARGV[0]; $target = $File::Find::name.'/'.$ARGV[0]; print $target," -> ",$temp,"\n"; symlink($temp,$target); } } find(\ &wanted,'.');
The output seems fine:
shell# ./recurlink.pl index.php
./Automotive/index.php -> ../index.php
./Automotive/Mechanics/index.php -> ../../index.php
./Banking_and_Finance/index.php -> ../index.php
./Fast_Food_and_Groceries/index.php -> ../index.php
./Fast_Food_and_Groceries/Take_Away_Shops/index.php -> ../../index.php
./House_and_Building/index.php -> ../index.php
./Educational_Institutions/index.php -> ../index.php
shell#
but on inspection, only the 1st level symlinks are created. none of the others exist.
any help muchly appreciated.
thanks

Replies are listed 'Best First'.
Re: recursive directory linking
by cwest (Friar) on Jul 28, 2000 at 23:25 UTC
    Error check much?
    symlink $temp, $target or die $!; # or print symlink $temp, $target ? $target."-> ".$temp."\n" : $!;
    --
    Casey
    
RE: recursive directory linking
by Anonymous Monk on Jul 29, 2000 at 00:34 UTC

    Although I applaud the aggressive attempt to use perl for this problem, you might consider using find(1).

    % find ./ -type d -exec ln -s some file {} \;
    
      That will work only if some file is an absolute path name, which isn't quite what's indicated. To get the relative paths per the example, you'd need to append "../" to the beginning of the file name for each level deep you are in the directory tree--a bit of a trick using find, but a snap for perl...
      I'd recommend looking into the File::Find module. This is an excellent way to recursively process files in a directory - I've written a very similar script in the past using it.

      -- Kirby

      Tuxtops: Laptops with Linux!

Re: recursive directory linking
by Penggu (Initiate) on Jul 29, 2000 at 18:02 UTC
    thanks for your replies guys

    i think ill stick to recurcopy.pl for now.. for some reason recurlink.pl refuses to link further than 1 level saying no such file or directory when there actually is....

    maybe ill learn to use perl -d later (when i run out of web space?), after i get the site going..

    Cheers,
    Penggu