How to get ddMMMyyyy format using SH
Hi i Would like to know how do we get
ddMMMyyyy ( 21Nov2006 ) format using date function using sh. what i want to do is actually i got several files such as below 1. server.log.01Oct2006 2. server.log.02Oct2006 3. server.log.03Oct2006 4. and so on i would like to create a script using sh that will backup ( tar and gzip ) all old log files excluding the last one week so if the script runs at 04/12/2006 it will let the log server.log.04Dec2006 - server.log.28Nov2006 still in place while any server.log below 28Nov2006 will be remove to some other backup folder and tar it. And how do i subtract date in sh? currently i m doing it manually by using my own logic which i think not that elegant. ( simple integer substraction if else and so on ) Thanks. |
well you can use date +%d%b%Y, but i'd think for the problem you're explaining you should actually be looking at getting logrotate to do this for you.
|
Hi thanks
i m actually using log4j in java and in Solaris We are using DailyLogAppender rather than logrotate so we can keep the log if the user report some problem few weeks ago for example? so basically we need to have a cron job running to backup old logs in some backup folder first ( tar and gzip ) and eventually maybe we will delete those files.. thanks for the tips on date format? how do we do date substraction in UNIX? so lets say 01Dec2006 and i do some date function substract by 1 day it will become 30Nov2006 automatically? Thanks. |
Not sure if this is appropriate for your problem, but it's such great advice I can't resist the urge to splurt it out. If you're making files with dates as part of the name consider using YYYYMMDD (%Y%m%d).
It's understandable by people who speak different languages, There's no risk of case sensitive pattern matches missing a target, and best of all, dictionary sort order is also date sort order. It's also one character shorter than DDMONYYYY. |
instead of trying to choose your files based on the file name, use the file modification date and the find command to compress the files.
find $logdir/server.log* -type f -mtime +7 -exec bzip2 {} \; that will find any files in the $logdir and bzip them if they are older than 7 days. --dan |
Highly recommend matthewg42's date format suggestion. It's a very common & useful convention.
|
All times are GMT -5. The time now is 06:47 AM. |