bash 用一行删除所有损坏的符号链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22097130/
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-09-10 00:42:23  来源:igfitidea点击:

Delete all broken symbolic links with a line?

bashshellunixsymlink

提问by fotanus

For a given folder, how can I delete all broken links within it?

对于给定的文件夹,如何删除其中的所有损坏链接?

I found this answerthat shows how to delete one broken link, but I can't put that together in only one line. Is there a one-liner for this?

我发现这个答案显示了如何删除一个断开的链接,但我不能把它放在一行中。有单行的吗?

A broken symbolic is a link that points to a file/folder that doesn't exists any longer.

损坏的符号是指向不再存在的文件/文件夹的链接。

回答by Gilles 'SO- stop being evil'

Here's a POSIX way of deleting all broken symbolic links in the current directory, without recursion. It works by telling findto traverse symbolic links (-L), but stopping (-prune) at every directory-or-symbolic-link-to-such.

这是删除当前目录中所有损坏的符号链接的 POSIX 方法,无需递归。它的工作原理是告诉find遍历符号链接 ( -L),但-prune在每个目录或符号链接到此类处停止 ( )。

find -L . -name . -o -type d -prune -o -type l -exec rm {} +

You can also use a shell loop. The test -Lmatches symbolic links, and -ematches existing files (excluding broken symlinks).

您还可以使用 shell 循环。该测试-L匹配符号链接,并-e匹配现有文件(不包括损坏的符号链接)。

for x in * .[!.]* ..?*; do if [ -L "$x" ] && ! [ -e "$x" ]; then rm -- "$x"; fi; done

If you want to recurse into subdirectories, this technique doesn't work. With GNU find (as found on non-embedded Linux and Cygwin), you can use the -xtypepredicate to detect broken symbolic links (-xtypeuses the type of the target for symbolic links, and reports lfor broken links).

如果您想递归到子目录中,则此技术不起作用。使用 GNU find(在非嵌入式 Linux 和 Cygwin 上可以找到),您可以使用-xtype谓词来检测损坏的符号链接(-xtype使用符号链接的目标类型,并报告l损坏的链接)。

find -xtype l -delete

POSIXly, you need to combine two tools. You can use find -type l -exec …to invoke a command on each symbolic link, and [ -e "$x" ]to test whether that link is non-broken.

POSIXly,你需要结合两个工具。您可以使用find -type l -exec …对每个符号链接调用命令,并[ -e "$x" ]测试该链接是否未损坏。

find . -type l -exec sh -c 'for x; do [ -e "$x" ] || rm "$x"; done' _ {} +

The simplest solution is to use zsh. To delete all broken symbolic links in the current directory:

最简单的解决方案是使用 zsh。删除当前目录中所有损坏的符号链接:

rm -- *(-@D)

The characters in parentheses are glob qualifiers: -to dereference symlinks, @to match only symlinks (the combination -@means broken symlinks only), and Dto match dot files. To recurse into subdirectories, make that:

括号中的字符是glob 限定符-取消引用符号链接,@仅匹配符号链接(组合-@意味着仅损坏的符号链接),以及D匹配点文件。要递归到子目录,请执行以下操作:

rm -- **/*(-@D)

回答by sanmiguel

Simple answer based on the answer you linked (for a given directory, $DIR):

基于您链接的答案的简单答案(对于给定目录,$DIR):

find -L $DIR -maxdepth 1 -type l -delete

回答by saurabheights

For MAC, do a dry run as follows:-

对于MAC,请按如下方式进行试运行:-

DIR=<some path>
find -L $DIR -maxdepth 1 -type l -print

Now, you can prune the old symlinks as follows:-

现在,您可以按如下方式修剪旧的符号链接:-

for f in `find -L $DIR -maxdepth 1 -type l`; do unlink $f; done

回答by Mr. Kennedy

From man findEXAMPLES:

man find例子:

find -L /usr/ports/packages -type l -exec rm -- {} +

Delete all broken symbolic links in /usr/ports/packages.

删除 /usr/ports/packages 中所有损坏的符号链接。

回答by rayphi

Answer based on the accepted answer to the question question "How can I find broken symlinks":

根据对问题“我如何找到损坏的符号链接”的公认答案进行回答:

find . -type l -! -exec test -e {} \; -print | xargs rm

回答by SamWN

The package symlinksis pre-installed on many distributions (including Ubuntu 16.04 & Fedora 25) and has some really useful features, one of which does precisely what you're looking for:

该软件包symlinks已预安装在许多发行版(包括 Ubuntu 16.04 和 Fedora 25)上,并且具有一些非常有用的功能,其中之一正是您正在寻找的功能:

symlinks -d ./

回答by devnull

You could use readlinkto determine if a symlink is broken or not.

您可以readlink用来确定符号链接是否已损坏。

The following would list all the broken symlinks in a given directory (and subdirectories):

以下将列出给定目录(和子目录)中所有损坏的符号链接:

find . -type l -exec sh -c 'readlink -f "{}" 1>/dev/null || echo "{}"' -- "{}" \;

Replace echowith rmto get rid of the broken ones!

替换echorm以摆脱损坏的!

(I've redirected the output of readlinkto /dev/nullso as to avoid confusion; it'd list the target for the symlinks.)

(我已重定向readlinkto的输出/dev/null以避免混淆;它会列出符号链接的目标。)

回答by Erdos Yi

You can try using rmlint.

您可以尝试使用rmlint

First, change directory to the folder that contains broken symlinks, and then run the following commands to find bad symlinks pointing nowhere:

首先,将目录更改为包含损坏符号链接的文件夹,然后运行以下命令以查找无处指向的错误符号链接

rmlint --types="badlinks"

Then rmlintwill create a bash script rmlint.shin your current directory and print a list of bad symlinks in your terminal. To delete all the bad symlinks in your current directory, you can run

然后rmlintrmlint.sh在您的当前目录中创建一个 bash 脚本并在您的终端中打印一个错误符号链接列表。要删除当前目录中的所有坏符号链接,您可以运行

./rmlint.sh

Not exactly one liner, but it is very easy to use.

不完全是一个衬垫,但它很容易使用。