This question already has an answer here:

I want to empty current file so I do: touch it, remove it and touch because I do not want to remove non-existing file; example

           touch "$filenameTarget"
           rm "$filenameTarget"
           touch "$filenameTarget"

I want to be very careful with deletes. What is the correct philosophy here?

OS: Debian 8.5

marked as duplicate by don_crissti, Christopher, Stephen Kitt, HalosGhost, Satō Katsura Oct 31 '16 at 20:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Simply write nothing to the file:

:> "$filenameTarget"

This will empty the file if it already exists, and create it (empty) if it doesn't. You need to have the appropriate permissions (the file must be writeable if it already exists, the containing directory must be writeable if the file doesn't already exist).

  • 1
    @Masi, yes, it will be an empty file. 0 bytes size. – juankvillegas Oct 31 '16 at 19:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.