File check before delete

07 March 2008   1 comment   Linux

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

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
Related by keywords:
Read in passwords with bash 25 March 2005
set -ex - The most useful bash trick of the year 31 August 2014
How I do deployments 16 December 2013
Catching a carriage return in bash 23 October 2006
ztar - my wrapper on tar -z 29 June 2005
Case insensitive list remove call 10 April 2006
Bash tip of the day: ff 25 March 2011
Mvbackupfiles - a script to "delete" back autosaved backup files 24 November 2005
pwdf - a mix of ls and pwd 07 April 2008
type - Writing shell scripts 28 April 2006
Furious and deprived by 'rm *' 15 October 2005
Case insensitive list remove call (part II) 11 April 2006