Find files modified in last hour sunOS 5.10

trying to find a way to locate files modified in the last hour in a shell script, unfortunately the command 'find . -mmin -60' is not supported on SunOS 5.10 (works on OpenSolaris 5.11 :mad:)

Does anyone know a method of doing this in shell script on 5.10?

cheers

filetime()  # create a filetime x hours ago
{
perl -e '      
      $mtime = time - (3600 * $ARGV[0]);

      # time structure into variables

      ($sec,$min,$hr,$day,$mon,$yr,$wday,@dntcare) = localtime($mtime);
      $yr = ($yr>=70) ? $yr+1900 : $yr+2000;
      $mon += 1;
      printf ("%d%02d%02d%02d%02d", $yr,$mon,$day,$hr,$min); ' "$1"
}
# get file time in YYYYMMddhhmm format
oldtime=$(filetime 1)

touch -t $oldtime dummy
find /path/to/files -newer dummy -print

Another one:

perl -MFile::Find -le'
  find { 
    wanted => sub {
      -f and 1 / 24 >= -M and print $File::Find::name;
      }
    }, shift    
  ' <dirname>  

-f is for regular files, you can modify it, if you need something different.

many thanks for these replies guys, is there a way to do this via shell and not perl?

Why? What's the problem with Perl?

1 Like

non whatsoever, just wondering if it can be accomplished in shell :b:

---------- Post updated at 03:21 PM ---------- Previous update was at 03:15 PM ----------

#!/usr/bin/ksh

# perl -MFile::Find -le'
  # find { 
    # wanted => sub {
      # -f and 1 >= -M and print $File::Find::name;
      # }
    # }, shift    
  # ' <dirname>
  
LOGS=/export/home/richard/share
  
  filetime()  # create a filetime x hours ago
{
perl -e '      
      $mtime = time - (3600 * $ARGV[0]);

      # time structure into variables

      ($sec,$min,$hr,$day,$mon,$yr,$wday,@dntcare) = localtime($mtime);
      $yr = ($yr>=70) ? $yr+1900 : $yr+2000;
      $mon += 1;
      printf ("%d%02d%02d%02d%02d", $yr,$mon,$day,$hr,$min); ' "$1"
}
# get file time in YYYYMMddhhmm format
oldtime=$(filetime 1)

touch -t $oldtime dummy

find $LOGS -newer dummy -print

-bash: ./perl.ksh: /usr/bin/ksh^M: bad interpreter: No such file or directory

Yes,
with zsh:

print -l **/*(Dmh-1)

Thanks :slight_smile: I guess it's not that easy with ksh then

I suppose it is not, I would use Perl :slight_smile:

I don't understand how to integrate your perl code into a shell script rad, can you give an example? i.e. switching to the directory /richard/share then listing all files in there modified in the last hour?

If that's the only operation, you could simply execute this code on your command line:

perl -MFile::Find -le'
  find { 
    wanted => sub {
      -f and 1 / 24 >= -M and print $File::Find::name;
      }
    }, shift    
  '  /richard/share

If you need to include that code in a script, just paste it inside:

#!/bin/sh
... other code here ...
filenames="$(
  perl -MFile::Find -le'
  find { 
    wanted => sub {
      -f and 1 / 24 >= -M and print $File::Find::name;
      }
    }, shift    
  ' /richard/share  
)"
... continue here ...

is this line here:

-f and 1

which does the actual test to see if it's less than 1 hour? I take it the 1 relates to 1 hour?

when i run it against my directory it shows files which have been modified over an hour ago?

function SearchLogsLastHour
{

cd $URS_LOGS

filenames="$(
  perl -MFile::Find -le'
  find { 
    wanted => sub {
      -f and 1 >= -M and print $File::Find::name;
      }
    }, shift    
  ' /export/home/richard/share/ipcc/urslogs  
)"

echo $filenames | ls -l

}

When i run the above it just shows all the file in the directory - I opened and saved one of them, ran it again (expected it to just list the one file I saved a few seconds ago) but it still lists all the files the directory

Sorry, 1 is one day, so you need 1/24:

perl -MFile::Find -le'
  find { 
    wanted => sub {
      -f and 1 / 24 >= -M and print $File::Find::name;
      }
    }, shift    
  '  /richard/share

I corrected my posts above.

1 Like

Working :b: cheers for that rad; you the man :cool:

---------- Post updated at 08:11 PM ---------- Previous update was at 07:25 PM ----------

so say the perl program finds 3 files matching the critera - is there an easy way to modify this so that once the first file is found, we do a grep on that file then output what we need to the temp file, then proceed to the second file, do a grep on that, output to the temp file etc etc?

Of course, just describe what exactly you want to grep and put into the temp file(s).

cd $LOGS

filenames="$(
  perl -MFile::Find -le'
  find { 
    wanted => sub {
      -f and 1 / 24 >= -M and print $File::Find::name;
      }
    }, shift    
  ' /export/home/richard/logs
)"

grep "cheese" $filenames

That seems to work, but I need to introduce an IF into it so that if the perl returns any files of the extension .gz it does a gzgrep instead of a normal grep

You can do it all in Perl, of course, but if you feel more comfortable with shell scripting, you could try something like this (untested):

cd "$LOGS"

perl -MFile::Find -le'
  find { 
    wanted => sub {
      -f and 1 / 24 >= -M and print $File::Find::name;
      }
    }, shift    
  ' /export/home/richard/logs |
    while IFS= read -r; do
      case $REPLY in
        *.gz ) gzgrep cheese "$REPLY";;
           * ) grep cheese "$REPLY" ;;
      esac
    done   
filenames="$(
  perl -MFile::Find -le'
  find { 
    wanted => sub {
      -f and 1 / 24 >= -M and print $File::Find::name;
      }
    }, shift    
  ' /export/home/richard/share/logs |
      while IFS= read -r; do
      case $REPLY in
        *.gz ) gzgrep cheese "$REPLY";;
        *.log ) grep cheese "$REPLY" ;;
      esac
    done
)"

The grep only returns one line :confused: should be about a hundred from one .log file and one .gz file

What happens when you run the code directly, without assigning it to a variable?

yes you can do like
find path -name -print | grep "get the time stamp from date command for the hour only" > file.txt

put it in the cron job which will run every hour and you will get the result