Is it better to check if file exists before deleting it
- by Kevin Fegan
Sometimes when I want to delete a file (from within a script), I will just delete it rather than checking if it exists first. So I do this:
$ rm "temp.txt" 2>/dev/null
Instead of this:
[ -f "temp.txt" ] && rm "temp.txt"
I just feel it's a waste of time to go and check if the file exists and return an exit code.
So, perhaps it's quicker to do it the first way, especially if most of the time, the file is likely to be present.
Are there any other advantages (or downsides) to do it one way or the other?
Am I wrong to think it will ever be quicker?