The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
c shell script help with find bigboizvince Shell Programming and Scripting 1 03-25-2008 11:49 AM
Getting a script to find another script via lookup service zeppelin147 Shell Programming and Scripting 2 12-21-2006 04:44 PM
How to find the time taken for a script to run? Raynon Shell Programming and Scripting 8 09-18-2006 06:44 PM
how to find Script file location inside script asami Shell Programming and Scripting 10 03-14-2006 08:57 PM
Find Command In Script kdreaves SUN Solaris 3 01-21-2004 07:28 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-18-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,322
a find script

I wrote this find script for all those questions about 'files created in the last three hours' and 'files created more than 2 minutes ago'. This script *will* recurse into subdirectories.
As always, please suggest/make changes as you see fit.

Code:
#!/usr/bin/ksh
#set -x                 # unhash for debugging

print_help(){
        echo "Usage: find.sh <num><h|m> dir_to_search_in <older|newer>"
        echo "example:
        find.sh 3m /tmp newer
this will search in /tmp for files created in the last 3 minutes.
        find.sh 3h /tmp older
this will search in /tmp for files older than 3 hours."
}

PERLDIR=/opt/perl/bin # you can specify the path of your perl binary here
if [ $# -ne 3 ]; then
        print_help
        exit -2
fi
ARG1=$1
ARG2=$2
ARG3=$3

case "$ARG3" in
        "newer") new_val="-newer";;
        "older") new_val="! -newer";;
        *) print_help; exit -2;;
esac
unit=`$PERLDIR/perl -w -e 'print chop $ARGV[0]' $ARG1`
diff=`$PERLDIR/perl -w -e 'print substr $ARGV[0],0,-1' $ARG1`
case $unit in
        h) mult=3600;;
        m) mult=60;;
        *) print_help; echo "PLEASE NOTE: Only hour or minute granularity supported. For coarser stuff, use regular find. Finer granularity not yet supported."; exit -1;;
esac
diff_sec=$(($diff*$mult))
then_time=`$PERLDIR/perl -w -e '@mytime=localtime (time - $ARGV[0]); printf "%d%.2d%.2d%.2d%.2d", $mytime[5]+1900,$mytime[4]+1,$mytime[3],$mytime[2],$mytime[1];
' $diff_sec`

touch -m ${then_time} $ARG2/file_for_find
find $ARG2 -type f $new_val $ARG2/file_for_find
rm $ARG2/file_for_find
exit
--EDIT--
This has been tested on a HPUX 11.11 system. All utilities are HPUX standard.
--EDIT--

Last edited by blowtorch; 08-18-2005 at 11:46 PM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-19-2005
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,465
I've posted a perl snippet about 5/6 times as well. It finds file times down to the second.

In production we use a little ditty in C that does math arithmetic based on seconds, then stats thru files looking for older/younger files. I posted part of that thing as well.
The problem is not that we haven't posted useful code, like yours, it's that the posters almost never do a search, or if they do a search they use 'file creation date' or some other non-sequitur term that probably won't find anything useful.
Reply With Quote
  #3 (permalink)  
Old 08-19-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,334
The find article in our FAQ section mentions the touch/find trick but many users can't even be bothered to scan the FAQ section prior to asking a question. I will link this thread in the FAQ article as well. It will help those users who do read the FAQ's.
Reply With Quote
  #4 (permalink)  
Old 11-14-2006
Registered User
 

Join Date: Nov 2006
Posts: 1
a small error?

I found that it didn't work.

I had to make a small change to the touch line to add "-t" before the time variable.

i.e.

touch -m -t ${then_time} $ARG2/file_for_find

then it worked.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0