Getting file timestamp in certain format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting file timestamp in certain format
# 1  
Old 07-13-2012
Getting file timestamp in certain format

Hi all,

I'm a Unix newbie and I need to get file timestamp in the following format:
Code:
YYYYMMDD HH24:MM:SS

example:
Code:
20120713 18:49:30

For start I've tried the following code, but I don't know how to display the year and even format the date:
Code:
ls -l $filename | awk '{print $7 $6 $8}'

My environment is HP-UX so I need to get it using Korn shell.

Any help would be greatly appreciated.

Thanks & Regards,

Setya
# 2  
Old 07-13-2012
How is your input date looking? I don't have your environment to test...
# 3  
Old 07-13-2012
I'd actually just started up a wiki to try to add things that would help when I'm teaching that might be helpful to you.

Code:
date +%Ymd %H%M%S

Give 'man date' a look, will have some HP-UX specific options in it and might have a prefab close to what you're looking for.
# 4  
Old 07-13-2012
Hi,

Thank you for your response.
As I understand 'date' is used to display current timestamp.
What I need is to display file timestamp with certain format.

Regards,

Setya
# 5  
Old 07-13-2012
Code:
filemtime()
{
perl -e '      
      # @arr = stat("$ARGV[0]");
      ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = 
            localtime( (stat("$ARGV[0]") )[9]);
      printf("%4d %02d %02d %02d:%02d:%02d",
            $year+1900, $mon+1, $mday, $hour, $min, $sec);     
      '  $1
}

echo $(filemtime t.lis)

ls -l will not work because the date format changes for files older than 6 months.
perl or some other language like C is the only generally available choice.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Timestamp format in HP-UX

I am on HP-UX B.11.11 OS. My requirement is to display a list of directories (not files) and the last modified time format should be DD-MON-YYYY HH:MI:SS AM/PM. I am able to get the list using either ls -lF | grep / OR ls -ld -- */ but I am unable to set the time format as I want. The... (5 Replies)
Discussion started by: nickz2017
5 Replies

2. Shell Programming and Scripting

Convert date in dd mm yyyy format to UNIX timestamp

Hello All, I have a date in DD/MM/YYYY format. I am trying to convert this into unix timestamp. I have tried following: date -d $mydate +%s where mydate = 23/12/2016 00:00:00 I am getting following error: date: extra operand `+%s' Try `date --help' for more information. ... (1 Reply)
Discussion started by: angshuman
1 Replies

3. Shell Programming and Scripting

Convert UNIX timestamp to readable format in the file

Hello I have a file : file1.txt with the below contents : 237176 test1 test2 1442149024 237138 test3 test4 1442121300 237171 test5 test7 1442112823 237145 test9 test10 1442109600 In the above file fourth field represents the timestamp in Unix format. I found a command which converts... (6 Replies)
Discussion started by: rahul2662
6 Replies

4. UNIX for Dummies Questions & Answers

How to compare a file by its timestamp and store in a different location whenever timestamp changes?

Hi All, I am new to unix programming. I am trying for a requirement and the requirement goes like this..... I have a test folder. Which tracks log files. After certain time, the log file is getting overwritten by another file (randomly as the time interval is not periodic). I need to preserve... (2 Replies)
Discussion started by: mailsara
2 Replies

5. UNIX for Dummies Questions & Answers

Format/Fix Timestamp Data in a File.

Hello Experts, I have a timestamp(6) column in a .csv data file , format of the data is as below:- ETCT,P,Elec, Inc.,abc,11/5/2010 4:16:09.000000 PM,Y,Y,Y I want the timestamp column to be properly formatted like 11/05/2010 04:16:09.000000 PM Currently the "0" is missing with... (3 Replies)
Discussion started by: mtlrsk
3 Replies

6. Shell Programming and Scripting

To convert a date(in string format) to unix timestamp

Hi All, I have a string like below. "Mar 31 2009" . I want to convert this to unix time . Also please let me know how to find the unix time for the above string minus one day. For Eg. if i have string "Mar 31 2009" i want to find the unix time stamp of "Mar 30 2009". Thanks in advance,... (11 Replies)
Discussion started by: girish.raos
11 Replies

7. UNIX for Advanced & Expert Users

ls -l timestamp display format

The time stamp format using "ls -l" is either mmm dd hh:mm or mmm dd yyyy. For later case, how can I know the hh:mm as well. Thanks. (3 Replies)
Discussion started by: pok.fung
3 Replies

8. Programming

converting unix timestamp into readable format using c++

hi everyone, im new here and am in desperate need of help. I want to convert my 32 bit unix time stamp ' 45d732f6' into a readable format (Sat, 17 February 2007 16:53:10 UTC) using c++. I have looked around the interent but i just cant make sense of anything. All examples i can find just... (3 Replies)
Discussion started by: uselessprog
3 Replies

9. UNIX for Advanced & Expert Users

get the timestamp of a file in desired format

Hi, I have a file say abc. I get the timestamp in following way: ls -ltr abc | awk -F" " '{print $6,$7,$8}' Mar 8 10:23 I need to get the timestamp as : 03-08-2007 10:23:00 Thanks Sumeet (1 Reply)
Discussion started by: sumeet
1 Replies

10. Shell Programming and Scripting

how to convert from timestamp to date format in tcsh

hello all im looking for fast way to convert timestamp format to date format and vaiseversa in tcsh , can it be done? thanks allot (2 Replies)
Discussion started by: umen
2 Replies
Login or Register to Ask a Question