>_ IzzyLab

Hands-on task

This task is step 21 of the course “find and xargs: search and bulk operations”

find: clean dangling links out of the assets

medium~5 min

After several moves, the static files tree /srv/www/assets has symbolic links pointing nowhere at various depths — their targets were deleted long ago. Delete all broken links in the whole tree. Working links (to files and to directories), regular files and directories must not be touched.

What you'll practise:
  • find
  • -xtype l
  • symbolic links
  • broken links
  • -delete

Topic breakdown

A symbolic link stores only the path to its target. If the target is deleted or moved, the link stays and becomes dangling: ls shows it, but it can't be opened. find has two type tests: -type looks at the object itself (for any link that's l), and -xtype at what you get after following the link. A working link to a file gives f, to a directory d, while a dangling link has nowhere to go, so it stays l. Having selected only the dead links this way, you delete them with the -delete action without touching live links or the files themselves.

What's checked

  1. no broken links left
  2. working links in place
  3. files and directories intact

What it looks like