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/
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
With a modern find that supports -lname:
find /usr/local/bin -lname '/usr/local/texlive/*' -delete
should do it.
Method 3
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
Method 4
With zsh:
rm -f /usr/local/bin(@e'{[[ $REPLY:P = /usr/local/texlive/* ]]}')
$REPLY:P fully resolves the path to a symlink-free one, so assuming /usr/local/texlive is symlink free itself, it would remove all files which after symlink resolution live under /usr/local/textlive which would include links to /usr/local/texlive/foo but also to ../texlive/bar or to /usr/./local/texlive/whatever or to /some/other/symlink which is itself a symlink pointing in /usr/local/texlive, etc.
Method 5
Go to path your and set yourself path configuration
ls -alh|grep "your-pattern-to-file-or-folder-for-symlink"| awk '{print $9}'|xargs rm -rf
Method 6
sudo unlink /path/of/your/package
thats it
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0