Find top N oldest files on AIX system not supporting printf in find command

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: IBM Documentation

Hi @pchegoor,

maybe xargs is worth a try:

find /home/sk/ostechnix -type f | xargs -I {} ls -ltr | head -n 5

-I {} ensures that results with spaces in their names are processed correctly.

Update: the disadvantage is that if find's result is too large, an error message could appear because the input is too long for xargs.

With perl:

find /home/sk/ostechnix/ -type f -exec perl -le '
  use POSIX; foreach (@ARGV) { $d=(stat)[9]; print POSIX::strftime("%Y-%m-%d %H:%M:%S ", localtime $d), $_ }
' {} + | sort | head

1 Like

Thank You @MadeInGermany .This is a BRILLIANT Solution using the power of Perl. I am not familiar with the language but this One-liner works like magic.

Thanks for your inputs @bendingrodriguez .I decided to go with the solution by MadeInGermany below.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.