Saturday, April 27, 2024
30
rated 0 times [  30] [ 0]  / answers: 1 / hits: 24994  / 2 Years ago, thu, july 28, 2022, 11:15:54

I have a text file in my home directory which receives data regularly through a cron job. Because the cron job provides dynamic data, I would also want to set a cron job to empty the file contents (the file must still exist). I don't need help with cron, just the command which can help with emptying the file.


More From » command-line

 Answers
5

The simplest way:



> filename


This is a less obvious way and probably less readable in a shell script by others, but it's all you need.



Because > is not actually a really command (it is bash builtin) you can't use:



sudo > filename


when you don't have permissions on that file. But you can use:



sudo bash -c "> filename"

[#29480] Saturday, July 30, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
listeerrated

Total Points: 354
Total Questions: 112
Total Answers: 100

Location: Guam
Member since Fri, Jun 18, 2021
3 Years ago
;