我运行find
并使用[ \( -L $F \) ]
迭代结果以收集某些符号链接。
我想知道在这种情况下是否有一种简单的方法可以确定链接是否已损坏(指向不存在的文件)。
这是我的代码:
FILES=`find /target/ | grep -v '\.disabled$' | sort`
for F in $FILES; do
if [ -L $F ]; then
DO THINGS
fi
done
答案 0 :(得分:44)
# test if file exists (test actual file, not symbolic link)
if [ ! -e "$F" ] ; then
# code if the symlink is broken
fi
答案 1 :(得分:29)
这应打印出已损坏的链接:
find /target/dir -type l ! -exec test -e {} \; -print
您还可以将操作链接到find
命令,例如删除断开的链接:
find /target/dir -type l ! -exec test -e {} \; -exec rm {} \;
答案 2 :(得分:7)
readlink -q
将无声地失败:
for F in $FILES; do
if [ -L $F ]; then
if readlink -q $F >/dev/null ; then
DO THINGS
else
echo "$F: bad link" >/dev/stderr
fi
fi
done
答案 3 :(得分:5)
如果符号链接指向文件或目录,但现在已断开
,这将起作用if [[ -L "$strFile" ]] && [[ ! -a "$strFile" ]];then
echo "'$strFile' is a broken symlink";
fi
答案 4 :(得分:3)
这会找到“link”类型的所有文件,这些文件也会解析为“link”类型。即。一个破碎的符号链接
find /target -type l -xtype l
答案 5 :(得分:2)
如果你不介意遍历非破坏的dir符号链接,找到所有孤立的链接:
$ find -L /target -type l | while read -r file; do echo $file is orphaned; done
要查找非孤立链接的所有文件:
$ find -L /target ! -type l
答案 6 :(得分:0)
怎么了?
file $f | grep 'broken symbolic link'
答案 7 :(得分:0)
如果确实符合符号链接的条件,但“不存在”,则它是断开的链接。
{
"name": "web-resources",
"version": "1.0.0",
"private": true,
"dependencies": {
"animate.css": "3.7.0",
"bootstrap": "4.1.3",
"bootstrap-select": "1.13.2",
"chartist": "0.11.0",
"clipboard": "2.0.1",
"custombox": "4.0.3",
"datatables": "1.10.18",
"flag-icon-css": "3.0.0",
"flatpickr": "4.5.1",
"gmaps": "0.4.24",
"instafeed.js": "1.4.1",
"ion-rangeslider": "2.2.0",
"jquery": "^3.4.1",
"jquery-migrate": "3.0.1",
"jquery-validation": "1.17.0",
"lazysizes": "5.2.0",
"malihu-custom-scrollbar-plugin": "3.1.5",
"popper.js": "1.14.4",
"summernote": "0.8.10",
"svg-injector": "1.1.3",
"typed.js": "2.0.9"
},
"devDependencies": {
"browser-sync": "^2.26.7",
"del": "3.0.0",
"gulp": "^4.0.2",
"gulp-autoprefixer": "5.0.0",
"gulp-cache": "1.0.2",
"gulp-changed": "3.1.0",
"gulp-clean": "0.4.0",
"gulp-concat": "2.6.1",
"gulp-cssnano": "2.1.3",
"gulp-htmlmin": "4.0.0",
"gulp-imagemin": "4.1.0",
"gulp-inject": "^5.0.4",
"gulp-rename": "1.4.0",
"gulp-sass": "4.0.1",
"gulp-sourcemaps": "2.6.4",
"gulp-uglifyjs": "0.6.2",
"imagemin-jpeg-recompress": "5.1.0",
"imagemin-pngquant": "5.1.0"
}
}