shell script to remove a file if it already exist

2020-05-14 04:16发布

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
7条回答
走好不送
2楼-- · 2020-05-14 05:12

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
查看更多
登录 后发表回答