On CentOS 6 Linux I use date +%a in several cronjobs and scripts to generate logfile names like myprog-Tue.txt and this works well for me since many years.

But now for one program I need to delete the previous logfiles, i.e. in my cronjob before I start my program and redirect its output to myprog-Tue.txt, I'd like to delete the myprog-Wed.txt.

This way I'd like to get the old logfiles out of my way, but still have 5-6 days of logfiles around.

So I have tried:

# date -v+1d +%a
date: invalid option -- 'v'
Try `date --help' for more information.

Does anybody know what's wrong here please?

  • 2
    You may want to post your Linux questions here: unix.stackexchange.com Aug 1, 2014 at 16:03
  • @JasonEnochs There is nothing against posting Linux questions here on SuperUser, even though a more specialized site exists. (Same for Unix & Linux and Ask Ubuntu.) Alexander, whatever you do, do not simply post the same question on multiple sites in the network. If you want your question migrated to a different site, "flag" it for moderator attention using the custom reason option, ask for migration, and the moderators will take a look and take appropriate action.
    – user
    Aug 1, 2014 at 16:22
  • 4
    IMHO this is a bad idea to have the name-of-week. what if you have two weeks of logs? what if you restart during the day. At best both scenarios above would be confusing, at worst you'd (possibly) overwrite the existing file. Aug 1, 2014 at 17:28

4 Answers 4


You would do that like this:

date -d "+1 days" +%a

That renders:



The problem is not how you can use date to output what you want... your problem is:

This way I'd like to get the old logfiles out of my way, but still have 5-6 days of logfiles around.

So, why not using find to remove all files but this week's?

find /path/to/files/ -mtime +7 -exec rm {} \;

In addition, date has many different implementations - I remember the last time I used it it was kind of hard to find the right flags to do what I wanted... and it was not easy to make it work the same way between a Linux and an AIX machine... so, just in case this comment helps you to decide against date :)

  • 1
    +1 for solving the problem and not just answering the question :)
    – user
    Aug 1, 2014 at 16:23
  • Yes, thanks - I use similar "find/xargs" commands elsewere too. But in this case I don't want to rely on "mtime", because I sometimes edit/look at the logfiles with "vi". Aug 1, 2014 at 16:27
  • 1
    @AlexanderFarber this shouldn't change mtime unless you save the file. Were you perhaps thinking of atime? Also, maybe you should use view instead of vi, view opens readonly by default. Aug 1, 2014 at 17:25
  • This might help to understand atime, mtime, and ctime: unix.stackexchange.com/a/91200/74190. Also, Ext4 and other file systems seem to store the creation date for the files - maybe that's something you can take advantage of (if using any of those filesystems, though).
    – jimm-cl
    Aug 1, 2014 at 17:29
  • 2
    @jim BTW: +1 for AIX... I was stuck using it years ago. Also, since this is Linux, you can rely on the -delete operator for (GNU) find. You can replace the | xargs rm {} \; with -delete and not be stuck with quoting rules or -no-run-if-empty. Aug 1, 2014 at 17:37

Use the -d option to specify a phrase:

date -d '+ 1 day' +%a

Another entry for "how to sanely manage your log files"....

You can look at logrotate which should be part of most Linux distributions. It can manage deletion and compression. A lot of Linux daemons use this, so you can look at some example config files.

A bit simpler is rotatelogs which handles the rotation part, but doesn't do any deletion (your find ... -delete would do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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