Shell 脚本:将 bash 与 xargs 结合使用

发布于 2024-08-08 04:10:01 字数 230 浏览 4 评论 0原文

我正在尝试编写一个 bash 命令来删除与特定模式匹配的所有文件 - 在本例中,它是所有已建立的旧 vmware 日志文件。

我已经尝试过这个命令:

find . -name vmware-*.log | xargs rm

但是,当我运行该命令时,它会阻塞所有名称中包含空格的文件夹。有没有办法格式化文件路径,以便 xargs 将其传递给 rm 引用或正确转义?

I'm trying to write a bash command that will delete all files matching a specific pattern - in this case, it's all of the old vmware log files that have built up.

I've tried this command:

find . -name vmware-*.log | xargs rm

However, when I run the command, it chokes up on all of the folders that have spaces in their names. Is there a way to format the file path so that xargs passes it to rm quoted or properly escaped?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

无所的.畏惧 2024-08-15 04:10:02
find -iname pattern

使用 -iname 进行模式搜索

find -iname pattern

use -iname for pattern search

傲世九天 2024-08-15 04:10:02

为了避免 xargs 中的空格问题,我将使用换行符作为带有 -d 选项的分隔符:

find . -name vmware-*.log | xargs -d '\n' rm

To avoid space issue in xargs I'd use new line character as separator with -d option:

find . -name vmware-*.log | xargs -d '\n' rm
醉南桥 2024-08-15 04:10:01

尝试使用:

find . -name vmware-*.log -print0 | xargs -0 rm

这会导致 find 在每个文件名后输出一个空字符,并告诉 xargs 根据空字符而不是空格或其他标记来分解名称。

Try using:

find . -name vmware-*.log -print0 | xargs -0 rm

This causes find to output a null character after each filename and tells xargs to break up names based on null characters instead of whitespace or other tokens.

烟雨扶苏 2024-08-15 04:10:01

不要使用 xargs。 Find 可以在没有任何帮助的情况下完成此操作:

find 。 -name "vmware-*.log" -exec rm '{}' \;

Do not use xargs. Find can do it without any help:

find . -name "vmware-*.log" -exec rm '{}' \;

旧梦荧光笔 2024-08-15 04:10:01

查看 xargs-0 标志;结合 find-print0 你应该被设置。

find . -name vmware-*.log -print0 | xargs -0 rm

Check out the -0 flag for xargs; combined with find's -print0 you should be set.

find . -name vmware-*.log -print0 | xargs -0 rm
新一帅帅 2024-08-15 04:10:01

GNU 查找

find . -name vmware-*.log -delete

GNU find

find . -name vmware-*.log -delete
绝不服输 2024-08-15 04:10:01

<代码>查找 . -名称 vmware-*.log | xargs -i rm -rf {}

find . -name vmware-*.log | xargs -i rm -rf {}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文