2

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

1 Answers1

4

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).

Stephen Kitt
  • 434,908