terminalrm

how to prevent exit 1 when blanket rm -R with empty directories


So I am deploying a script to remove several directories of a program we no longer use. It was such a cluster though that not all directories are present on the computers. I am doing a simple rm -R with all the directories listed.

However my script fails when it comes up with "No such file or directory". Is there any way to have the script exit 0 instead of one? I would like to avoid an if then for simplicity. I'm hoping there is a flag or other operator I don't know about.

remove several directories and exit 0 even if the directories don't exist


Solution

  • You can use the --force/-f flag for this:

    $ rm -rf doesnotexist; echo $?
    0