列出终端中带空格的文件

发布于 2024-11-16 17:08:13 字数 121 浏览 8 评论 0原文

ls -1 | grep -v images | xargs rm -r

这不会删除其中包含空格的文件。我怎样才能将它们包括在内?有没有办法当我说 ls 时它会显示转义的空格?

ls -1 | grep -v images | xargs rm -r

That does not delete files that have spaces in them. How can I include them? Is there a way when I say ls that it displays with spaces escaped?

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

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

发布评论

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

评论(3

白芷 2024-11-23 17:08:13

ls 有 -Q 选项来引用它们。然后你可以简单地这样做:

ls -Q | grep -v images | xargs rm -r

ls has the -Q option to quote them. You then simply can do this:

ls -Q | grep -v images | xargs rm -r
记忆里有你的影子 2024-11-23 17:08:13

虽然您可以使用 ls(1) 来完成这项工作,但我认为更好的方法是使用 find(1) 来代替:

find . \! -name '*images*' -exec rm -r {} \;

或者

find . \! -name '*images*' -print0 | xargs -0 rm -r

我更喜欢 -print0 | xargs -0 工作时接近,因为 xargs(1) 只会生成所需数量的 rm(1) 命令。当您只有 200 个文件时,执行 200 次或执行一次不会有太大区别,但如果您有 10,000 个文件要删除,您实际上宁愿只执行 200 次或500 次而不是全部 10,000 次。

While you can make this work with ls(1), I think a better approach is to use find(1) instead:

find . \! -name '*images*' -exec rm -r {} \;

or

find . \! -name '*images*' -print0 | xargs -0 rm -r

I prefer the -print0 | xargs -0 approach when it works, because xargs(1) will spawn only as many rm(1) commands as is necessary. When you've only got 200 files, 200 executions or one execution won't make much difference, but if you had 10,000 files to delete, you'd really rather execute rm(1) only 200 or 500 times rather than all 10,000 times.

浅黛梨妆こ 2024-11-23 17:08:13

这应该可以解决问题:

ls -1 | grep -v images | xargs -I {} rm -r "{}"

This should do the trick:

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