How to prevent rm from reporting that a file was not found?

The main use of -f is to force the removal of files that would
not be removed using rm by itself (as a special case, it “removes”
non-existent files, thus suppressing the error message).

You can also just redirect the error message using

$ rm file.txt 2> /dev/null

(or your operating system’s equivalent). You can check the value of $?
immediately after calling rm to see if a file was actually removed or not.

Leave a Comment