Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting how old a file is

Status
Not open for further replies.

gonzilla

Programmer
Apr 10, 2001
125
US
Hi,

I'm writing a script that uses 4 known files as input. I'd like to check to see if the files are over a day old so I can delete them because if they aren't over a day old, then there is no reason to recreate the file (which takes some time with sort and uniq and join etc...)

So, is there anyway in script to say something like,

if the specific file is over 1 day old, delete it.

...and do this for 4 different specific files?

Thanks for your help.

-Tyler
 
find ${WORK_DIR} -mtime +1 -exec rm -f {}

where ${WORK_DIR} is the directory.
You can get filename specific with -name option. Be aware that in HPUX, asterisks if
used as wildcards in this command should be escaped. Mtime is used for "modified time"
ctime is used for "creation time". Number
following is in days as measured in 24 hour
periods, so a file created yesterday before 5 PM will not be deleted before 5PM today.


 
You can use the "find" command for this (see the manpage 'man find' for details). A command such as:

find -name file1 -mtime +1 -exec rm {}\;

says find a file named file1 with a modification time of more than 1 day and remove it. You can test the outcome by replacing "rm" with "ls" which will list the files instead of removing them. If the files have a common rootname, for example chapter1, chapter2, chapter3 etc.. you can use a wildcard like "-name chapter*" Hope this helps. d3funct
zimmer.jon@cfwy.com
The software required `Windows 95 or better', so I installed Linux.

 
Sorry, I forgot to put the location in the find command. It should be as rarnold1 stated:

find [directory to search] -name [name of file] -mtime +1 -exec rm {} \; d3funct
zimmer.jon@cfwy.com
The software required `Windows 95 or better', so I installed Linux.

 
Here is something else you can use for discrete files.

You say you have a fixed list of files right?
If you have a tcl interpreter installed on your machine
this is pretty handy.

#!/bin/sh
#starts tclsh
exec tclsh $0 ${1+$@}

#globals
array set arr {}
set f_names [lindex [lrange $argv 0 3]]

proc fil_in {arr S} {
upvar 1 $arr loc_arr
foreach name $S {
if {[file isfile $name]} {
set loc_arr($name) [clock format [file mtime $name]]
} else {
catch {puts "$name does not appear to be a valid file.\n"}
}
}
puts "[array size loc_arr]."
}

proc gt_cans {L} {
global arr
set cands {}
foreach X [lsort $L] {
lappend cands $X
}

if {![catch {fil_in arr $cands}]} {
foreach J [array names arr] {
puts "$J was modified $arr($J)."
}
} else {
error "Errors occurred during fil_in."
exit
}

return
}

gt_cans $f_names

You can call the script like:
scriptname file1 file2 file3 file4.

The output looks like this:
/home/user/file was modified Sun Dec 16 16:26:22 EST 2001.
/home/user/file1 was modified Tue Dec 31 03:23:55 EST 2001
etc...

























 
Thanks everyone for your help!

I'll give them a try.

On a second note...is there any way to speed up cutting, sorting, and using uniq with delimited fields from large files without having to write a whole program to do it?

Ex.
----------------------------
cut -f1,3,4,5,8,79,92 -d',' accounts.dat | sed 's/,/ /g' | sed 's/###//g' > tzout.temp

cut -f1,7 -d' ' tzout.temp | sort -n | uniq > used.temp

cut -f5,2,8,9,13 -d',' records.log | sed 's/,/ /g' | sort +1 -2 > rec.temp
----------------------------

With an accounts.dat file of only 7000 records (size 2001444 @ 2MB) it takes 5-6 seconds. The files I will use will gernerally be larger.

Any help would be great.

Thanks again.

-Tyler



 
I think awk would be very suitable for what you are doing there. As far as speed goes, sed is very fast usually....
If you have some data examples there is an awk forum that could help you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top