I am looking to do a ls on a folder and have the output of the ls be structured so that is is modificaiton date, file name with the date in a format that is compatible with mysql. I am trying to build a table that stores the last modification date of certain files so I can display it on some web reports.
I know I can do
ls -lrt | ts -s " " | cut -d" " -f6-9
and get down to
Dec 23 07:08 filename
Dec 24 05:12 file2
but is there a way I can get the date in a format I can upload into mysql? I really want to get a file in the format of
2010-12-23 07:08:00, filename
2010-12-24 05:12:00, file2
perl -MPOSIX -le'
print strftime("%Y-%m-%d %H:%M:%S", localtime +(stat)[9]), ", ", $_
for <*>
'
man stat
You can use either "stat -c %y" for a better format (might still need some parsing, but closer to what you want), or you may even want to store the time in epoch time, using "stat -c %Y"
$ stat -c %Y filename
1293413752
$ stat -c %y filename
2010-12-26 17:35:52.000000000 -0800
I know MySQL handles epoch time just fine.
-dufftime
Yes,
on a GNU system you could use something like this which is close:
stat -c '%y, %n' *
Or:
ls --time-style='+%Y-%m-%d %H:%m:%S' -l |
sed 's/\([^ ]* *\)\{5\}//;s/ /, /2'
Thanks.. stat didnt work for me on this machine but the perl script worked like a champ..