shell script to remove a file if it already exist

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.


我使用的另一条命令是:

[ -e file ] && rm file

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

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.


你可以使用这个:

#!/bin/bash

file="file_you_want_to_delete"

if [ -f $file ] ; then
    rm $file
fi
链接地址: http://www.djcxy.com/p/24128.html

上一篇: 如何检查文件是否存在于shell脚本中

下一篇: shell脚本删除文件,如果它已经存在