1

I am trying to find a solution similar to the one used below to find the Top N oldest files (modification time) on my AIX system starting from a given directory and digging though all sub-directories as well under it . Unfortunately printf is not supported on AIX ( my version being 7.1) find command. Is there an alternative way to accomplish the same task on AIX?

$ find /home/sk/ostechnix/ -type f -printf '%T+ %p\n' | sort | head -n 5

Source: https://ostechnix.com/find-oldest-file-directory-tree-linux/

AIX man for find command: https://www.ibm.com/support/knowledgecenter/ssw_aix_71/f_commands/find.html

pchegoor
  • 386
  • 1
  • 5
  • 14
  • Would [this thread](https://stackoverflow.com/questions/4561895/how-to-recursively-find-the-latest-modified-file-in-a-directory) be of any help? – vgersh99 Dec 13 '20 at 23:02
  • AIX, the bane of *nix (-; . Does it support `stat` utility and does that utility include `printf` options? Then you can `find /path -print | xargs stat printf"%...." | sort ...` Otherwise, your left to use the `-ls` options and parse/reformat that data (possibly in a separate script), that you also call with `xargs`. Good luck. – shellter Dec 14 '20 at 06:35

1 Answers1

1

After searching the web , I came up with the below one line solution based mostly on the answer provided here : https://superuser.com/a/294164 and https://stackoverflow.com/a/23478116

The below solution first uses find command to find files (starting from current directory) and then pipes the output to perl command to do the sort and then pipes the resultant list of files into another perl command to get the timestamp of each of the file in a desired format.The result will show Top 5 oldest files.

I am not a perl expert but i am guessing the below can further be simplifed. Please do let me know if this is the case. The below solution seems to be working fine as of now.It works well on my AIX system.

find . -type f -print  2>/dev/null |
perl -l -ne '
$_{$_} = -M;  
END {
    $,="\n";
    @sorted = sort {$_{$b} <=> $_{$a}} keys %_;  
    print @sorted[0..5];
}'  | xargs -I {} perl -MPOSIX   -e 'print "\n $ARGV[0] -------> $1 " . strftime("%A %Y-%m-%d  %H:%M:%S", localtime((stat "$ARGV[0]")[9]))  '  {}

This gives an output as follows:

./file1.txt ---> Sunday 2018-03-04 15:20:32
./sample/file2.sh ---> Sunday 2019-01-27 08:30:45
./test/file3.txt ---> Tuesday 2019-05-21 18:45:32
./sample/temp/file4.sh ---> Friday 2019-12-27 12:30:45
./file5.txt ---> Tuesday 2020-06-13 15:20:32
pchegoor
  • 386
  • 1
  • 5
  • 14