Bash – How to periodically remove all content from a file without deleting it

bashcronscripting

I need to periodically empty a text file without deleting it (I know how many lines it has).

I tried this:

Created a cron that executes the following script

other lines from the script here

sed '1,14d' file.txt>file.txt

If I run it from a terminal it works fine, but when I run the cron it executes the first part (the "other lines from script here" part) but it doesn't empty the file.

Note: The file has 777 permissions so I don't think it's permissions related.

Best Answer

Maybe the current working directory isn't what you're expecting when cron executes the script. Try specifying the full path to file.txt. Also, this should empty the file as well, just in case there's some issue with the sed command:

echo > file.txt
Related Topic