Linux 有没有办法检查是否有指向目录的符号链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/100170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:28:23  来源:igfitidea点击:

Is there a way to check if there are symbolic links pointing to a directory?

linuxsymlink

提问by nickf

I have a folder on my server to which I had a number of symbolic links pointing. I've since created a new folder and I want to change all those symbolic links to point to the new folder. I'd considered replacing the original folder with a symlink to the new folder, but it seems that if I continued with that practice it could get very messy very fast.

我的服务器上有一个文件夹,我有许多符号链接指向该文件夹。我已经创建了一个新文件夹,我想更改所有这些符号链接以指向新文件夹。我曾考虑用新文件夹的符号链接替换原始文件夹,但似乎如果我继续这种做法,它可能会很快变得非常混乱。

What I've been doing is manually changing the symlinks to point to the new folder, but I may have missed a couple.

我一直在做的是手动更改符号链接以指向新文件夹,但我可能错过了一些。

Is there a way to check if there are any symlinks pointing to a particular folder?

有没有办法检查是否有指向特定文件夹的符号链接?

采纳答案by skymt

I'd use the find command.

我会使用 find 命令。

find . -lname /particular/folder

That will recursively search the current directory for symlinks to /particular/folder. Note that it will only find absolute symlinks. A similar command can be used to search for all symlinks pointing at objects called "folder":

这将递归搜索当前目录的符号链接到/particular/folder. 请注意,它只会找到绝对符号链接。类似的命令可用于搜索指向名为“文件夹”的对象的所有符号链接:

find . -lname '*folder'

From there you would need to weed out any false positives.

从那里你需要清除任何误报。

回答by bfabry

find / -lname 'fullyqualifiedpathoffile'

回答by paxdiablo

For hardlinks, you can get the inode of your directory with one of the "ls" options (-i, I think).

对于硬链接,您可以使用“ls”选项之一(-i我认为)获取目录的 inode 。

Then a findwith -inumwill locate all common hardlinks.

然后findwith-inum将定位所有常见的硬链接。

For softlinks, you may have to do an ls -lon all files looking for the text after "->" and normalizing it to make sure it's an absolute path.

对于软链接,您可能必须ls -l在所有文件上查找“->”之后的文本并将其规范化以确保它是绝对路径。

回答by Greg Hewgill

There isn't really any direct way to check for such symlinks. Consider that you might have a filesystem that isn't mounted all the time (eg. an external USB drive), which could contain symlinks to another volume on the system.

确实没有任何直接的方法来检查此类符号链接。考虑到您的文件系统可能没有一直挂载(例如,外部 USB 驱动器),其中可能包含指向系统上另一个卷的符号链接。

You could do something with:

你可以做一些事情:

for a in `find / -type l`; do echo "$a -> `readlink $a`"; done | grep destfolder

I note that FreeBSD's finddoes not support the -lnameoption, which is why I ended up with the above.

我注意到 FreeBSDfind不支持该-lname选项,这就是我以上述方式结束的原因。

回答by stephanea

Apart from looking at all other folders if there are links pointing to the original folder, I don't think it is possible. If it is, I would be interested.

除了查看所有其他文件夹是否有指向原始文件夹的链接之外,我认为这是不可能的。如果是的话,我会感兴趣的。

回答by no1uknow

find . -type l -printf '%p -> %l\n'

回答by JJK

You can audit symlinks with the symlinksprogramwritten by Mark Lord -- it will scan an entire filesystem, normalize symlink paths to absolute form and print them to stdout.

您可以使用Mark Lord 编写symlinks程序审核符号链接——它将扫描整个文件系统,将符号链接路径规范化为绝对形式并将它们打印到标准输出。

回答by Lunar Mushrooms

find /foldername -type l -exec ls -lad {} \;