Is it better to check if file exists before deleting it
        Posted  
        
            by 
                Kevin Fegan
            
        on Super User
        
        See other posts from Super User
        
            or by Kevin Fegan
        
        
        
        Published on 2014-05-31T19:59:55Z
        Indexed on 
            2014/06/01
            3:31 UTC
        
        
        Read the original article
        Hit count: 304
        
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?
© Super User or respective owner