UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old January 6th, 2005, 01:31 AM
izza_azhar izza_azhar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 49 izza_azhar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 33 m 57 sec
Reputation Power: 4
compare date

let say file name is
'file_20040101.txt'
'file_20040802.txt'
'file_20041202.txt'
'file_20050101.txt"

where '20040101' is date for that file.
and todays date is 2006106.

how we compare the date?
where the ouput will remove file before december 2004?

in this case, 'file_20040101.txt', 'file_20040802.txt' will be remove and other file will remain in the directory.
pleaseeeeee.......

Reply With Quote
  #2  
Old January 6th, 2005, 08:13 PM
dynamicvb dynamicvb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 45 dynamicvb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 40 m
Reputation Power: 5
Not sure how to do the compare, but I think there's another way if the file names corelate to the last time the file was written to. You should be able to use find for this. Sometihng like

find /yourFilePath -name '*.txt' -mtime +TheNumberOfDaysYouWantToDelete -exec rm {} \\;


look at the man pages for find and you will probably find what you want there. This is for AIX, you may need to modify a little for other flavors.

Reply With Quote
  #3  
Old January 7th, 2005, 04:41 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
Quote:
Originally Posted by izza_azhar
let say file name is
'file_20040101.txt'
'file_20040802.txt'
'file_20041202.txt'
'file_20050101.txt"

where '20040101' is date for that file.
and todays date is 2006106.

how we compare the date?
where the ouput will remove file before december 2004?

in this case, 'file_20040101.txt', 'file_20040802.txt' will be remove and other file will remain in the directory.
pleaseeeeee.......



Hi,

the way dynamicvb suggests is probably the best one. But if you must get the date from the file name you must make a little drill in shell programming. For instance:


#!/usr/bin/ksh
# Script to delete files older than the given date
# Tested under AIX

# Function to print the digital substring from the first parameter
get_date() {
echo $1 |
awk '
{ if (match($0, "[0-9][0-9]*"))
print substr($0, RSTART, RLENGTH)
}
'
}

# Here we go
[ $# -eq 1 ] || {
echo "$0: usage: $0 date_limit" >&2
exit 1
}

LIMIT=$( get_date $1 )

# You should test the given parameter, here the length test only
# Bad parameter can cause a disaster, rm is irreversible!!
LENGTH=$( expr $LIMIT : [0-9][0-9]* )
[ $LENGTH -ne 8 ] && {
echo "$0: bad date limit given: $1" >&2
exit 1
}

# You must be in the appropriate directory (do cd ... first)
# or change the following find accordingly
find . -name "file_*.txt" |
while read F; do
DATE=$( get_date $F )
[ "$DATE" -lt "$LIMIT" ] && {
# Here echo the command only. After testing, uncomment following line
# rm $F
echo rm $F
}
done


Good luck

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > compare date


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway