Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Search Forums:



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-11-2004
Registered User
 

Join Date: Aug 2002
Location: London, England
Posts: 92
Thanks: 1
Thanked 0 Times in 0 Posts
Question time stamp of file create

Hi,


Sounds a simple request but I also need (would like) to gather the seconds too. I'm not even sure if this is held. I would think it is, somewhere??!!?!

I belive that stat would/could work but I don't do C (we'll not yet).

Is there any comamnd line util I can use?

SunOS.

Cheers and beers,
Neil
Sponsored Links
    #2  
Old 06-11-2004
Perderabo's Avatar
Unix Daemon (Adminstrator Emeritus)
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,679
Thanks: 26
Thanked 238 Times in 148 Posts
Unix does not store the file's creation time.
Sponsored Links
    #3  
Old 06-11-2004
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 8,511
Thanks: 67
Thanked 403 Times in 392 Posts
perl version for inodetime

Code:
#!/usr/contrib/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 filename
#^ example:
#^           $ inodetime.pl /tmp/t*
#^           19961115105425 /tmp/test.sql 
#^           19970116113616 /tmp/tststat.pl 
#^ 
#^ 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 $curfile\n");
      }
   ########################################
   # Shift to next position in command line
   ########################################
   shift (@ARGV);
}


#~###################################################################
#~###################################################################
#~
#~ $Log:	inodetime.pl,v $
#~ Revision 1.1  97/01/20  10:17:00  10:17:00  http ()
#~ reversed output
#~ from: filename date
#~ to:   date filename
#~ 
#~ Revision 1.0  97/01/17  12:37:32  12:37:32  http ()
#~ Initial revision
#~ 
#~###################################################################
#~###################################################################

    #4  
Old 06-11-2004
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 8,511
Thanks: 67
Thanked 403 Times in 392 Posts
Sort of the same thing in C - using stat

Code:
/* this displays files times to the second: ctime & mtime */
/* usage filetime <file name> [file name...[]] -  for HPUX */

/* jmc 4/25/1997 10:09AM */

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>   
#include <string.h>
#include <errno.h>


void statfile(char *, struct stat *);
void usage(void); 
void bail(void);  
void filetimes(char *, struct stat *);    

int main(int argc, char *argv[]){      
       struct stat st;
       int i=0;    
       char tmp[256]={'\0'};
       if(argc <2) usage();       
       for(i=1; i<argc;i++){       
               strcpy(tmp,argv[i]);
               statfile(tmp,&st);
               filetimes(tmp,&st);            
       }        
       
       return 0;
}                         
void filetimes(char *fname,struct stat *st){  /* display the file times */
       struct tm *tmptr;            
       struct stat lst;           
       char output[91]={'\0'};
       lst = *st;   
       printf("File times for %s\n",fname);
       tmptr=localtime(&lst.st_atime);
       strftime(output,80,"Last status change: %D %T",tmptr);
       printf("\t%s\n",output);
       tmptr=localtime(&lst.st_mtime);
       strftime(output,80,"Last modify time  : %D %T",tmptr);
       printf("\t%s\n",output);

       
}
void statfile(char *fname, struct stat *st){
       int result=0;   
       result=stat(fname,st);
       if(result == (-1)) bail();
}               
void usage(void){
    printf("usage:  filetime <filenames> \n");  
    bail();
}                

void bail(void){
    if (errno) perror("Fatal error");
    exit(EXIT_FAILURE);
}

Sponsored Links
    #5  
Old 06-11-2004
Registered User
 

Join Date: Aug 2002
Location: London, England
Posts: 92
Thanks: 1
Thanked 0 Times in 0 Posts
Many thanks....Perl solution is great.

Cheers and beers,
Neil
Sponsored Links
    #6  
Old 06-14-2004
dangral dangral is offline Forum Advisor  
Registered User
 

Join Date: Oct 2002
Posts: 722
Thanks: 3
Thanked 3 Times in 3 Posts
Jim,

I'm a bit confused. I was under the impression that Unix doesnt track the create time of a file.

Isnt your script tracking i-node changes? In which case the time would change when someone does a chown or chmod.
Sponsored Links
    #7  
Old 06-14-2004
Registered User
 

Join Date: Mar 2004
Location: Esher, Surrey, England.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
I've just tried the perl script version and it just brings back the last time the file was updated.

I think nhatch wanted the time the file was actually created, which ( as has been said) isn't stored ...
Sponsored Links
Closed Thread

Tags
mtime, perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Copy all the files with time stamp and remove header,trailer from file ksrams UNIX for Dummies Questions & Answers 35 07-30-2007 02:15 PM
Need to delete the files based on the time stamp of the file samudha UNIX for Dummies Questions & Answers 2 06-20-2007 07:02 AM
how do i put a time stamp in a file name jhamm Shell Programming and Scripting 5 01-29-2007 09:00 AM
Inserting Date&Time Stamp In Existing Log File shephardfamily UNIX for Dummies Questions & Answers 3 02-24-2006 03:01 AM
File date and time stamp Xenon UNIX for Dummies Questions & Answers 1 10-09-2001 03:58 PM



All times are GMT -4. The time now is 03:29 AM.