I want to build an mysql insert script from the files in a directory
I have alias v ='ls -l --time-style=+"%Y-%m-%d %H:%M:%S"'
The directory has about 1000 files and I want to extract the files *.afl
and store them into a table with a script constructed from an awk command:
ls -1 *.afl| here="$(cygpath -w $PWD)" awk -v source="$source" '{print "INSERT INTO action_diary (entry_date, entry_description,entry_details) VALUES (STR_TO_DATE(<FILE CREATION DATE>,\x27%Y-%m-%d\x27),,\x27\x27,\x27)" "File Name: "$0"\n"ENVIRON["here"]"\n"source"\x27"}'
Creates this line:
INSERT INTO action_diary (entry_date, entry_description,entry_details) VALUES (STR_TO_DATE(<FILE CREATION DATE>,'%Y-%m-%d'),'','File Name: 2011 02 21 drdttl.afl
C:\Users\athena\Downloads\Project_1\00.MBT
Source: Parallel action
I'm stuck here because I want the file timestamp to be included, so that means I can't use ls -1.
My depth of knowledge here has it's bottom.
Any savvy users have some tricks up their sleeves?
ANSWER I USED
stat -c '%y %n' *.afl| here="$(cygpath -w $PWD)" awk -v source="$source" '{print "INSERT INTO action_diary (entry_date, entry_description,entry_details) VALUES (STR_TO_DATE(\x27"substr($0,1,18)"\x27,\x27%Y-%m-%d\x27),\x27\x27,\x27File Name: "substr($0,37)"\n"ENVIRON["here"]"\n"source"\x27"}'
stat
instead. The-1
is useless anyway,ls
applies that option by default when the output is not to a terminalstat -c '%y %n' *.afl
no need for a loop or ls at allls
without-1
in the pipe and you'll see it behaves the same. That's what I meant by-1
is useless.ls
behaves differently in the pipe.