【发布时间】:2016-08-13 23:51:08
【问题描述】:
我想删除目录中的 n 个(在我们的例子中是 2 个)最大的文件。
files=$(ls -S | head -2)
rm $files
这不起作用,因为文件名中有空格和各种特殊字符。我用这个ls -xS | head -2 | xargs rm 得到了类似的结果。我想应该转义文件名中的所有特殊字符,但有各种类型的特殊字符。虽然可行,但没想到会这么复杂。
我使用 -Q 选项来引用文件名,但我仍然得到同样的错误。
Downloads > files=$(ls -SQ | head -1)
Downloads > echo $files
"[ www.UsaBit.com ] - Little Children 2006 720p BRRip x264-PLAYNOW.mp4"
Downloads > rm $files
rm: cannot remove ‘"[’: No such file or directory
rm: cannot remove ‘www.UsaBit.com’: No such file or directory
rm: cannot remove ‘]’: No such file or directory
rm: cannot remove ‘-’: No such file or directory
rm: cannot remove ‘Little’: No such file or directory
rm: cannot remove ‘Children’: No such file or directory
rm: cannot remove ‘2006’: No such file or directory
rm: cannot remove ‘720p’: No such file or directory
rm: cannot remove ‘BRRip’: No such file or directory
rm: cannot remove ‘x264-PLAYNOW.mp4"’: No such file or directory
【问题讨论】:
-
他们有引号吗?
-
我以为你的意思是我在文件名中有引号。 :)
-
这就是我的意思。
-
无论如何,很高兴您解决了问题。 :D (我的解决方案看起来比@choroba 的要丑得多……;)
标签: bash ubuntu-14.04