Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
3  
Alternative: tail -n +1000 file.txt > file.txt should remove 1000 lines and keep the newest lines in the file (you never know when you might need some of the recent record to track something). –  Rinzwind Sep 12 '13 at 8:50
2  
That is actually a better option than clearing the file on scheduled times. –  Bruno Pereira Sep 12 '13 at 8:54
    
@Rinzwind, that won't work since the file will be truncated before tail tries to read it. You need to redirect to a new file, and then replace the original with the new one. –  psusi Sep 12 '13 at 13:53
    
True @psusi :) there needs to be an extra redirect there. –  Rinzwind Sep 12 '13 at 13:59
add comment

4 Answers

up vote 3 down vote accepted

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"
share|improve this answer
    
what kind of permissions need to be altered because it says permission denied, i have done chmod 755 to the file already,or should i make it a new Post(i mean the permissions question) –  sosytee Sep 12 '13 at 10:37
    
@sosytee See my new edits. –  Radu Rădeanu Sep 12 '13 at 10:46
    
so this means this cannot be made a cronjob? –  sosytee Sep 12 '13 at 10:51
1  
@sosytee Of course you can, but use sudo crontab -e to add the cron job for root, not for you. Or change the file permission to 766 (or 666), not to 755: sudo chmod 766 filename. More about: codex.wordpress.org/Changing_File_Permissions –  Radu Rădeanu Sep 12 '13 at 11:03
    
truncate filename is a little cleaner and simpler than redirecting empty output to the file and doesn't run into the problems with sudo. –  psusi Sep 12 '13 at 13:54
show 1 more comment

There is a tool created for that purpose:

truncate -s 0 filename

This will clear the contents but it will still be the same file.

An arguably easy to remember way is this:

echo -n > filename

Note that truncate and echo -n are not defined by POSIX.

For POSIX compliance use cat /dev/null:

cat /dev/null > filename

Yet another way, more obscure and probably not shell agnostic:

> filename

If the file to truncate needs root privileges to write then the following lines will not work as probably expected:

sudo echo -n > filename
sudo cat /dev/null > filename
sudo > filename

This is because the redirection is done by the shell even before sudo is executed. That means that the write access to the file is done using the privileges of the user who runs the shell, not the privileges which will be granted by sudo.

A workaround is to defer the redirection to a shell executed using sudo privileges like this:

sudo sh -c "echo -n > filename"

Using truncate you do not need such a workaround because the write access is not done by the shell but by truncate which is executed by sudo using sudo privileges.

sudo truncate -s 0 filename
share|improve this answer
    
it says Permission denied, have used chmod 755 filename but still the permission is denied –  sosytee Sep 12 '13 at 8:49
2  
perhaps you have noclobber enabled in bash? –  lesmana Sep 12 '13 at 8:51
add comment

To automate this you can use logrotate. Just create a configuration file for the file you want to be emptied, add other settings and you can have it empty every day automatically based on criteria you choose f.e everyday, or when file is bigger than etc.

Take a look here on how to configure logrotate: http://linuxcommand.org/man_pages/logrotate8.html

share|improve this answer
1  
I favor this option myself. We use "file >10 Mb" -> "tar.gz it to backup" "empty file". And the backups get deleted after 900 days. –  Rinzwind Sep 12 '13 at 9:58
add comment

echo "" > foo_file is a simple way of doing that, it will replace all contents of foo_file with "" making it in this case, empty.

You can also use cat /dev/null > foo_file, that will read the null device and overwrite your file with nothing at all.

share|improve this answer
    
it says Permission denied, have used chmod 755 filename but still the permission is denied –  sosytee Sep 12 '13 at 8:55
    
Can you read the file permissions with your normal user please? –  Bruno Pereira Sep 12 '13 at 8:58
    
yes i can read the contents as normal user –  sosytee Sep 12 '13 at 9:02
add comment

Your Answer

 
discard

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.