Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to test a symlink target for directoryhood

104 views
Skip to first unread message

rea...@newsguy.com

unread,
Jan 10, 2008, 1:16:31 PM1/10/08
to begi...@perl.org
I'm working on a little script that will generate an html page from a
directory full of symlinks to target directories. However in some
cases there may be symlinks that point to regular files.

How can perl tell the difference. Or rather how can I test the
symlink to see if it points at a real openable directory.

Looking through the `stat' function, I'm not imagining a way to get at the
target of the symlink for a test.

rea...@newsguy.com

unread,
Jan 10, 2008, 1:55:22 PM1/10/08
to begi...@perl.org
rea...@newsguy.com writes:

Oops.. daydreaming and forgot lstat. But still for expressions where
stat is used like:
my @dir = grep { /^\w/ && -d "./$_" } readdir(DIR);

How would I check if the item passing the -d test is really a symlink.
Or maybe it would be better to find all the symlinks first and see if
they are directories.

The aim here is to establish if symlinks are pointing to directories
or if the directory is a normal directory.

Once that is established, then we want to test if there is an
`index.htm[l]' behind the directories.

So first establish if we are dealing with a real directory or symlink
Note:
(the first information is for admins use, Not for more coding in the
perl script so it can just be printed and forgotten. (admin trying
to setup a few directories that house symlinks to a host of other
things.)

The second part is for further coding, generate a formatted html page
with the index.html files setup as a list of hrefs.
(Not asking for help on that part (as yet anyway))

Chas. Owens

unread,
Jan 10, 2008, 1:52:06 PM1/10/08
to rea...@newsguy.com, begi...@perl.org
snip

The -X operators follow the symlink.

if (-d $symlink && -r $symlink) {
print "$symlink points to a readable directory\n";
print "and you can chdir to it\n" if -x $symlink;
} else {
print "$symlink is not a directory or it is not readable\n";
}

rea...@newsguy.com

unread,
Jan 10, 2008, 1:56:42 PM1/10/08
to begi...@perl.org
"Chas. Owens" <chas....@gmail.com> writes:

Ok thanks... I'd already posted a followup on my own post before I saw
yours.. you've answered all I needed for now... thanks again.

rea...@newsguy.com

unread,
Jan 10, 2008, 2:06:34 PM1/10/08
to begi...@perl.org
"Chas. Owens" <chas....@gmail.com> writes:

> The -X operators follow the symlink.

Where is there a list of the -X operators
perldoc perlop doesn't mention it. Or at least
/\-X in that page doesn't find anything.

> if (-d $symlink && -r $symlink) {
> print "$symlink points to a readable directory\n";
> print "and you can chdir to it\n" if -x $symlink;
> } else {
> print "$symlink is not a directory or it is not readable\n";
> }

So first how can I tell they are symlinks or not.

This doesn't do it after pullin all dir in to @dir
for(@dir){
if(lstat $_){ print "$_ is symlink\n";
}
}

It prints both symlinks and real directories.

John W. Krahn

unread,
Jan 10, 2008, 2:17:34 PM1/10/08
to Perl Beginners

The -l test will tell you if a file is a symlink and readlink() will
read the contents of that symlink.

if ( -l $file && -d readlink $file ) {
print "$file is a symlink that points to a directory\n";
}

John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Chas. Owens

unread,
Jan 10, 2008, 4:26:22 PM1/10/08
to rea...@newsguy.com, begi...@perl.org

They are listed in perlfunc.

perldoc -f -X

from perldoc perlfunc
-l File is a symbolic link.

rea...@newsguy.com

unread,
Jan 10, 2008, 5:42:52 PM1/10/08
to begi...@perl.org
"Chas. Owens" <chas....@gmail.com> writes:

> perldoc -f -X

Ack... I was trying perldoc -f X

rea...@newsguy.com

unread,
Jan 10, 2008, 5:43:33 PM1/10/08
to begi...@perl.org
"John W. Krahn" <kra...@telus.net> writes:

> The -l test will tell you if a file is a symlink and readlink() will
> read the contents of that symlink.
>
> if ( -l $file && -d readlink $file ) {
> print "$file is a symlink that points to a directory\n";
> }

Ahh nice... thanks

0 new messages