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";

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?


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.