Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I know how to know if a file or dir is a symbolic link. But how can I know that the target does NOT exist ? Ex : ln1 ...
  1. #1
    xuo
    xuo is offline
    Just Joined!
    Join Date
    Oct 2006
    Posts
    29

    How to find "dead" symbolic links ?

    Hi,

    I know how to know if a file or dir is a symbolic link. But how can I know that the target does NOT exist ?

    Ex :
    ln1 -> tutu
    ln2 -> titi
    titi does not exist, tutu does. tutu and toto can be either dir or file. How can I make the difference between the 2 symbolic links ? I would like to use a tcsh script with simple commands such as ls, find ...

    I think it is possible with find but I did not succeed to make it work.
    I had another idea which was :
    if the target is a dir and that I try to "cd" inside but can't, then the link is dead. But how to do if the target is a file ? Moreover, doing "cd" in each possible symbolic link and going back to current dir can be very long. This is a possible workaround but not a good idea.

    Regards.

    Eric.

  2. #2
    Just Joined! Smither's Avatar
    Join Date
    Feb 2006
    Location
    Scotland
    Posts
    58
    file /symbolic/link/

    If it's broken it'll say something along the lines of:
    /usr/bin/ear: broken symbolic link to `/usr/lib/erlang/bin/ear'

    This searches my system for them:
    Code:
    for i in `find /`; do if (test -h $i); then file $i|grep broken; fi; done
    Make sure you check it yourself instead of piping it to something else - a symbolic link with broken in the name will show too.

  3. #3
    xuo
    xuo is offline
    Just Joined!
    Join Date
    Oct 2006
    Posts
    29
    Hi,

    I did not know this command. I think it will help me a lot.

    Thank you.

    Eric.

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    find / -type l ! -exec test -r {} \; -print

  5. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Observations: these two methods differ in two ways. As noted, the first may produce false positives if the pathname contains the string "broken". The first is also much slower. I benchmarked the two on a system of about 12K directories and 120K files:
    Code:
    real    5m24.875s
    user    0m22.586s
    sys     4m45.621s
    
    real    0m19.110s
    user    0m0.893s
    sys     0m2.875s
    Except for the anomalies mentioned above, both produced the same results. The method posted by ghostdog74 is far faster ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  6. #6
    xuo
    xuo is offline
    Just Joined!
    Join Date
    Oct 2006
    Posts
    29
    Hi,

    Thank you to all of you. I'll try both of them. This will help me to understand these options of the find command that I didn't know.

    Regards.

    Eric.

  7. #7
    Just Joined!
    Join Date
    Apr 2008
    Posts
    3

    Use the find command

    The following command finds only broken symbolic links:

    find -L -type l

    This works because "-L" causes find to dereference symbolic links, in which case only broken links will have type "l". This is probably unique to gnu find (from findutils)

    more info:
    ynform.org slash w slash Pub slash UnixCommandEquivalents

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...