Timestamp & date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Timestamp & date
# 1  
Old 08-25-2008
Timestamp & date

Hi,

I have list of files as below, with prefix named as date & time. Anyone how to transform each file as below to yyyy-mm-dd hh:mm:ss



Quote:
-rw-r----- 1 ccbop ssd 132283 Aug 22 03:00 080822_030001_BATCH.inp
-rw-r----- 1 ccbop ssd 9182 Aug 22 03:30 080822_033002_BATCH.inp
-rw-r----- 1 ccbop ssd 7016 Aug 22 04:00 080822_040001_BATCH.inp
-rw-r----- 1 ccbop ssd 4489 Aug 22 04:30 080822_043002_BATCH.inp
-rw-r----- 1 ccbop ssd 1962 Aug 22 05:00 080822_050001_BATCH.inp
-rw-r----- 1 ccbop ssd 4128 Aug 22 05:30 080822_053001_BATCH.inp
-rw-r----- 1 ccbop ssd 9182 Aug 22 06:00 080822_060001_BATCH.inp
-rw-r----- 1 ccbop ssd 377326 Aug 22 06:30 080822_063001_BATCH.inp
-rw-r----- 1 ccbop ssd 450977 Aug 22 07:00 080822_070001_BATCH.inp

Regards,
# 2  
Old 08-25-2008
chop out the date string from the file name and use
date +%F -d "<string>"
this will convert the string date to the yyyy-mm-dd format
# 3  
Old 08-25-2008
It didn't work as expected

date +%F-d"`ll -lrt | awk '{print $9}' | cut -c1-6`"
August-d
080822
080822
080822
080822
080822
080822
# 4  
Old 08-25-2008
Nothing wrong with the date solution -- you just need to run date one file at a time. (Use a for/while loop around ls -l). Meanwhile, perl's very handy to get the job done:

Code:
ls -1 | perl -n -e 'chop; print localtime( (stat($_))[9] ) . "\n";

The perl command processes each line of input (with -n option) and for each line, $_ is the input line. "chop" removes the newline character at the end of the line. "stat" retrieves the inode data from the filename (in $_). "localtime" will spit out the date/time of the file using the local timezone. (Use "gmtime" if you want it to ignore timezone settings.)
# 5  
Old 08-25-2008
ls -E *.inp | awk '{print $6,$7}' | cut -d"." -f1

above command will give u the date and time as you expected..
# 6  
Old 08-25-2008
U can try:
Code:
ls -l|while read file
do
   file2="$(echo ${file}|sed -e "s/\(..\)\(..\)\(..\)_\(..\)\(..\)\(..\)/20\1-\2-\3 \4:\5\:\6/")"
   mv "${file}" "${file2}"
done

# 7  
Old 08-25-2008
Klashxx,

can u explain the above sed logic.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing Date in the file with Create date and timestamp

Hello, I have files that with a naming convention as shown below. Some of the files have dates in the file name and some of them don't have dates in the file name. imap-hp-import-20150917.txt imap-dell-gec-import-20150901.txt imap-cvs-import-20150915.txt imap-gec-import.txt... (8 Replies)
Discussion started by: Saanvi1
8 Replies

2. UNIX for Dummies Questions & Answers

How to get a timestamp when running nohup & from a shell?

Hi, I am running nohup cp & in a shell script. How do I get a timestamp so I can get a timing of how long the copy took? Roughly, the script does something like below: date nohup cp -rp /files/source /files/target & date I am mainly wanting to know how long it took for the... (9 Replies)
Discussion started by: newbie_01
9 Replies

3. Shell Programming and Scripting

Prepend TimeStamp to STDERR & STDOUT to a file

Currently I am redirecting STDERR and STDOUT to a log file by doing the following { My KSH script contents } 2>&1 | $DEBUGLOG Problem is the STDERR & STDOUT do not have any date/time associated. I want this to be something that i can embed into a script opposed to an argument I use... (4 Replies)
Discussion started by: nitrobass24
4 Replies

4. Shell Programming and Scripting

Epoch & Unix Timestamp Conversion Tools

Hi All, Please read the below data carefully. I need an unix command for converting unix timestamp to Epoch timestamp. I need to daily convert this today's unix(UTC) time to epoch time, so i am thinking to make a shellscript for this. Please help me for this by providing... (3 Replies)
Discussion started by: aish11
3 Replies

5. Shell Programming and Scripting

Check if a date field has date or timestamp or date&timestamp

Hi, In a field, I should receive the date with time stamp in a particular field. But sometimes the vendor sends just the date or the timestamp or correctl the date&timestamp. I have to figure out the the data is a date or time stamp or date&timestamp. If it is date then append "<space>00:00:00"... (1 Reply)
Discussion started by: machomaddy
1 Replies

6. Shell Programming and Scripting

How to convert date and timestamp?

Hi, I have a file file1 having data as below 20110501,070742, ,012345678909,09999999999,68.5, 20110501,070236, ,089375855455,09376383333,374.3, 20110501,070525, ,090345895555,08444233444,206.2, 20110501,230051, ,000934744433,07624262223,480.1, First field is date(YYYYMMDD) and second... (5 Replies)
Discussion started by: vsachan
5 Replies

7. Shell Programming and Scripting

Just listing size, timestamp & name of files in a directory

How can I list the files in a directory and just show the file size, date stamp, timestamp and file name.. I've been trying to ls -lrt the directory to a file and then use the cut command but I'm not having any luck with getting the proper results.. I thought i could use the -f switch and count... (4 Replies)
Discussion started by: Jazmania
4 Replies

8. UNIX for Dummies Questions & Answers

vmstat output with date & timestamp

Hello all This is a sample vmstat output ... $ vmstat 2 2 kthr memory page disk faults cpu r b w swap free re mf pi po fr de sr hx hx hx hx in sy cs us sy id 1 0 0 23105784 7810488 323 767 1742 5 5 0 0 0 0 0 0 683 780 457 43 ... (9 Replies)
Discussion started by: luft
9 Replies

9. UNIX for Dummies Questions & Answers

getting date from timestamp

Hi All, i have a time stamp. from that i am trying to awk to get the year, month and date. TIME=20060614092446 DESIRED OUTPUT: 20060614 i am doing the following; TIME=20060614092446 $ TimeStarted=`expr match '$TIME' '.*\(......\)'` echo $TimeStarted i am not getting... (2 Replies)
Discussion started by: pavan_test
2 Replies

10. UNIX for Dummies Questions & Answers

get a file date/timestamp

Could someone tell me how to get the date/time (to the second) a file was last modified? I need to know if a file was modified in the last 30 seconds from the system date. I'm on AIX/unix 4.3 (3 Replies)
Discussion started by: alex31
3 Replies
Login or Register to Ask a Question