If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Unix-shell script for puging files

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-07-06, 13:39
vkaramched vkaramched is offline
Registered User
 
Join Date: Jan 2003
Location: Atlanta
Posts: 134
Angry Unix-shell script for puging files

Can someone help with the script to do:
purge/delete files in a directory with a known ( .txt ) extention which are older than 3 ( this can vary like 2 hours or 5 hours ) hours.

Any help is appreciated.

Thanks, Vinnie
Reply With Quote
  #2 (permalink)  
Old 09-07-06, 16:40
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Cool


Try this:
PHP Code:
#!/usr/bin/ksh
CURTIME=$(date '+%m%d%H%M')
(( 
REQTIME CURTIME-200 ))
touch ---t 0$REQTIME HOURFILE
find 
. ! -newer HOURFILE -type f -name '*.txt' -exec rm {} \;
rm -f HOURFILE 


__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #3 (permalink)  
Old 09-07-06, 19:05
vkaramched vkaramched is offline
Registered User
 
Join Date: Jan 2003
Location: Atlanta
Posts: 134
Unhappy Thanks for the script BUT !!

I suspect when the date is like: (CURRTIME=09010001) ie: 09/01 00:01 midnight past then it is failing on the touch format with invalid time string. It can happen on 10/01 00:01 and so on. Will you please check this and give a fix?

Thanks, Vinnie
Reply With Quote
  #4 (permalink)  
Old 09-08-06, 01:50
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
You can e.g. do the date calculation like this
# echo "curr date:" $(export TZ;TZ=GMT-2;date '+%Y%m%d%H%M')
curr date: 200609080748
# dt_3=$(export TZ;TZ=GMT+1;date '+%Y%m%d%H%M')
# touch -t $dt_3 xx
# ls -l xx
-rw-r--r-- 1 root other 0 Sep 8 04:49 xx

Edit: Note, GMT+4 mean you need to add 4 hours to local time to get to GMT time. The GMT in GMT+4 is just a tag and can be anything.
With daylight savings time you’ll have to sometimes use GMT+7 and other time GMT+8 so first check your local time zone (EDT/EST)

Last edited by pdreyer; 09-08-06 at 02:23.
Reply With Quote
  #5 (permalink)  
Old 09-08-06, 07:43
vkaramched vkaramched is offline
Registered User
 
Join Date: Jan 2003
Location: Atlanta
Posts: 134
Unhappy Thanks for reply again but

the problem is not in calculation of curr_time but the calculation of time by using LKBRWN code as:

CURTIME=$(date '+%m%d%H%M')
(( REQTIME = CURTIME-200 ))
touch -m -a -t 0$REQTIME HOURFILE

When the currtime=09010010 ( Say on Sept 01, 00:10 just past mid-night ) and when you subtract 200 ( I guess for 2 hour files that I want to keep ) then the REQTIME value will not be in the FORMAT that "touch command" can be executed. Most of times it works but in such date/times I see a problem. Any fixes or ideas to address this is greatly appreciated.
My original request was: How do I create a shell program that searches one or more directories in Unix and purge criteria files that are OLDER THAN ( let us say 2 hours and this 2 hours need to be configurable like 3 hours or 4 hours etc. ).

Thanks, Vinnie
Reply With Quote
  #6 (permalink)  
Old 09-08-06, 08:22
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Hi, in Linux (RedHat 7) I can use
Code:
find /dir -amin -240 -a -amin +180 -exec rm {} \;
to delete files in /dir that were last accessed (-cmin for last changed) between 180 and 240 minutes ago. On my AIX 4.1 system -amin or -cmin are no implemented tests though. Maybe in your system it's available...

Grts

BTW: If you run your script every x hours with crontab why wouldn't you leave the reference file in LKBRWN's example on disk and just update it's timestamp at the end of the script with the current time, like:
Code:
#!/usr/bin/ksh

[ -f "${1}HOURFILE" ] && find /dir ! -newer "${1}HOURFILE" -type f -name '*.txt' -exec rm {} \;
touch "${1}HOURFILE"
and do the interval assignment through a parameter ($1) when invoking the script?

Last edited by Tyveleyn; 09-08-06 at 09:04.
Reply With Quote
  #7 (permalink)  
Old 09-08-06, 09:55
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
Quote:
Originally Posted by vkaramched
the problem is not in calculation of curr_time but the calculation of time by using LKBRWN code
That is why I used TZ
e.g. now is:
# date -u
Fri Sep 8 13:52:20 GMT 2006

