2

In the sardi icon theme I have noticed that there are symbolic links to symbolic links to symbolic links to the original image.

The command

ls -lR . | grep ^l | wc

shows me i have 3511 symbolic links times 5 icons sets. 15,000 links to check.

Is there a (bash) way to show me all the links that point to another symbolic link?

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • 1
    Links, both hard and symbolic, is one way only. If you want to see all links "pointing" to another link, you have to check *all* the links in your entire system and see which ones point to the specified target. Basically you need to do `find / -type l` and check each file (which will indeed be several thousands) if they point to the target. – Some programmer dude Jul 16 '16 at 11:36
  • Possible duplicate of: https://stackoverflow.com/questions/6184849/symbolic-link-find-all-files-that-link-to-this-file – Leo Izen Jul 16 '16 at 12:58
  • @LeoIzen I don't think it's a duplicate. – melpomene Jul 16 '16 at 13:47

1 Answers1

0

Here's a solution using find and some bash features:

shopt -s extglob
find . -type l | while read -r; do
    t="$(readlink "$REPLY")"
    if [[ "$t" != /* ]]; then
        t="${REPLY%%*([^/])}$t"
    fi
    [ -L "$t" ] && echo "$REPLY"
done

Or all in one line:

find . -type l | while read -r; do t="$(readlink "$REPLY")"; if [[ "$t" != /* ]]; then t="${REPLY%%*([^/])}$t"; fi; [ -L "$t" ] && echo "$REPLY"; done
  • find . -type l finds all symlinks.
  • Then for each symlink $REPLY, we get its target $t using readlink.
  • If the target is not an absolute name (doesn't start with /), we prepend the dirname from the original path $REPLY (the expression ${REPLY%%*([^/])} removes all trailing non-slash characters).
  • If the target is also a symlink (-L), we output the original name $REPLY.

The extglob extension must be enabled to get *([^/]) to work.

melpomene
  • 84,125
  • 8
  • 85
  • 148