How do I check if a file is a symbolic link to a directory?

4,199

Solution 1

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

Solution 2

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

Solution 3

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
Share:
4,199

Related videos on Youtube

yodatg
Author by

yodatg

Updated on September 18, 2022

Comments

  • yodatg
    yodatg almost 2 years

    I have been given 2 data sets and want to perform cluster analysis for the sets using KNIME.

    Once I have completed the clustering, I wish to carry out a performance comparison of 2 different clustering algorithms.

    With regard to performance analysis of clustering algorithms, would this be a measure of time (algorithm time complexity and the time taken to perform the clustering of the data etc) or the validity of the output of the clusters? (or both)

    Is there any other angle one look at to identify the performance (or lack of) for a clustering algorithm?

    Many thanks in advance,

    • T
  • yodatg
    yodatg over 12 years
    Thank you for your response :-) The data I will be using is a Wine Data Set (chemical analysis of wines grown in italy - archive.ics.uci.edu/ml/machine-learning-databases/wine/…) and a Breast Cancer Data Set - : archive.ics.uci.edu/ml/machine-learning-databases/…. Could you please elaborate on what you mean behind "external" labels vs "internal" quality measure?
  • Has QUIT--Anony-Mousse
    Has QUIT--Anony-Mousse over 12 years
    See Wikipedia: en.wikipedia.org/wiki/… It explains "internal" and "external". But be aware that you are not testing whether or not you have actually discovered something useful new. In particular when using classification data such as the UCI data. Clustering actually is about the discovery of new clusters, not reproducing the known classes (then it would be classification). Ideally, you would manually inspect the found clusters and check if they make any sense to you.
  • rubo77
    rubo77 about 10 years
    If you want to check hidden files and directories too preceed this before: shopt -s dotglob
  • Camilo Martin
    Camilo Martin almost 10 years
    Using find and a while read for that is kinda overkill...
  • rubo77
    rubo77 almost 10 years
    Find is really useful if you search only for certain files