Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

With the command:

ls -la *

I can list all my symbolic links.

How can I remove all symbolic links which are linked to a special folder?

For example:

In my directory usr/local/bin I have the following entries:

lrwxrwxrwx 1 root root 50 Apr 22 14:52 allneeded -> /usr/local/texlive/2011/bin/x86_64-linux/allneeded
lrwxrwxrwx 1 root root 47 Apr 22 14:52 amstex -> /usr/local/texlive/2011/bin/x86_64-linux/amstex
lrwxrwxrwx 1 root root 24 Apr 23 19:09 arara -> /home/marco/.arara/arara

Now I want to remove all links with the path /usr/local/texlive/

share|improve this question
1  
Do you mean remove all symlinks linked to a target folder? Or move all found symlinks into a specific folder? –  uther May 25 '12 at 13:12
    
@uther: I mean removing the links. –  Marco Daniel May 25 '12 at 13:18
add comment

3 Answers

up vote 11 down vote accepted

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
share|improve this answer
    
Does this command remove all symlinks? Please see my edit above.. –  Marco Daniel May 25 '12 at 13:29
    
@Marco: yes, the first three lines would. Editing more :) –  0xC0000022L May 25 '12 at 13:30
add comment

With a modern find that supports -lname:

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

should do it.

share|improve this answer
1  
That is a great and simple solution. I tested and it works like a charm. Thanks –  Marco Daniel May 25 '12 at 15:53
    
Presumably that should be find /usr/local/bin –  James Youngman May 25 '12 at 22:39
    
Not even modern, necessarily. GNU find has had -lname for longer than I have maintained it (since around 2003). –  James Youngman May 25 '12 at 22:43
    
@JamesYoungman: Thanks! By modern find i really meant GNU find. ;) I used usr/local/bin, because that's what the OP used. –  Christoffer Hammarström May 25 '12 at 22:52
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.