Do not try to check if the file exists, just try deleting it.
rm -f /p/a/t/h
Note that the second command will fail (return a non-zero completion status) if the file does not exist, but the first will succeed thanks to -f (short for --force ). Depending on the situation, this can be an important detail.
But most likely, if you add a file, it is because your script uses >> to redirect something to the file. Just replace >> with > . Itβs hard to say since you did not provide the code.
Note that you can do something like test -f/p/a/t/h && rm/p/a/t/h , but doing this is completely pointless. It is possible that the test will return true, but / p / a / t / h will not exist before you try to delete it, or, even worse, the test will fail and / p / a / t / h will be created. before executing the next command, which expects it to not exist. Trying is a classic race condition. Do not do this.
William Pursell Jul 09 '15 at 12:59 on 2015-07-09 12:59
source share