26

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

aPugLife
  • 989
  • 2
  • 14
  • 25
  • Possible duplicate of [Ignore empty result for xargs](https://stackoverflow.com/questions/8296710/ignore-empty-result-for-xargs) – jazzmax Dec 06 '17 at 18:23
  • 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.) – tripleee Dec 07 '17 at 04:40
  • @tripleee hello, I edited the post. I hope I did it properly! Thanks for noticing it – aPugLife Dec 07 '17 at 09:40
  • 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! – tripleee Dec 07 '17 at 09:44

3 Answers3

47

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
Inian
  • 80,270
  • 14
  • 142
  • 161
12

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

-f, --force 
       ignore nonexistent files and arguments, never prompt
Jingguo Yao
  • 7,320
  • 6
  • 50
  • 63
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.

aPugLife
  • 989
  • 2
  • 14
  • 25