1
lrwxrwxrwx.   1 oracle   oinstall    10 Oct 14 03:47 abc -> xyz
drwxr-xr-x.   6 oracle   oinstall     4 Oct 26 21:11 xyz

i want to find that xyz is having a symbolic link with name abc

ilkkachu
  • 138,973

3 Answers3

0

If you want to find all the links named abc to a file xyz are in current directory this could be done with:

find -L / -samefile `readlink -f xyz` -name abc

readlink -f xyz can be replaced with a full path to a file (which it does in fact for the file in current directory).

If you know exact link name, but instead want to find just all the links to a file xyz with any names, you could just omit name like this:

find -L / -samefile `readlink -f xyz`

You can also add 2> /dev/null at the end of the command to suppress "permission denied" warnings.

NStorm
  • 119
  • what if they don't know the name of the link? – ilkkachu Jan 19 '22 at 20:47
  • @ilkkachu OP asked "is having a symbolic link with name abc". I've offered a solution to find links named exactly abc. If he wants to find all links to the file xyz, when the -name abc part of the find command could be just omitted. – NStorm Jan 19 '22 at 21:00
  • the words "to find that" would imply that the fact in question isn't supposed to be known beforehand, but is exactly what they're trying to find out. – ilkkachu Jan 19 '22 at 21:25
  • @ilkkachu maybe you're right, sorry not a native English here. I've updated my answer to reflect both options. – NStorm Jan 20 '22 at 05:32
0

There is no back reference or count of symbolic links maintained to a file or directory.

So, for example, it would be perfectly valid to delete the xyz directory. The presence of any symbolic links wouldn't prevent this, nor would it prevent the data in xyz from being destroyed. Any symbolic links would only be recognised as being invalid if an attempt was made to open them -- they would become "broken links".

The situation would be substantially different for hard links.

Jeremy Boden
  • 1,320
0

With zsh:

print -rC1 - **/*(ND@e['[[ $REPLY -ef xyz ]]'])

Would print raw on 1 Column the files of type symlink (@) with any name (*), under and level of subdirectories (**/), including hidden ones (D), not complaining if there's no match (N), and that are the same file as xyz after symlink resolution (-ef).

Note it would also report symlinks to another symlink or hardlink to xyz.


If you just wanted to find symlinks whose target is the literal string xyz, then it's just:

find . -lname xyz