6 Answers
6

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:

Moving the file & touching a new file (hence, changing the file descriptor) may prevent existing apps from continuing to write to the file, so that generally is undesirable. If a copy if the existing file is required, you can do something like (although, note it's not atomic, so some date may be duplicated in both files):

$ cp myfile myfile-$(date '+%F') && cat /dev/null > myfile

And, as mentioned, 'cron' jobs do not usually run in the directory you expect; but you can "cd" to your desired directory in the script to do the work & still use relative paths.