1

AIX 7.2 Looking to recreate the below on AIX.

 find /etc -maxdepth 1 -iname "*conf" -type f -mmin +5  -printf '%p;%T@\n' | awk -F ';' -v time="$( date +%s )"  '{ print $1";"$2";" (time-$2 ) }'

/etc/rsyslog.conf;1640302499.0000000000;46381761

conf files are just and example of finding a list of specific files that may be older than a set number of seconds. Could be as low as 300 seconds or 43200 seconds or more.

5

1 Answer 1

0

If I had to solve this on an AIX system, I'd again lean on perl. Since you're using -maxdepth 1, there's no real need to get into the File::Find module here. I came up with a perl script that uses two essential functions:

  • glob to match the expected filename pattern
  • stat to extract the mtime of the files

If the script finds files that match the pattern that have a last-modified time older than the expected age, it prints them in the semicolon-delimited format you gave. Beware that semicolons (and newlines and other whitespace) are allowable characters in filenames, so be careful when dealing with the output.

#!/usr/bin/perl -w
# prints matching file mtime and age in seconds

use strict;

# expect 3 arguments:
# * a starting directory
# * a (quoted from the shell) wildcard for -iname
# * a cutoff age in seconds

if ($#ARGV != 2) {
  die "Usage: $0 [ dir ] [ pattern ] [ age ]"
}

my ($start_dir, $pattern, $age) = @ARGV;

unless ($age =~ /^\d+$/ && $age > 0) {
  die "$0: age must be a positive integer"
}

my $now = time();
my $cutoff = $now - $age;

foreach my $file (glob "${start_dir}/${pattern}") {
  next unless -f $file;
  my $mtime = (stat($file))[9];
  if ($mtime < $cutoff) {
    print "${file};${mtime};" . ($now - $mtime) . "\n";
  }
}
4
  • Very useful as it shows the concept is possible. Might need some tweaks Wildcard wouldn't work but partially specifying the file name with a wild card did work. ./getfiletime.pl /opt * 1 no output ./getfiletime.pl /opt fi* 1 /opt/file;1686773770;253
    – mdtoro
    Commented Jun 14, 2023 at 20:21
  • You would want to quote any wildcard you pass to this script, to protect it from being expanded by your shell. Commented Jun 14, 2023 at 20:58
  • Need to review this again. Trying to pass the script an input file instead of passing parameters on the command line. File would contain multiple lines such as: /etc *.conf 300 /tmp/app1 *.ext 300 /tmp/app2 *.gf 10000
    – mdtoro
    Commented Nov 30, 2023 at 20:09
  • Hi @mdtoro! It sounds like that's a bit different from your original question, so I'd suggest asking a new question, linking to this one, and spelling out your new requirements. Commented Nov 30, 2023 at 20:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .