64BIT - The Collection

Bash - Extracting Timestamps From Files Recursively

I needed to quickly extract the filesize and timestamps of a large number of files within a directory, and store that information in a CSV format. This script is not pretty, but it gets the job done fairly quickly.  This was designed on Mac OS X 10.11.6 so it uses some kind of nerfed stat command, and requires ggrep to be installed to use the -P flag (GNU grep).

#!/bin/bash

dir='/home/user/Downloads/directoryoffiles'
output='/home/user/Downloads/file_dates.csv'

find $dir -type f > /tmp/templist.txt

echo \"filename\",\"filesize_bytes\",\"access_time\",\"modified_time\",\"change_time\",\"birth_time\" > $output

while read line; do

filename=$line

timestamps=`stat -L -t %Y-%m-%dT%H:%M:%S%z "$filename" | ggrep -oP '\".*\"' | sed 's/ /,/g'`
bytesize=`stat -L -t %Y-%m-%dT%H:%M:%S%z "$filename" | ggrep -oP ' ([0-9]{1,100}) \"' | sed 's/..$//' | sed 's/^.//'`


echo \"$filename\",\"$bytesize\",$timestamps >> $output
done < /tmp/templist.txt

rm /tmp/templist.txt