删除断开的链接 Unix [英] Deleting a broken link Unix
问题描述
我想删除一个断开的链接,但在此之前我想确认链接文件是否存在于目录中.让我们调用链接A
:
I want to delete a broken link, but before that I want to confirm if the link file is present in directory. Let's call the link A
:
if [ -a A ] then
print 'ya A is ther'
fi
但是如果 A
是一个断开的链接,那么我该如何检查?
But if A
is a broken link then how can I check?
推荐答案
find -L -type l
查找损坏的符号链接.首先使用 test -d
确认该文件不是目录或指向目录的符号链接(如果它是目录,find
会递归进入该目录).因此:
find -L -type l
finds broken symbolic links. First confirm that the file is not a directory or a symbolic link to a directory with test -d
(if it's a directory, find
would recurse into it). Thus:
is_broken_symlink () {
case $1 in -*) set "./$1";; esac
! [ -d "$1" ] && [ -n "$(find -L "$1" -type l)" ]
}
如果test
调用和find
调用之间的链接发生变化,这很容易出现竞争条件.另一种方法是告诉 find
不要递归.
This is prone to a race condition, if the link changes between the call to test
and the call to find
. An alternative approach is to tell find
not to recurse.
is_broken_symlink () {
case $1 in -*) set "./$1";; esac
[ -n "$(find -L "$1" -type l -print -o -prune)" ]
}
这篇关于删除断开的链接 Unix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!