bash 与“查找”命令一起使用时出现错误“rm:缺少操作数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36617999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:29:55  来源:igfitidea点击:

Error 'rm: missing operand' when using along with 'find' command

bashrmoperand

提问by Nihvel

I see that this question is getting popular. I answered my own question below. What says Inianis correct and it helped me to analyze my source code better.

我看到这个问题越来越流行。我在下面回答了我自己的问题。什么说Inian是正确的,它帮助我更好地分析我的源代码。

My problem was in the FINDand not in the RM. My answer gives a block of code, which I am currently using, to avoid problems when FIND finds nothing but still would pass arguments to RM, causing the error mentioned above.

我的问题是在FIND而不是在RM. 我的回答给出了一个我目前正在使用的代码块,以避免在 FIND 什么也没找到但仍将参数传递给 RM 时出现问题,从而导致上述错误。

OLD QUESTION BELOW

下面的老问题

I'm writing many and many different version of the same command. All, are executed but with an error/info:

我正在编写同一命令的许多不同版本。所有,都被执行,但有一个错误/信息:

rm: missing operand
Try 'rm --help' for more information.

These are the commands I'm using:

这些是我正在使用的命令:

#!/bin/bash
BDIR=/home/user/backup
find ${BDIR} -type d -mtime +180 -print -exec rm -rf {} \;
find ${BDIR} -type d -mtime +180 -print -exec rm -rf {} +
find "$BDIR" -type d -mtime +180 -print -exec rm -rf {} \;
find "$BDIR" -depth -type d -mtime +180 -print -exec rm -rf {} \;
find ${BDIR} -depth -type d -mtime +180 -print -exec rm -rf {} +

find $BDIR -type d -mtime +180 -print0 | xargs -0 rm -rf

DEL=$(FIND $BDIR -type d -mtime +180 -print)
rm -rf $DEL

I'm sure all of them are correct (because they all do their job), and if I run them manually I do not get that message back, but while in a .sh script I do.

我确信它们都是正确的(因为它们都在做自己的工作),如果我手动运行它们,我不会收到该消息,但是在 .sh 脚本中我会收到。

EDIT: since I have many of these RM's, the problem could be somewhere else. I'm checking all of them. All of the above codes works but the best answer is the one marked ;)

编辑:因为我有很多这样的 RM,问题可能出在其他地方。我正在检查所有这些。以上所有代码都有效,但最好的答案是标记的那个;)

回答by Inian

The problem is when using find/grepalong with xargsyou need to be sure to run the piped command only if the previous command is successful. Like in the above case, if the findcommand does not produce any search results, the rmcommand is invoked with an empty argument list.

问题是当find/grepxargs你一起使用时,你需要确保只有在前一个命令成功时才运行管道命令。与上述情况一样,如果该find命令不产生任何搜索结果,rm则使用空参数列表调用该命令。

The manpage of xargs

man页面xargs

 -r      Compatibility with GNU xargs.  The GNU version of xargs runs the
         utility argument at least once, even if xargs input is empty, and
         it supports a -r option to inhibit this behavior.  The FreeBSD
         version of xargs does not run the utility argument on empty
         input, but it supports the -r option for command-line compatibil-
         ity with GNU xargs, but the -r option does nothing in the FreeBSD
         version of xargs.

Moreover, you don't to try all the commands like you pasted the below simple one will suit your need.

此外,您不要像粘贴下面那样尝试所有命令,简单的命令将适合您的需要。

Add the -rargument to xargs like

-r参数添加到 xargs 中,例如

find "$BDIR" -type d -mtime +180 -print0 | xargs -0 -r rm -rf

回答by Jingguo Yao

-foption of rmsuppresses the rm: missing operanderror:

-frm抑制rm: missing operand错误的选项:

-f, --force 
       ignore nonexistent files and arguments, never prompt

回答by Nihvel

After researches, the command I'm comfortable using is:

经过研究,我习惯使用的命令是:

HOME=/home/user
FDEL=$HOME/foldersToDelete
BDIR=/backup/my_old_folders
FLOG=/var/log/delete_old_backup.log
find ${BDIR} -mindepth 1 -daystart -type d -mtime +180 -printf "%f\n" > ${FDEL}
if [[ $? -eq 0 && $(wc -l < ${FDEL}) -gt 0 ]]; then
    cd ${BDIR}
    xargs -d '\n' -a ${FDEL} rm -rf
  LOG=" - Folders older than 180 were deleted"
else
  LOG=" - There aren't folders older than 180 days to delete"
fi
echo ${LOG} >> ${FLOG}

Why?I search all the old folders I want to delete and print them all into a file, regardless for their naming with or without space. If the file is bigger than 0 byte this means that there are folder I want no more.

为什么?我搜索所有要删除的旧文件夹并将它们全部打印到一个文件中,无论它们的命名是否有空格。如果文件大于 0 字节,这意味着有我不再想要的文件夹。

If your 'FIND' fails with a 'rm: missing operand', it probably isn't to search in the RM rather in the FIND itself. A good way of removing the file using FIND, is the one I felt to share with you.

如果您的 'FIND' 因 'rm: missing operation' 而失败,则可能不是在 RM 中搜索,而是在 FIND 本身中搜索。使用 FIND 删除文件的一种好方法是我想与您分享的方法。