Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
I'm surprised you say that sed 1,14d file > file works: the shell will see the redirection and truncate the file before executing the sed command. – glenn jackman Mar 30 '11 at 17:43
up vote 4 down vote accepted

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
share|improve this answer
Specified the full path and replaced sed with echo and now it works fine. Thank you – zozo Mar 30 '11 at 11:34
Cool, glad it was a simple fix. – db2 Mar 30 '11 at 12:15
Just thought I'd mention that even though this may fix your problem, it does not empty the file, but rather leaves a newline char in the file. – beans Mar 30 '11 at 14:40

You can try a simple >file.txt

$ cat file.txt
this is a test
$ >file.txt
$cat file.txt
share|improve this answer
This is the most relevant one as it dose empty the file. – fossilet Mar 30 '11 at 15:12

You can also use logrotate, which gives you the option of not just emptying the file, but archiving (e.g., compressing) the previous contents of the file, and even mail the file to you.

share|improve this answer

The traditional un*x way to "empty" a file:

$ cat /dev/null > myfile

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.

share|improve this answer

I would do this: mv file file.bak && touch file && rm file.bak

I move the file first, in case the system happens to have it open.

share|improve this answer

You can even use truncate(1), if your system has it.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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