UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old July 23rd, 2004, 11:06 AM
rosebud1 rosebud1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 rosebud1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unix timestamps

I haven't done a whole lot with scripting. I need to get a timestamp from unix files displayed in strictly number format. ie - YYMMDDhhmmss with nothing else shown but the time stamp (not even the filename). Anybody know who to do this?

Actually I really just want to compare files to see which is the oldest. Any ideas?

Reply With Quote
  #2  
Old July 23rd, 2004, 11:48 AM
jim mcnamara jim mcnamara is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,299 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 41 m 52 sec
Reputation Power: 47
You cannot get seconds in a timestamp using just shell commands You have to resort to some facility that can access stat() - perl, python C.
Here is a perl version. Just pass it one file name at a time.
[
Code:
#!/bin/perl

#^###################################################################
#^ $Header: inodetime.pl,v 1.1 97/01/20 10:17:00 http srvr? $
#^###################################################################
#^ PROGRAM DESCRIPTION
#^ -------------------
#^ This program prints the modification times and names of files.
#^ It uses the following format:  inodetime.pl filename
#^ It will accept:  inodetime.pl filename1 filename2 filename3
#^                  inodetime.pl /tmp/file*
#^ The format of the output is: YYYYMMDDhhmmss (changed for this post) jmc
#^ example:
#^           $ inodetime.pl /tmp/t*
#^           19961115105425 
#^           19970116113616 
#^ 
#^ TRICK To Remember The Program Name
#^ ----------------------------------
#^     inodetime == I No De Time
#^     ^^ ^ ^ 
#^                                                                KSG 
#^###################################################################

############################################
# Get the (next) input from the command line 
############################################
while ($curfile = $ARGV[0])
{
   #################################################
   # Do following code block only if $curfile exists
   #################################################
   if (-e $curfile)
   {
      ###############################
      # stat structure into variables
      ###############################
      ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
      $atime,$mtime,$ctime,$blksize,$blocks)
      = stat("$curfile");
      ###############################
      # time structure into variables
      ###############################
      local($sec,$min,$hr,$day,$mon,$yr,$wday,@dntcare) = localtime($mtime);
      $yr = ($yr>=70) ? $yr+1900 : $yr+2000;
      $yr="$yr";
      $mon = (++$mon < 10) ? "0$mon" : "$mon";
      $day = ($day < 10) ? "0$day" : "$day";
      $hr  = ($hr < 10) ? "0$hr" : "$hr";
      $min = ($min < 10) ? "0$min" : "$min";
      $sec = ($sec < 10) ? "0$sec" : "$sec";
      ##################################################################
      # Rearrange in the YYYYMMDDhhmmss format and assign to $dte variable
      ##################################################################
      $dte = join('',$yr,$mon,$day,$hr,$min,$sec);
      ######################################
      # Print modification date and filename
      ######################################
      print ("$dte \n");
      }
   ########################################
   # Shift to next position in command line
   ########################################
   shift (@ARGV);
}





Reply With Quote
  #3  
Old July 23rd, 2004, 12:00 PM
rosebud1 rosebud1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 rosebud1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Wow, thank you! Can't wait to try it out! Do you know how to do it with shell commands if you don't include seconds?

Thanks for your help. I'll let you know how it works.

Reply With Quote
  #4  
Old July 23rd, 2004, 01:18 PM
jim mcnamara jim mcnamara is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,299 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 41 m 52 sec
Reputation Power: 47
Code:
ll <filename>

gives you time information about the last access to file.

Reply With Quote
  #5  
Old July 23rd, 2004, 01:21 PM
jim mcnamara jim mcnamara is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,299 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 41 m 52 sec
Reputation Power: 47
Code:
ls -lrt <filenames>

will sort files by time, oldest first, newsest last.

Reply With Quote
  #6  
Old July 23rd, 2004, 01:23 PM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 5
If you have two files, say, filea and fileb, and you want the older file, do:
ls -tr filea fileb | sed 1q

Reply With Quote
  #7  
Old July 26th, 2004, 02:54 PM
rosebud1 rosebud1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 rosebud1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you very much everyone for your info, it's proven extremely helpful. I think I have what I need to know now. Thanks again.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Unix timestamps


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway