0

I need to do this on a mac/linux:

  • Given: some directory such as /usr/local/lib/node_modules
  • Find: all links that are symbolic links to any files within the given directory

I would like to remove those links since the directory does not exist anymore and provide a cleaner working environment.

Shaun
  • 4,057
  • 7
  • 38
  • 48

1 Answers1

0

You can do this with the answer to this question:

Linux: Find all symlinks of a given 'original' file? (reverse 'readlink')

The basic idea is that you are doing an ls -al on every directory and grepping the contents to see if it contains the directory as a target of the link (instead of file as in previous answer). Just replace /usr/share/applications with your directory, and the .* matches all files or subfolders. The \-> ensures it matches link targets instead of normal filenames.

Example:

pax$ find / -exec ls -ald {} ';' 2>/dev/null | grep '\-> /usr/share/applications.*' lrwxrwxrwx 1 pax pax 23 2010-06-12 14:56 /home/pax/applications_usr_share -> /usr/share/applications

Halcyon
  • 1,376
  • 1
  • 15
  • 22