As Joost++ points out, the problem in your code is that it checks only for text files (-T), hence not including your executable binary.

Also, if you're trying to create the link on the parent directory, the destination string should read "/home/test/", not "/home/folder" (you should probably not hardcode it like that either, as any typo can bring unexpected bugs like this one. Use you $dir variable! :-)

But what I would really warn you about is on putting the returned value of symlink() back on $file. Not only it destroys your filename for further use, the variable name differing from what it actually does (or any multi-use variable at any rate) will most likely confuse you if your program escalates.

Even worse, if a user tries to run your program on a filesystem that does not support symbolic links, it will cause a fatal error unless you trap it in an eval block. You could do something like:

#!/usr/bin/perl use strict; use warnings; my $dir = "/home/test/folder"; opendir(BIN, $dir) or die "Can't open $dir: $!\n"; my @array = grep { -f "$dir/$_" } readdir BIN; foreach my $file (@array) { eval { symlink ("$dir/$file","$dir/../$file") }; # links to parent if ($@) { print "could not create symlink for $file\n"; } else { print "success!\n"; } }
Of course, if this is just a simple PoC code where you were not particularly concerned with the pointed issues, feel free to ignore them :-)

Hope this helps!


In reply to Re^2: symbolic links by garu
in thread symbolic links by muizelaar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.