Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 12-04-2006, 06:59 AM   #1
LQ Newbie
Registered: Dec 2006
Posts: 24

Rep: Reputation: 15
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 )

Old 12-04-2006, 07:08 AM   #2
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
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.
Old 12-04-2006, 07:12 AM   #3
LQ Newbie
Registered: Dec 2006
Posts: 24

Original Poster
Rep: Reputation: 15
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?

Old 12-04-2006, 08:20 AM   #4
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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.
Old 12-04-2006, 02:04 PM   #5
LQ Newbie
Registered: May 2006
Posts: 15

Rep: Reputation: 0
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.

Old 12-04-2006, 05:57 PM   #6
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,406

Rep: Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396
Highly recommend matthewg42's date format suggestion. It's a very common & useful convention.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
format a hard drive (/dev/hda1) in Reiser fs format Linh Linux - General 7 06-17-2009 06:30 AM
Converting wma format to mp3 format. fakie_flip Linux - General 6 10-02-2005 07:49 PM
replace empty function from the old format to the new format powah Programming 4 07-12-2005 08:07 PM
use vi to convert linux format to windows format intolinux Linux - Software 2 12-10-2003 10:20 PM
Format Problems Saving Star Office Docs in Word Format kkempter Linux - Software 4 08-27-2003 04:23 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:34 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration