列出终端中带空格的文件
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ls 有 -Q 选项来引用它们。然后你可以简单地这样做:
ls has the -Q option to quote them. You then simply can do this:
虽然您可以使用
ls(1)
来完成这项工作,但我认为更好的方法是使用find(1)
来代替:或者
我更喜欢
-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 usefind(1)
instead:or
I prefer the
-print0 | xargs -0
approach when it works, becausexargs(1)
will spawn only as manyrm(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 executerm(1)
only 200 or 500 times rather than all 10,000 times.这应该可以解决问题:
This should do the trick: