Writing file name and date from LS command into a file to be imported into mysql


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Writing file name and date from LS command into a file to be imported into mysql
# 1  
Old 01-05-2011
Writing file name and date from LS command into a file to be imported into mysql

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
Code:
ls -lrt | ts -s " " | cut -d" " -f6-9

and get down to

Code:
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

Code:
2010-12-23 07:08:00, filename
2010-12-24 05:12:00, file2


Last edited by radoulov; 01-05-2011 at 06:50 PM.. Reason: Code tags, please!
# 2  
Old 01-05-2011
Code:
perl -MPOSIX -le'
  print strftime("%Y-%m-%d %H:%M:%S", localtime +(stat)[9]), ", ", $_ 
    for <*>
    '

# 3  
Old 01-05-2011
Use 'stat' instead

Code:
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"

Code:
$ 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
# 4  
Old 01-05-2011
Yes,
on a GNU system you could use something like this which is close:

Code:
stat -c '%y, %n' *

Or:

Code:
ls  --time-style='+%Y-%m-%d %H:%m:%S' -l | 
  sed 's/\([^ ]* *\)\{5\}//;s/ /, /2'

# 5  
Old 01-06-2011
Thanks.. stat didnt work for me on this machine but the perl script worked like a champ..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. HP-UX

HP/UX command to pull file name/date based on date

HI, Can anyone tell me how to pull the date and file name separated by a space using the find command or any other command. I want to look through several directories and based on a date timeframe (find -mtime -7), output the file name (without the path) and the date(in format mmddyyyy) to a... (2 Replies)
Discussion started by: lnemitz
2 Replies

2. Shell Programming and Scripting

awk - writing matching pattern to a new file and deleting it from the current file

Hello , I have comma delimited file with over 20 fileds that i need to do some validations on. I have to check if certain fields are null and then write the line containing the null field into a new file and then delete the line from the current file. Can someone tell me how i could go... (2 Replies)
Discussion started by: goddevil
2 Replies

3. UNIX for Dummies Questions & Answers

Writing a script that will take the first line from each file and store it in an output file

Hi, I have 1000 files names data1.txt through data1000.txt inside a folder. I want to write a script that will take each first line from the files and write them as output into a new file. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

4. UNIX and Linux Applications

MySQL Daemon failed to start - no mysql.sock file

After doing a yum install mysql mysql-server on Fedora 14 I wasn't able to fully install the packages correctly. It installed MySQL 5.1. I was getting the following error when running the: mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)... (3 Replies)
Discussion started by: jastanle84
3 Replies

5. Shell Programming and Scripting

echoing date command into file

I want to echo into file1 echo & date commands, which in turn will be echoed into file2 string and the current date. So when I'll run file1 it will echo into file2 the commands 'echo' & 'date' My problem is that the date command turns into the actual date value. Example:... (2 Replies)
Discussion started by: liav
2 Replies

6. Shell Programming and Scripting

Searching for Log / Bad file and Reading and writing to a flat file

Need to develop a unix shell script for the below requirement and I need your assistance: 1) search for file.log and file.bad file in a directory and read them 2) pull out "Load_Start_Time", "Data_File_Name", "Error_Type" from log file 4) concatinate each row from bad file as... (3 Replies)
Discussion started by: mlpathir
3 Replies

7. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

8. UNIX for Advanced & Expert Users

Reading a file and writing the file name to a param file.

Hi All, Not sure if this would be in a dummies sectiin or advanced. I'm looking for a script if someone has doen something like this. I have list of files - adc_earnedpoints.20070630.txt adc_earnedpoints.20070707.txt adc_earnedpoints.20070714.txt adc_earnedpoints.20070721.txt... (1 Reply)
Discussion started by: thebeginer
1 Replies

9. UNIX for Dummies Questions & Answers

command for modification date of a file

Good morning, I would like to find all files of a certain type and display their name as well as their modification date. In order to do this, I would do the following: find ./ -name *.csv | ???????? My question: what to put after the pipe instead of the question marks? Is there a basic... (5 Replies)
Discussion started by: scampsd
5 Replies
Login or Register to Ask a Question