And 500 hours back in time is:
# echo $(export TZ;TZ=GMT+500;date)
Fri Aug 18 17:52:31 GMT 2006
Reply With Quote
  #8 (permalink)  
Old 09-08-06, 13:05
vkaramched vkaramched is offline
Registered User
 
Join Date: Jan 2003
Location: Atlanta
Posts: 134
Appreciate your suggestions

And look at these files in a test directory and files. The file listing is given below. This is AIX-5.3. As I understand from the commands given am I not supposed to get the list of files : xyz and zz which fall within the 180 and 240 minutes range? Am I missing something from your suggestions?

Vinnie


(mmprod):/home/oracle/vinnie1> find . -amin -240 -a -amin +180 -exec ls -l \;
(mmprod):/home/oracle/vinnie1> find . -cmin -240 -a -cmin +180 -exec ls -l \;
(mmprod):/home/oracle/vinnie1> find . -amin -240 -a -amin +180 -exec ls -l \;
(mmprod):/home/oracle/vinnie1> ls -l
total 94
drwxr-xr-x 2 oracle dba 512 Sep 08 12:48 ./
drwxr-xr-x 11 oracle dba 15360 Sep 08 12:00 ../
-rwxrwxrwx 1 oracle dba 788 Sep 06 21:11 a.sh*
-rw-r--r-- 1 oracle dba 328 Sep 06 21:11 outfile.log
-rw-r--r-- 1 oracle dba 0 Sep 08 09:30 xxx
-rw-r--r-- 1 oracle dba 19134 Sep 08 12:46 xyz
-rw-r--r-- 1 oracle dba 0 Jan 01 2006 yyy
-rw-r--r-- 1 oracle dba 11055 Sep 08 12:46 zz
(mmprod):/home/oracle/vinnie1>
(mmprod):/home/oracle/vinnie1> date
Fri Sep 8 13:03:08 EDT 2006
(mmprod):/home/oracle/vinnie1>
Reply With Quote
  #9 (permalink)  
Old 09-08-06, 13:17
vkaramched vkaramched is offline
Registered User
 
Join Date: Jan 2003
Location: Atlanta
Posts: 134
Red face May be I was too soon to write before

but the command following is it not supposed to filter out and give me the file names: between 30 minutes and 480 minutes ? which should list the files: zz and xxx ? I tried the options -amin and also -cmin with the same criteria but get the same results. Any clarification on this?

Thanks, Vinnie

(mmprod):/home/oracle/vinnie1> find . -mmin -480 -a -mmin +30 -exec ls -l \;
total 63
-rwxrwxrwx 1 oracle dba 788 Sep 06 21:11 a.sh
-rw-r--r-- 1 oracle dba 328 Sep 06 21:11 outfile.log
-rw-r--r-- 1 oracle dba 0 Sep 08 09:30 xxx
-rw-r--r-- 1 oracle dba 19134 Sep 08 12:46 xyz
-rw-r--r-- 1 oracle dba 0 Jan 01 2006 yyy
-rw-r--r-- 1 oracle dba 11055 Sep 08 12:46 zz
(mmprod):/home/oracle/vinnie1>
Reply With Quote
  #10 (permalink)  
Old 09-08-06, 13:55
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Don't forget the {} as parameter for the input lines...
Code:
find . -amin -480 -a -amin +30 -exec ls -l {} \;
, works on Debian too.

Grts
Reply With Quote
  #11 (permalink)  
Old 09-08-06, 14:39
vkaramched vkaramched is offline
Registered User
 
Join Date: Jan 2003
Location: Atlanta
Posts: 134
Unhappy Sorry for my mistake and good point

(mmprod):/home/oracle/vinnie1> ls -l
total 94
drwxr-xr-x 2 oracle dba 512 Sep 08 12:48 ./
drwxr-xr-x 11 oracle dba 15360 Sep 08 14:29 ../
-rwxrwxrwx 1 oracle dba 788 Sep 06 21:11 a.sh*
-rw-r--r-- 1 oracle dba 328 Sep 06 21:11 outfile.log
-rw-r--r-- 1 oracle dba 0 Sep 08 09:30 xxx
-rw-r--r-- 1 oracle dba 19134 Sep 08 12:46 xyz
-rw-r--r-- 1 oracle dba 0 Jan 01 2006 yyy
-rw-r--r-- 1 oracle dba 11055 Sep 08 12:46 zz
(mmprod):/home/oracle/vinnie1>

Last edited by vkaramched; 09-08-06 at 14:47.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On