Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

My problem was in the FIND and 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.

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.

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 ;)

like image 328
aPugLife Avatar asked Apr 14 '16 08:04

aPugLife


People also ask

What does Missing operand mean in Linux?

Answer 50f9be8887690fcabe008961 As far as I understand it, the error means you've used a wrong mathematical expression for “greater than or equal.” The correct operand is >=. I used => the first time and got the same error.


2 Answers

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

The man page of 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 -r argument to xargs like

find "$BDIR" -type d -mtime +180 -print0 | xargs -0 -r rm -rf 
like image 192
Inian Avatar answered Sep 18 '22 13:09

Inian


-f option of rm suppresses the rm: missing operand error:

-f, --force         ignore nonexistent files and arguments, never prompt 
like image 37
Jingguo Yao Avatar answered Sep 18 '22 13:09

Jingguo Yao