[Chapter 16] 16.28 oldlinks: Find Unconnected Symbolic Links
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:38:07Z

#

| Chapter 16 Where Did I Put That? |

## 16.28 oldlinks: Find Unconnected Symbolic Links

One problem with symbolic links is that they're relatively "fragile" (18.6). The link and the file itself are different kinds of entities; the link only stores the name of the "real" file. Therefore, if you delete or rename the real file, you can be left with a "dead" or "old" link: a link that points to a file that doesn't exist.

This causes no end of confusion, particularly for new users. For example, you'll see things like this:

>     % ls nolink
>     nolink
>     % cat nolink
>     cat: nolink: No such file or directory

The file's obviously there, but cat tells you that it doesn't exist.

There's no real solution to this problem, except to be careful. One way to be careful is to write a script that checks links to see whether or not they exist. Here's one such script from Tom Christiansen; it uses find to track down all links, and then uses perl (37.1) to print the names of links that point to nonexistent files.

>     #!/bin/sh
>     find . -type l -print | perl -nle '-e || print'

The script only lists "dead" links; it doesn't try to delete them or do anything drastic. If you want to take some other action (like deleting these links automatically), you can use the output of the script in backquotes (9.16). For example:

>     % rm `oldlinks`

- ML

16.27 newer: Print the Name of the Newest File |  | 16.29 sls: Super ls with Format You Can Choose