{"id":593,"date":"2013-07-04T15:22:32","date_gmt":"2013-07-04T14:22:32","guid":{"rendered":"http:\/\/blog.stratus.org.uk\/?p=593"},"modified":"2013-07-04T15:33:11","modified_gmt":"2013-07-04T14:33:11","slug":"unix-shell-cgi-script-using-stat-and-xarg-for-file-listing","status":"publish","type":"post","link":"https:\/\/blog.stratus.org.uk\/?p=593","title":{"rendered":"Unix Shell CGI Script using find, stat or xarg for File Listing"},"content":{"rendered":"<p>This little code snippet was required to find out the date and time for files for use in a CGI script.<\/p>\n<p>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.<\/p>\n<p>After that it it uses <code>find<\/code> with the output as a <code>printf<\/code> statement. The nice thing about using printf is that we can just select the name of the file without the full path (you don\u2019t want to tell the world exactly where it lives!).<\/p>\n<pre> #!\/bin\/sh\r\n RUNPATH=\/home\/<your full path here>\r\n PATTERN=<the files you want, e.g. *.txt>\r\n NOW=$(date +\"%H:%M:%S %d-%h-%Y\")\r\n echo \"Content-type: text\/html\"\r\n echo \"\"\r\n echo \"\"\r\n echo \"<pre>\"\r\n echo \"<h3>Production Times and Sizes at $NOW<\/h3>\"\r\n echo \"<pre>\"\r\n find $RUNPATH -name \"$PATTERN*\" -printf \"%AD %Ar %10s %f\\n\"\r\n echo \"<\/pre>\"<\/pre>\n<p>And that\u2019s it!<\/p>\n<p>You can change the format specifiers in the printf to suit what you want.<\/p>\n<p>A sample output looks like this:<\/p>\n<pre>Production Times and Sizes at 13:31:07 04-Jul-2013\r\n\r\n07\/04\/13 08:04:31 AM      77956 00fcst-movie-FULL-UK2.flv\r\n07\/04\/13 06:20:23 AM      61540 00fcst-movie-FULL-UK2.wmv\r\n07\/04\/13 06:20:22 AM     124996 00fcst-movie-FULL-UK2.avi\r\n07\/04\/13 11:30:06 AM    8061942 00fcst-movie-FULL-UKEA62-WINDOW.wmv\r\n07\/04\/13 11:30:05 AM    6447314 00fcst-movie-FULL-UKEA62-WINDOW.avi\r\n07\/04\/13 11:30:06 AM    4371525 00fcst-movie-FULL-UKEA62-WINDOW.flv\r\n07\/04\/13 11:41:05 AM    4409202 00fcst-movie-FULL-UK4.flv\r\n07\/04\/13 07:05:02 AM    8527096 00fcst-movie-FULL-UK4.wmv\r\n07\/04\/13 07:05:02 AM   11068020 00fcst-movie-FULL-UK4.avi<\/pre>\n<p>You can of course consider using <code>stat<\/code> and <code>xarg<\/code>, by replacing the tail of the <code>find<\/code> like this if you do want the full path:<\/p>\n<pre>\r\nfind $RUNPATH | grep $PATTERN | xargs stat -c \"%y %10s %n\"\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/blog.stratus.org.uk\/?p=593\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-593","post","type-post","status-publish","format-standard","hentry","category-misc"],"_links":{"self":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/posts\/593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=593"}],"version-history":[{"count":15,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/posts\/593\/revisions"}],"predecessor-version":[{"id":600,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/posts\/593\/revisions\/600"}],"wp:attachment":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}