How can I remove all symbolic links with a special target?

Please make sure to read the alternative answer. It's even more to the point although not voted as high at this point.

You can use this to delete all symbolic links:

find -type l -delete

with modern find versions.

On older find versions it may have to be:

find -type l -exec rm {} \;
# or
find -type l -exec unlink {} \;

To limit to a certain link target, assuming none of the paths contain any newline character:

 find -type l | while IFS= read -r lnkname; do if [ "$(readlink '$lnkname')" == "/your/exact/path" ]; then rm -- "$lnkname"; fi; done

or nicely formatted

 find -type l |
 while IFS= read -r lnkname;
 do
   if [ "$(readlink '$lnkname')" = "/your/exact/path" ];
   then
     rm -- "$lnkname"
   fi
 done

The if could of course also include a more complex condition such as matching a pattern with grep.


Tailored to your case:

find -type l | while IFS= read -r lnk; do if (readlink "$lnk" | grep -q '^/usr/local/texlive/'); then rm "$lnk"; fi; done

or nicely formatted:

find -type l | while IFS= read -r lnk
do
  if readlink "$lnk" | grep -q '^/usr/local/texlive/'
  then
    rm "$lnk"
  fi
done

With a modern find that supports -lname:

find /usr/local/bin -lname '/usr/local/texlive/*' -delete

should do it.


The find solution is great.

Just in case your find doesn't support -lname, here's another way that uses only shell and readlink.

cd /usr/local/bin
for f in *; do
  case "$(readlink "$f")" in /usr/local/texlive/*)
    rm "$f"
    ;;
  esac
done