sda*_*aau 49 linux bash symlink reverse
请考虑以下命令行代码段:
$ cd /tmp/
$ mkdir dirA
$ mkdir dirB
$ echo "the contents of the 'original' file" > orig.file
$ ls -la orig.file
-rw-r--r-- 1 $USER $USER 36 2010-12-26 00:57 orig.file
# create symlinks in dirA and dirB that point to /tmp/orig.file:
$ ln -s $(pwd)/orig.file $(pwd)/dirA/
$ ln -s $(pwd)/orig.file $(pwd)/dirB/lorig.file
$ ls -la dirA/ dirB/
dirA/:
total 44
drwxr-xr-x 2 $USER $USER 4096 2010-12-26 00:57 .
drwxrwxrwt 20 root root 36864 2010-12-26 00:57 ..
lrwxrwxrwx 1 $USER $USER 14 2010-12-26 00:57 orig.file -> /tmp/orig.file
dirB/:
total 44
drwxr-xr-x 2 $USER $USER 4096 2010-12-26 00:58 .
drwxrwxrwt 20 root root 36864 2010-12-26 00:57 ..
lrwxrwxrwx 1 $USER $USER 14 2010-12-26 00:58 lorig.file -> /tmp/orig.file
Run Code Online (Sandbox Code Playgroud)
在这一点上,我可以readlink用来看看"原始"是什么(好吧,我想这里的通常术语是"目标"或"来源",但我脑海中的那些也可能是相反的概念,所以我会只是称它为符号链接的"原始"文件,即
$ readlink -f dirA/orig.file
/tmp/orig.file
$ readlink -f dirB/lorig.file
/tmp/orig.file
Run Code Online (Sandbox Code Playgroud)
...但是,我想知道的是 - 是否有一个命令我可以在'原始'文件上运行,并找到指向它的所有符号链接?换句话说,像(伪):
$ getsymlinks /tmp/orig.file
/tmp/dirA/orig.file
/tmp/dirB/lorig.file
Run Code Online (Sandbox Code Playgroud)
提前感谢您的任何意见,
干杯!
Pau*_*ce. 139
使用GNU find,这将找到硬链接或符号链接到文件的文件:
find -L /dir/to/start -samefile /tmp/orig.file
Run Code Online (Sandbox Code Playgroud)
pax*_*blo 30
我没有看到这个命令,这不是一件容易的事,因为目标文件包含有关源文件指向它的零信息.
这类似于"硬"链接,但至少这些链接始终位于同一文件系统中,因此您可以执行find -inode列出它们.软链接更有问题,因为它们可以跨文件系统.
我认为你要做的就是基本上ls -al对整个层次结构中的每个文件执行一次并grep用来搜索-> /path/to/target/file.
例如,这里有一个我在我的系统上运行(格式化的可读性-最后两行实际上是对一个在现实输出线):
pax$ find / -exec ls -ald {} ';' 2>/dev/null | grep '\-> /usr/share/applications'
lrwxrwxrwx 1 pax pax 23 2010-06-12 14:56 /home/pax/applications_usr_share
-> /usr/share/applications
Run Code Online (Sandbox Code Playgroud)
受到戈登·戴维森评论的启发。这与另一个答案类似,但我使用 exec 得到了所需的结果。我需要一些可以在不知道原始文件所在位置的情况下找到符号链接的东西。
find / -type l -exec ls -al {} \; | grep -i "all_or_part_of_original_name"
Run Code Online (Sandbox Code Playgroud)