Debugger
Debugger

Reputation: 532

how to make symbolic link between file in folders using perl?

here is the scenario.

I have dir1-->file1
          |-->file2
          |-->subdir1--->file3
          |         |---> file4 
          | 
          |-->file1
          |-->subdir2-->file6

also dir2 different file tree...

I need to symlink above file in /path/dir1 and /path/dir2 to anthor path call /newpath/dir1 and /newpath/dir2..

is it possible?

So far i tried below

#!/usr/bin/perl

use strict;
use File::Find qw(find);
my $path = "path";


find(\&Search, $path);

sub Search{
    my $filename = $File::Find::name;

    if(-f $filename){
     symlink("$filename", "path2");
     }
     }

Upvotes: 1

Views: 406

Answers (2)

Debugger
Debugger

Reputation: 532

Finally i went with below method.

    my $dir = "/path/";
    my $smlinkdir = "/smlink/path/";
    chdir($smlinkdir) or die "Cant chdir to $smlinkdir $!";
    if(-d $smlinkdir) {
    system(cp, '--recursive', '--preserve=all', '--no-dereference', '--symbolic-link', "$dir", "$smlinkdir");
    } else {
    exit 1
    }

this worked as well and i went with this since this gives the output i expected.

Upvotes: 0

James Anderson
James Anderson

Reputation: 27478

symlink($filename, "path2/" . $_);

Should do the trick. Always assuming "path2" is the directory where you want the links to be place.

Upvotes: 2

Related Questions