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?

rubo77
  • 28,966

1 Answers1

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).

rubo77
  • 28,966
Chris Johnsen
  • 20,101