开发者

list files in terminal with spaces

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

That does not delete file开发者_如何学JAVAs that have spaces in them. How can I include them? Is there a way when I say ls that it displays with spaces escaped?


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

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


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.


This should do the trick:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