File check before delete

07 March 2008   1 comment   Linux

Mind That Age!

This blog post is 9 years old! Most likely, its content is outdated. Especially if it's technical.

Powered by Fusion×

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


rm -f foobar.html will not complain if the file doesn't exist
Thank you for posting a comment

Your email will never ever be published

Related posts

"Confessions of a College Callgirl" is the new "My Secret Life As A Prostitute" 02 March 2008
See you at PyCon 2008 11 March 2008
And bash basics 16 October 2015
set -ex - The most useful bash trick of the year 31 August 2014
How I do deployments 16 December 2013
Bash tip of the day: ff 25 March 2011
pwdf - a mix of ls and pwd 07 April 2008
Catching a carriage return in bash 23 October 2006
type - Writing shell scripts 28 April 2006
Case insensitive list remove call (part II) 11 April 2006
Case insensitive list remove call 10 April 2006
Mvbackupfiles - a script to "delete" back autosaved backup files 24 November 2005