LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-26-2014, 06:25 AM   #1
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Rep: Reputation: Disabled
How to convert file arrival time to specified date format?


Hi,

i'll be receiving a file every half an hour, i want to read the file arrival time and convert it to ddmmyyyy hh:mm, how could we do that?

e.g
$ ls -lrt falcon_input_data_v2.txt
-rwxrwxrwx 1 111tcsi 111tcsi 16 Jun 26 10:30 falcon_input_data_v2.txt


every half an hour, i want to read this file arrival time and convert it to the format 26062014 10:30 , how could we do that?

could someone please help?
 
Old 06-26-2014, 07:23 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,548

Rep: Reputation: 922Reputation: 922Reputation: 922Reputation: 922Reputation: 922Reputation: 922Reputation: 922Reputation: 922
What have you tried so far?

Hint:
Code:
man ls
man cut

Last edited by TenTenths; 06-26-2014 at 07:29 AM.
 
Old 06-26-2014, 07:33 AM   #3
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TenTenths View Post
What have you tried so far?

Hint:
Code:
man ls
man cut
i have prepared small script to convert date and stuck, here is the code:
Code:
#!/bin/ksh

disp_dt=$(ls -lrt /home/www/BCARD/data/falcon/falcon_input_data_v2.txt)
chk_pos=$(echo $disp_dt|awk -F ' ' '{print $6,$7,$8}')
sys_yr=$(date +'%Y')
echo "chk_pos:$chk_pos $sys_yr"
output:

./test.sh
chk_pos:Jun 26 10:30 2014

now i want to convert it to the format as 26062014 10:30
 
Old 06-26-2014, 07:44 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,548

Rep: Reputation: 922Reputation: 922Reputation: 922Reputation: 922Reputation: 922Reputation: 922Reputation: 922Reputation: 922
Your script will give the wrong answers around midnight on 31st December as you are calculating the year from the system date and not the file date.

So if your file is received at 23:58 on 31st December and your script runs at 00:05 on 1st January you'll get "Dec 31 23:58 2015" as the result, so your file will be almost a year in the future!

Depending on your distribution and version of ls you may find it supports the --time-style option. If it does then:

lsdate.sh
Code:
#!/bin/bash
FILE=$1
DATE=$(ls -l --time-style=+%d%m%Y" "%H:%M ${FILE} | cut -d " " -f 6,7)
echo ${FILE} ${DATE}
Gives:
Code:
[tt@audi ~]# ls -l phpinst.sh
-rwxr-xr-x 1 tt tt 192 Nov  9  2013 phpinst.sh

[tt@audi ~]# ./lsdate.sh phpinst.sh
phpinst.sh 09112013 14:23

Last edited by TenTenths; 06-26-2014 at 07:46 AM.
 
1 members found this post helpful.
Old 06-26-2014, 08:08 AM   #5
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
Thanks a ton its working fine. appreciated your help
 
Old 06-26-2014, 08:38 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,713

Rep: Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279
Shouldn't be using ls for that at all. The date provided is not accurate.

Try the stat command. It provides the date in full (the -t provides the number) and you can use the shell printf to format it however you want.

And forgot to mention that stat also allows you to print whichever date you want.

Last edited by jpollard; 06-26-2014 at 08:42 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help on converting date time format in text file and triggering action depam Linux - General 3 11-28-2012 10:46 AM
how to convert windows date and time to unix date and time jitupatil_2007 General 8 03-31-2008 06:58 AM
convert date time format given in string suchi_s Programming 6 01-10-2005 05:06 AM
loading data from a flat file with a date and time having a different format spyghost Programming 2 11-02-2003 05:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration