|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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....... |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
Quote:
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 ![]() |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > compare date |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|