![如何在服务器的整个本地文件系统中搜索断开的链接?](https://linux22.com/image/1369810/%E5%A6%82%E4%BD%95%E5%9C%A8%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%9A%84%E6%95%B4%E4%B8%AA%E6%9C%AC%E5%9C%B0%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E4%B8%AD%E6%90%9C%E7%B4%A2%E6%96%AD%E5%BC%80%E7%9A%84%E9%93%BE%E6%8E%A5%EF%BC%9F.png)
由于未安装 bash,因此我必须在 korn 中执行此操作。这是我目前的脚本,但它并没有像我预期的那样工作。
cd /
find . -xdev -type d > /tmp/dirlist
export YOURLIST=`cat /tmp/dirlist2`
echo START > /tmp/final
for a in $YOURLIST; do
export MYLIST=`ll $a | grep "\->" | awk '{print $11;}'`
echo in dir $a >> /tmp/final
sleep 5
for b in $MYLIST; do
echo `ll $b` 2>&1 > /tmp/result
grep -q not /tmp/result
export RC=$?
if [ "$RC" = "0" ]
then
cat /tmp/result >> /tmp/final
fi
done
done
目前它只打印目录,/tmp/final
并且仍然显示未找到奇怪的是,信息stdout
。
答案1
find
应该能够找到符号链接,你不必依赖解析ls
。
find / -xdev -type l -print |
while IFS= read -r f; do
target=$(readlink "$f")
[[ -e "$target" ]] || echo "broken symlink: $f -> $target"
done
答案2
做了两件事来模拟 Glenn 的回应中缺失的内容
root@system:/ $ cat /bin/checklink
export ORIGINAL=`ll $1 | awk '{print $9;}'`
export ORIGINALDIR=`dirname $ORIGINAL`
export LINKIS=`ll $1 | awk '{print $11;}'`
export FOUND="0"
if [ -d ${ORIGINALDIR}/${LINKIS} ]
then
export FOUND="1"
else if [ -f ${ORIGINALDIR}/${LINKIS} ]
then
export FOUND="2"
else if [ -d ${LINKIS} ]
then
export FOUND="3"
else if [ -f ${LINKIS} ]
then
export FOUND="4"
fi
fi
fi
fi
if [ "$FOUND" = "0" ]
then
echo $1 pointing to $LINKIS not found
fi
随后致电
`find / -xdev -type l -exec checklink {} \;
请随意改进...