LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-24-2006, 12:04 PM   #1
spiffytech
Member
 
Registered: Dec 2005
Location: NC, USA
Distribution: (K)ubuntu
Posts: 139

Rep: Reputation: 15
How do I insert timestamps in bash?


I am writing a script that concatenates several log files that accumulate over the week into a single file and archives that concatenated file.

I want to be able to name files with a date range, like 01/17/06-01/23/06. I also want to be able to post dates into the concatenated file, like this:

Quote:
cat Sunday [sundays_date]> catted

cat file1 >> catted

cat Monday [mondays_date{ >> catted

cat file2 >> catted
etc, etc...


How do I put the dates in like this?
 
Old 01-24-2006, 12:36 PM   #2
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
I suggest you to use a date format that do not have slashes in it, because it could make the handle of file a bit more difficult, since its get confused with the path specification: for instance, in the hypothetical file /dir/dir2/01/17/06-01/23/06, "01/17/06" is part of path or part of filename ?
So, I suggest you to use a military format like 20060117-20060123. A good side effect is those files are sorted in a chronological way, rather than a numeric way.
To get a date formated in such way, use the formating capabilities of date.
$ date +%Y%m%d
20060124
When you create the Sunday file, create it with this timestamp in the name so you will be able to extract it later to create the composite version 20060117-20060123 you wanted.

I am not sure I understand "be able to post dates into the concatenated file", but "date +%Y%m%d > catted" will create an empty file with the string "20060124" in the first line.

I hope this can help you.

Last edited by marozsas; 01-24-2006 at 12:40 PM.
 
Old 01-24-2006, 12:56 PM   #3
spiffytech
Member
 
Registered: Dec 2005
Location: NC, USA
Distribution: (K)ubuntu
Posts: 139

Original Poster
Rep: Reputation: 15
I understand how to insert dates into the file now. Do you know of any logic to work with dates in the past? I need to be able to assign a date to each day of the week. I need to be able to do Sunday, [last Sunday's date] for each day of the week.

This script will run at about midnight Sunday morning every week, so the script needs to be able to automatically calculate the dates for the past week, accounting for months and years. Is there some script code somewhere that already does this?
 
Old 01-25-2006, 04:18 AM   #4
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
Uhmmm.... Let say you already have the dates you want. Lets say today is Sunday Jan 29. What if you already have a file named lastSunday with the string 20060122 on it (and a lastMonday, etc) ? So you can use its contents to your purposes, right ?
My idea is to have two set of scripts. The one you are writing to deal with the files. The another one, runs everyday and just create the files lastMonday, lastTuesday, etc with the strings on it. When Sunday comes in, you already have all files with the strings on it. It's helps ?
 
Old 01-26-2006, 07:16 PM   #5
spiffytech
Member
 
Registered: Dec 2005
Location: NC, USA
Distribution: (K)ubuntu
Posts: 139

Original Poster
Rep: Reputation: 15
I figured it out... Just use the date program.

"date -d +%Y%m%d --date '7 days ago'"
 
Old 01-27-2006, 04:00 AM   #6
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
Ohhh ! it's new for me ! never stop learning..
 
  


Reply



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
solaris ping timestamps plata Solaris / OpenSolaris 8 05-27-2005 01:36 PM
no file access timestamps being updated! takahaya Linux - General 4 04-03-2005 06:48 PM
VSFTPD and file timestamps t3___ Linux - Newbie 4 01-21-2004 02:26 PM
squid timestamps chamkila Linux - General 1 05-08-2003 08:59 AM
Timestamps Config Programming 2 04-22-2002 02:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:22 PM.

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