Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
2  
You may want to post your Linux questions here: unix.stackexchange.com –  Jason Enochs Aug 1 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. –  Michael Kjörling Aug 1 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. –  Rich Homolka Aug 1 at 17:28

4 Answers 4

up vote 5 down vote accepted

You would do that like this:

date -d "+1 days" +%a

That renders:

Sat

share|improve this answer
    
Thanks! I got confused by Mac's "date" :-) –  Alexander Farber Aug 1 at 16:29

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 :)

share|improve this answer
1  
+1 for solving the problem and not just answering the question :) –  Michael Kjörling Aug 1 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". –  Alexander Farber Aug 1 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. –  Rich Homolka Aug 1 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). –  jim Aug 1 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. –  Rich Homolka Aug 1 at 17:37

Use the -d option to specify a phrase:

date -d '+ 1 day' +%a
share|improve this answer

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.

share|improve this answer

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.