shell script to remove a file if it already exist

Shell

Shell Problem Overview


I am working on some stuff where I am storing data in a file. But each time I run the script it gets appended to the previous file.

I want help on how I can remove the file if it already exists.

Shell Solutions


Solution 1 - Shell

Don't bother checking if the file exists, just try to remove it.

rm -f /p/a/t/h
# or
rm /p/a/t/h 2> /dev/null

Note that the second command will fail (return a non-zero exit status) if the file did not exist, but the first will succeed owing to the -f (short for --force) option. Depending on the situation, this may be an important detail.

But more likely, if you are appending to the file it is because your script is using >> to redirect something into the file. Just replace >> with >. It's hard to say since you've provided no code.

Note that you can do something like test -f /p/a/t/h && rm /p/a/t/h, but doing so is completely pointless. It is quite possible that the test will return true but the /p/a/t/h will fail to exist before you try to remove it, or worse the test will fail and the /p/a/t/h will be created before you execute the next command which expects it to not exist. Attempting this is a classic race condition. Don't do it.

Solution 2 - Shell

Another one line command I used is:

[ -e file ] && rm file

Solution 3 - Shell

You can use this:

#!/bin/bash

file="file_you_want_to_delete"

if [ -f "$file" ] ; then
    rm "$file"
fi

Solution 4 - Shell

If you want to ignore the step to check if file exists or not, then you can use a fairly easy command, which will delete the file if exists and does not throw an error if it is non-existing.

 rm -f xyz.csv

Solution 5 - Shell

A one liner shell script to remove a file if it already exist (based on Jindra Helcl's answer):

[ -f file ] && rm file

or with a variable:

#!/bin/bash

file="/path/to/file.ext"
[ -f $file ] && rm $file

Solution 6 - Shell

Something like this would work

#!/bin/sh

if [ -fe FILE ]
then 
    rm FILE
fi 

-f checks if it's a regular file

-e checks if the file exist

>Introduction to if for more information

EDIT : -e used with -f is redundant, fo using -f alone should work too

Solution 7 - Shell

if [ $( ls <file> ) ]; then rm <file>; fi

Also, if you redirect your output with > instead of >> it will overwrite the previous file

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNiksView Question on Stackoverflow
Solution 1 - ShellWilliam PursellView Answer on Stackoverflow
Solution 2 - Shellliwp_StephenView Answer on Stackoverflow
Solution 3 - ShellJindra HelclView Answer on Stackoverflow
Solution 4 - ShellgreperrorView Answer on Stackoverflow
Solution 5 - ShellnbeuchatView Answer on Stackoverflow
Solution 6 - ShellP1kachuView Answer on Stackoverflow
Solution 7 - ShellnLeeView Answer on Stackoverflow