Linux可以';不要删除文件
当我找到文件时,我无法删除它们。 任务:必须找到带有空格的文件并将其删除 我的尝试:) 但我有错误:Linux可以';不要删除文件,linux,bash,grep,find,Linux,Bash,Grep,Find,当我找到文件时,我无法删除它们。 任务:必须找到带有空格的文件并将其删除 我的尝试:) 但我有错误: rm: cannot remove `/root/test': No such file or directory rm: cannot remove `2.txt': No such file or directory rm: cannot remove `/root/test': No such file or directory rm: cannot remove `3.txt': No
rm: cannot remove `/root/test': No such file or directory
rm: cannot remove `2.txt': No such file or directory
rm: cannot remove `/root/test': No such file or directory
rm: cannot remove `3.txt': No such file or directory
rm: cannot remove `/root/test': No such file or directory
rm: cannot remove `1.txt': No such file or directory
请帮助解决这个问题
谢谢。为什么不这样做呢:
find /root -type f -name '* *' -exec rm -f {} ';'
可以为find命令指定-exec参数,以运行以生成的文件作为参数的命令。在您的情况下,以下命令将执行您想要的操作
find /root -type f -name '* *'
-类型f
将仅打印文件。如果只需要目录,请使用-键入d
。如果您不使用这两个选项,那么它将同时打印文件和目录
由于这是一个删除操作,请首先运行该命令,查看它是否正在打印所需的文件
find /root -type f -name '* *'
然后,如果一切正常,运行此命令删除它们
find /root -type f -name '* *' -exec rm {} \;
我猜您正在查找带有空格或引号的文件。试试这个:
find /test/path -print0 | xargs -0 rm
这样做的目的是将文件名发送到stdout,并以NULL
字节分隔,而xargs
将作为分隔符。这样可以在输出中使用空格、引号和其他有趣的内容
现在,如果要删除目录,rm
将不起作用。因此,您可能需要在上面添加一个-type f
请注意,gnufind
本身有一个-delete
操作符,它将为您删除文件,但您想知道原因。因此,较短的路线是:
find /test/path -delete
如果不添加
-type f
,这也将处理目录。它还将首先处理删除最深的内容(想想为什么需要这样做)。如果您向OP解释了他们的命令不起作用的原因,以及find-exec
如何更好,以及为什么更好,那就好了。