start page | rating of books | rating of authors | reviews | copyrights

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 16.27 newer: Print the Name of the Newest File Chapter 16
Where Did I Put That?
Next: 16.29 sls: Super ls with Format You Can Choose
 

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


Previous: 16.27 newer: Print the Name of the Newest File UNIX Power ToolsNext: 16.29 sls: Super ls with Format You Can Choose
16.27 newer: Print the Name of the Newest File Book Index16.29 sls: Super ls with Format You Can Choose

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System