LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2009, 07:39 PM   #1
coolplanet
LQ Newbie
 
Registered: Apr 2009
Posts: 20

Rep: Reputation: 0
cat file with its modified date n time


Dear sir,

I wish to cat those file with 90 days old to a filename with its modified date then null it, is it possible? eg

for maillog in $(find /var/log -name "maillog*" -type f -mtime +90);do
cat $maillog > $maillog.(its modified date n time)
sleep 5
cat /dev/null > $maillog
done

Regards,
 
Old 10-24-2009, 07:53 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
It might be better to pipe the find into a loop, i.e. find ... | while read maillog; do, in case you have a ridiculous number of files (this will also allow for spaces in file names.) You can get the file date/time using stat, which you might use with date for a custom format:
Code:
mod_date="$( date -d "$( stat "$maillog" -c%z )" +%Y%j%H%M )"
new_file="$maillog.$mod_date"
Kevin Barry

Last edited by ta0kira; 10-24-2009 at 08:02 PM.
 
Old 10-24-2009, 11:56 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can do that with GNU find's printf

Code:
find . -type f -mtime +90 -printf "%p:%t\n"
check the man page for more printf option.
 
Old 10-25-2009, 12:34 AM   #4
coolplanet
LQ Newbie
 
Registered: Apr 2009
Posts: 20

Original Poster
Rep: Reputation: 0
mod_date="$( date -d "$( stat "$maillog" -c%z )" +%Y%j%H%M )"
new_file="$maillog.$mod_date"

I change to +%Y%m%d%H%M and it work fine to me. Thanks for the great help!
 
Old 10-25-2009, 12:52 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by ghostdog74 View Post
you can do that with GNU find's printf

Code:
find . -type f -mtime +90 -printf "%p:%t\n"
check the man page for more printf option.
This would require that the file name be parsed out in order to be read, however, and it doesn't allow a custom format.
Kevin Barry
 
Old 10-25-2009, 06:58 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by ta0kira View Post
This would require that the file name be parsed out in order to be read, however, and it doesn't allow a custom format.
Kevin Barry
what do you mean "file name to be parsed out" ? if you are talking about that %t part, its just an example.
this
Code:
find /path -type f -name "$maillog" -printf "Name:%f, Modified time: %AY%Aj%AH%AM\n"
does the same thing as
Code:
date -d "$( stat "$maillog" -c%z )" +%Y%j%H%M

Last edited by ghostdog74; 10-25-2009 at 07:01 PM.
 
Old 10-25-2009, 07:36 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The point is that the loop reads one line at a time, whether that line be the file name, the date, or a combination. The loop requires the file name by itself, regardless; therefore, if the date is also fed to the loop, it will be on the same line as the file name. That means the file name must be parsed out of the current line with e.g. cut, or the file name must be sent to the loop by itself and the date for the file must be found from within the loop.
Kevin Barry
 
Old 10-25-2009, 08:44 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
 find . -type f -name "*log" -printf "cp '%h'/'%f' '%h'/'%f_%AY%Aj%AH%AM'\n" | bash
 
  


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
change modified date stamp of file B-Boy Linux - Newbie 2 08-21-2008 07:33 AM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 09:59 AM
[bash] Seeing if a file has been modified before/after a date w3stfa11 Programming 7 10-15-2006 06:02 PM
php read file creation, modified date problem antony_csf Programming 3 08-18-2004 06:46 AM
Where can I look for the config file for modified date and time ForumKid Linux - General 1 09-19-2003 08:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:13 AM.

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