(pdksh) Looping through n files and store their dates in array

272 Views Asked by At

Using pdksh

stat() command is unavailable on system.

I need to loop through the amount of files found and store their dates in an array. $COMMAND stores the number of files found in $location as can be seen below.

Can someone help me please?

COMMAND=`find $location -type f | wc -l`
CMD_getDate=$(find $location -type f | xargs ls -lrt | awk '{print $6} {print $7}')
1

There are 1 best solutions below

4
Mark Reed On

Well, first, you don't need to do the wc. The size of the array will tell you how many there are. Here's a simple way to build an array of dates and names (designed for pdksh; there are better ways to do it in AT&T ksh or bash):

set -A files 
set -A dates
find "$location" -type f -ls |&
while read -p inum blocks symode links owner group size rest; do 
  set -A files "${files[@]}" "${rest##* }"
  set -A dates "${dates[@]}" "${rest% *}"
done

Here's one way to examine the results:

print "Found ${#files[@]} files:"
let i=0
while (( i < ${#files[@]} )); do
  print "The file '${files[i]}' was modified on ${dates[i]}."
  let i+=1
done

This gives you the full date string, not just the month and day. That might be what you want - the date output of ls -l (or find -ls) is variable depending on how long ago the file was modified. Given these files, how does your original format distinguish between the modification times of a and b?

$ ls -l
total 0
-rw-rw-r--+ 1 mjreed  staff  0 Feb  3  2014 a
-rw-rw-r--+ 1 mjreed  staff  0 Feb  3 04:05 b

As written, the code above would yields this for the above directory with location=.:

Found 2 files:
The file './a' was modified on Feb  3  2014.
The file './b' was modified on Feb  3 00:00.

It would help if you indicated what the actual end goal is..