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

3

28

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

Valeric answered 14/4, 2016 at 8:47 Comment(4)
Possible duplicate of Ignore empty result for xargsUnobtrusive
Your question should remain a question. I'd revert your edit but I'm hoping to give you a chance to post your new text as an answer instead before rolling back the change. (It will still be available from the edit history which you get by clicking on the "edited (date)" notice, obviously.)Commorancy
@Commorancy hello, I edited the post. I hope I did it properly! Thanks for noticing itValeric
It's an improvement, though I'd still be tempted to remove the commentary, or maybe move it to your answer. Thanks for the fix, though!Commorancy
P
48

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
Pointless answered 14/4, 2016 at 8:59 Comment(0)
W
13

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

-f, --force 
       ignore nonexistent files and arguments, never prompt
Widespread answered 24/12, 2016 at 2:42 Comment(1)
It is more semantically correct to not invoke rm at all, when there is nothing for it to run for.Oligopsony
V
1

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.

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.

Valeric answered 7/12, 2017 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.