Unix Shell CGI Script using find, stat or xarg for File Listing

This little code snippet was required to find out the date and time for files for use in a CGI script.

It uses a conventional shell script to start with, setting some local variables to use in the script. It then outputs the HTML content type to enable CGI working.

After that it it uses find with the output as a printf statement. The nice thing about using printf is that we can just select the name of the file without the full path (you don’t want to tell the world exactly where it lives!).

 #!/bin/sh
 RUNPATH=/home/
 PATTERN=
 NOW=$(date +"%H:%M:%S %d-%h-%Y")
 echo "Content-type: text/html"
 echo ""
 echo ""
 echo "
"
 echo "

Production Times and Sizes at $NOW

" echo "
"
 find $RUNPATH -name "$PATTERN*" -printf "%AD %Ar %10s %f\n"
 echo "
"

And that’s it!

You can change the format specifiers in the printf to suit what you want.

A sample output looks like this:

Production Times and Sizes at 13:31:07 04-Jul-2013

07/04/13 08:04:31 AM      77956 00fcst-movie-FULL-UK2.flv
07/04/13 06:20:23 AM      61540 00fcst-movie-FULL-UK2.wmv
07/04/13 06:20:22 AM     124996 00fcst-movie-FULL-UK2.avi
07/04/13 11:30:06 AM    8061942 00fcst-movie-FULL-UKEA62-WINDOW.wmv
07/04/13 11:30:05 AM    6447314 00fcst-movie-FULL-UKEA62-WINDOW.avi
07/04/13 11:30:06 AM    4371525 00fcst-movie-FULL-UKEA62-WINDOW.flv
07/04/13 11:41:05 AM    4409202 00fcst-movie-FULL-UK4.flv
07/04/13 07:05:02 AM    8527096 00fcst-movie-FULL-UK4.wmv
07/04/13 07:05:02 AM   11068020 00fcst-movie-FULL-UK4.avi

You can of course consider using stat and xarg, by replacing the tail of the find like this if you do want the full path:

find $RUNPATH | grep $PATTERN | xargs stat -c "%y %10s %n"