Search code examples
bashfindrm

How to deal with missing operand error when trying to rm


I have the following code

rm "$torrent"/*.{txt,nfo,jpg} && echo "removed \"txt,nfo,jpg\" files"

If there are no files to delete then I get the "missing operand" error which I would like to avoid because it triggers an exit 1 for the script.

If I have the following code

rm -f "$torrent"/*.{txt,nfo,jpg} && echo "removed \"txt,nfo,jpg\" files"

the rm always evaluates to true even if there were no files deleted.

I want to display the message "removed .. files" if some files were deleted. If no files were deleted I want silence with no errors thrown.

I toyed with

find "$torrent" -type f -name "*.txt" -or -name "*.nfo" -or -name ".jpg" -delete && echo "files were deleted"

but it always evaluated to true even if no files were deleted.


Solution

  • Just rewrite it into a proper if statement:

    if rm "$torrent"/*.{txt,nfo,jpg}
    then
      echo "removed \"txt,nfo,jpg\" files"
    if
    

    This allows you to react to the exit status while set -e is in effect, without the script exiting