Find symbolic links

posted on 08:53 AM on Friday 08 August 2014

Prior to removing a directory, it might be interesting to see if there are any symbolic links within it. The following is a short piece of Python code to do this.

#!/usr/bin/env python

import os

for root, dirnames, filenames in os.walk("."):
  for dirname in dirnames:
    if os.path.islink(os.path.join(root, dirname)):
      print os.path.join(root, dirname), " => ", os.readlink(os.path.join(root, dirname))
  for filename in filenames:
    if os.path.islink(os.path.join(root, filename)):
      print os.path.join(root, filename), " => ", os.readlink(os.path.join(root, filename))

bernett.net