4

I created in /tmp/test/

mkdir somedir
ln -s somedir/ somelink

I want to loop through only directories:

for file in */ ; do 
  if [[ -d "$file" && ! -L "$file" ]]; then
    echo "$file is a directory";
  fi;
done

but why does this show somelink is a directory?

while without the slash it doesent?

for file in * ; do 
...

And is there a difference in zsh?

1
  • Do you use zsh and/or bash? Oct 21, 2013 at 4:44

1 Answer 1

6

The trailing slash in the argument given to -L causes the symbolic link to always be resolved (i.e. at the level of the lstat(2) call). See POSIX.1 Base Definitions, General Concepts, Pathname Resolution, or “Trailing slashes” in Linux’s path_resolution(2).

This is not specific to zsh.

You can use a simple parameter expansion to strip the trailing slash:

[[ … -L "${file%/}" … ]]

The above should work in any Bourne-like shell (ksh, ash, dash, bash, zsh, et cetera).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .