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!

File aging script 1

Status
Not open for further replies.

btinder

Programmer
Mar 12, 2004
27
US
Hi all,

I am working on a script that scans several directories looking for a particular file in each and see if the file is older than 30 minutes.

The filename is core.txt in all of the directories.

Currently, I have a script that works about 90% of the time, but lately it's been flaky, and after looking at the script (I didn't write it), I figured there must be a better way.

What's a quick and easy way to go about this?

I was thinking of this:

1. Touch a file with a timestamp exactly 30 minutes from the current time (not sure how to do this in KSH).

2. Get the last modified time of the core.txt file, and compare it like this:

if [[ core.txt -ot timestamp.txt ]] ; then

3. Touch a flag file to notify me that the file is older than 30 minutes old.

Notes:
The core.txt file is a file that collects output from a C program. Would I want to check on the last modified time or last accessed time? (If using the find command).

Any help or example would be greatly appreciated! Thanks!
 
Code:
#!/bin/sh

time="30 minutes ago" paths=( /path /another/path ) #findopts="-maxdepth 5"
stamp=$( date -d "$stamp" +%Y%m%d%H%M.%S)
tmp=$( mktemp ) || tmp=$( tempfile ) || tmp=/tmp/timestamp.tmp
#findopts="$findopts -cnewer $tmp -name core.txt"
clean() { rm $tmp; exit $1; }
trap 'clean 1' 2

touch -t $stamp $tmp || { echo touch failed; clean 1; }

find "${paths[@]}" $findopts -cnewer $tmp -name core.txt

clean

The alternative using bash and test -ot would contain this
Code:
shopt -s nullglob
for file in dir/{,*/{,*/{,*/{,*/}}}}core.txt; do # as many {,*/} as needed + closing brackets
    [[ $tmp -ot $file ]] && echo "-> $file"
done

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
It looks like it would work.

What does the time variable do?

Can you explain the script a little? Thanks.
 
$time is the key to the '30 minutes ago', see date(1), setting/showing time besides the current one.



paths=( /path /another/path ) #findopts="-maxdepth 5"
make an array containing valid paths, optional findopts (used in the find cmd later)

stamp=$( date -d "$stamp" +%Y%m%d%H%M.%S)
make a stamp accepted by touch(1)

tmp=$( mktemp ) || tmp=$( tempfile ) || tmp=/tmp/timestamp.tmp
mktemp and tempfile, on success, print the created temp file, or exit with non null (such as command not found), that tries mktemp, if that fails goes to tempfile, if that fails either sets the temp file to a static name

#findopts="$findopts -cnewer $tmp -name core.txt"
commented out other find options in favor of using them directly in the cmd

clean() { rm $tmp; exit $1; }
trap 'clean 1' 2

make a function to rm the file and exit, and assign signal 2 to do this (control-c)

touch -t $stamp $tmp || { echo touch failed; clean 1; }
touch it..

find "${paths[@]}" $findopts -cnewer $tmp -name core.txt
find..

clean
and clean too on script end

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Oops! I forgot that I wrote that post a long time ago. Thanks for reminding me PHV.

Thanks also to xmb for the help. I think I can try writing this script now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top