Because I always forget, here's how to check if a file exists before attempting to delete it in bash:


[ -f foobar.html ] && rm foobar.html

If you don't do it this way and the file doesn't exist you get this:


rm: cannot remove `foobar.html': No such file or directory

Comments

ivo

rm -f foobar.html will not complain if the file doesn't exist

Your email will never ever be published.

Previous:
"Confessions of a College Callgirl" is the new "My Secret Life As A Prostitute" March 2, 2008 Misc. links
Next:
See you at PyCon 2008 March 11, 2008 Python
Related by category:
How to find which git SHA it was when you merged in the default branch February 26, 2026 Linux
hylite as an executable October 15, 2025 Linux
Elasticsearch memory usage December 11, 2025 Linux
How to count the number of non-blank lines with Bash September 3, 2025 Linux
Related by keyword:
gg shell completion August 13, 2025 Linux, JavaScript, Bun, macOS
Find the source of an alias in bash September 29, 2025 Linux, Bash, macOS
set -ex - The most useful bash trick of the year August 31, 2014 Linux
How to intercept and react to non-zero exits in bash February 23, 2023 Bash, GitHub