shell script to remove a file if it already exist

前端 未结 7 1123
面向向阳花
面向向阳花 2021-01-30 15:43

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 f

7条回答
  •  悲&欢浪女
    2021-01-30 15:53

    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
    

提交回复
热议问题