111

I can check, if a file exists and is a symbolic link with -L

for file in *; do
    if [[ -L "$file" ]]; then echo "$file is a symlink"; else echo "$file is not a symlink"; fi
done

and if it is a directory with -d:

for file in *; do
    if [[ -d "$file" ]]; then echo "$file is a directory"; else echo "$file is a regular file"; fi
done

But how can I test for only links to directories?


I simulated all cases in a test folder:

/tmp/test# ls
a  b  c/  d@  e@  f@

/tmp/test# file *
a: ASCII text
b: ASCII text
c: directory
d: symbolic link to `c'
e: symbolic link to `a'
f: broken symbolic link to `nofile'
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
rubo77
  • 27,777
  • 43
  • 130
  • 199

3 Answers3

128

Just combine the two tests with &&:

if [[ -L "$file" && -d "$file" ]]
then
    echo "$file is a symlink to a directory"
fi

Or, for POSIX compliant-syntax, use:

if [ -L "$file" ] && [ -d "$file" ]
...

Note: the first syntax using [[ expr1 && expr2 ]] is valid, but only works in certain shells such as ksh (where it comes from), bash or zsh. The second syntax using [ expr1 ] && [ expr2 ] is POSIX-compliant and even Bourne-compatible, meaning it will work in all modern sh and sh-like shells

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • 1
    If you want to check hidden files and directories too preceed this before: `shopt -s dotglob` – rubo77 May 29 '14 at 16:04
14

Here is a single command which will recursively list symlinks whose target is a directory (starting in the current directory):

find . -type l -xtype d

Reference: http://www.commandlinefu.com/commands/view/6105/find-all-symlinks-that-link-to-directories

Mark Edington
  • 503
  • 4
  • 11
1

A solution with find and using a function:

dosomething () {
    echo "doing something with $1"; 
}
find -L -path './*' -prune -type d| while read file; do 
    if [[ -L "$file" && -d "$file" ]];
        then dosomething "$file";
    fi; 
done
rubo77
  • 27,777
  • 43
  • 130
  • 199