views:

45

answers:

1

I would like to create a new directory that its content is soft links to the the content of an existing directory, and set full permissions for this new directory.

I know how to this is in bash:

mkdir -m a=rwx new_dir
cd new_dir
ln -s /path/to/old/dir/* .

but having some problems with finding the perl equivalent

+2  A: 

How about something like this:

mkdir -m a=rwx new_dir     in perl: ->    mkdir ('new_dir', 0777);
cd new_dir                 in perl: ->    chdir ('new_dir');
ln -s /path/to/old/dir/* . in perl: ->

    use constant OLD_DIR => '/path/to/old/dir';

    for my $oldname (glob(OLD_DIR . '/*')) {
         my $newname = $oldname;
         $newname =~ s/^.*\///s;      # Remove everything up to last "/"
         symlink ($oldname, $newname);
    }

Of course, with Perl, "There's always more than one way to do it".

Adrian Pronk
I'm having trouble with the `for my $oldname (</path/to/old/dir/*>)` part. suppose my old dir path is stored as constant OLD_DIR. What should I put in the `for` header?
David B
Solved using `File::Next` (with `descend_filter => sub { 0 }`)
David B
@David: I've edited the answer to use a constant for OLD_DIR
Adrian Pronk
And for each of mkdir, chdir, and symlink, add ' or die "Error (helpful message here): $!"'
runrig