Is there a way to determine if a symbolic link is referenced in Linux?

Asked 1 years ago, Updated 1 years ago, 396 views

Thank you for your help.

As stated in the title, I am using Linux (CentOS). Is there any way to verify that a directory is referenced to a symbolic link?
 
Openjdk is placed under /usr/local/ as follows:

jdk->/usr/local/jdk-19.0.2
  jdk-19
  jdk-19.0.1
  jdk-19.0.2

PATH is set to /usr/local/jdk so that Java programs do not care about the version.
I regularly delete old versions of folders, but when I tried to delete them, I began to wonder if there was any problem with deleting them.
Since the environment itself is a shared server, there is a possibility that a program other than yourself will be able to see the JDK of jdk-19 without permission.

You can find the symbolic link from the root (/) and check it.
Is there any easier way?
If anyone knows, please give me an idea.
  

linux

2023-02-09 08:49

3 Answers

Symbolic links have reference information, but there is no reference information
Where is it being referenced from where. Therefore, it may be better to cache it using the following commands (for example) and search for it if necessary.

  • $find/usr/local/-type l
  • $ls-lR/usr/local/|grep^l

There's also a hard link.
If you want to create a hard link, you can at least tell if there are more than one (for example, ls-l).


2023-02-09 08:57

As you can see in your reply, the information from which symbolic links are referenced is not managed on the file system.

In Linux environments, readlink(1) seems to allow you to recursively follow symbolic links to find end-to-end files.

find/-type l-print-exec readlink-f{}\;|paste---

For example, if you do the above, you can see the symbolic link file and the end file pair, so if you narrow it down with grep, you will find a file that is symbolic linking directly or indirectly to the file you want to check.


2023-02-09 09:08

Use the -samefile option of the GNU find command.
※ Since searching from root directory may take a long time, you may want to limit the search start directory if possible or include the -xdev or -ftype options.

$cd/tmp
$ touch foo
$ (cd/var/tmp&&ln-s../../tmp/foo.;ls-lfoo)
lrwxrwxrwx1 nemo 13 Feb 8 18:45 foo->../../tmp/foo

$ find -- version
find (GNU findutils) 4.8.0

$ find-L/-xdev-samefile/tmp/foo2>/dev/null
/var/tmp/foo
/tmp/foo


2023-02-09 09:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.