Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read date attribute on a file

Status
Not open for further replies.

srishan

MIS
Oct 29, 2002
55
US
-rw-r--r-- 1 sridh_c oaa 3 Mar 13 09:59 TEST.txt

From the above file description, I need to get Mar 13 09:59. There should be a command, more like SED or AWK command?

I will appreciate the help.

Thx,
Sri
 
Hello,

You can use a combination of this command into a script to obtain the info in the format you need or just issue the command:

%ls -l | tr -s " " | cut -d " " -f6

Something like:

%ls -l | tr -s " " | cut -d " " -f6,7,8

tr -s " " eliminates all blank spaces except one in each separator and cut cuts the desired columns.

Hope that this helps you!

Bye,

jmiturbe
 
Hello again,

Note that there is a space between the quotes " ", both on tr and on cut!!!!!!!

I hope that both cut and tr exist on your Unix "distribution". If not just post, maybe we'll find another way.

Good luck!

jmiturbe
 
hi ,

you could do something like

ls -l | awk ' {print $6" " $7" " $8" "}'
 
Thanks!! to you both!

There is one more dangling problem, which is the year.

Once I get the "Mar 13 09:59" value, I need to be able to say which year it is. I am in HP UX, and it is keeping the time stamp for the files which were touched(updated) in the last 6 months and for the rest it is keeping the actual year like "2000" for year 2000. The purpose of this functionality is to automate the version control of the files, what I usually do for version control is, save the old version with this date as it's extension, before bringin in the new file to the directory, this way I save the previous versions of the programs.

filename.MMDDYY

This is where I need to convert the time stamp to year.

Any advises?

For Month conversion, I have written a function like,

format_month()
{
mm=$1
case $mm in
"Jan")
mm="01"
;;
"Feb")
mm="02"
;;
"Mar")
mm="03"
;;
...
... and so on
}

This works, but looks cumbersom.
Is there a better way?

Thx,
Sri
 
You have an additional problem that the format of the line changes when the file goes over 6 months old! Here's a snippet from an [tt]ls -la[/tt] in one of my directories...
[tt]
-rwxr-xr-x 1 sambones somegrp 447 Jan 18 2001 oraenv.sh
-rwxr-xr-x 1 sambones somegrp 2947 Jun 26 2001 phonelist.sh
-rwxr-xr-x 1 sambones somegrp 605 Jul 9 2001 prep
-rw-r--r-- 1 sambones somegrp 127 Feb 13 16:41 ptr.c
-rw-r--r-- 1 sambones somegrp 108 Feb 13 16:39 ptr.c~
-rw-r--r-- 1 sambones somegrp 2287 Apr 24 2001 roadresc.sh
-rwxrwxrwx 1 sambones somegrp 760 Nov 2 2000 sample.sh
[/tt]
Notice that [tt]prt.c[/tt] and [tt]prt.c~[/tt] show the time, but the older files show the year.

It's a little easier and straight to the point if you use C. Here's some code that will display the last modification date/time stamp of a file...
[tt]
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>

int main(int argc, char ** argv)
{
struct stat filestats;

if ( argc != 2 )
{
fprintf(stderr, &quot;%s: incorrect number of parameters!\n&quot;, argv[0]);
return( -1 );
}

if( stat(argv[1], &filestats) )
{
fprintf(stderr, &quot;%s: Couldn't get file stats for %s!\n&quot;, argv[0], argv[1]);
return( -1 );
}

printf(&quot;%s&quot;, asctime(localtime(&filestats.st_mtime)));

return(0);
}
[/tt]
This prints out something like...
[tt]
Thu Mar 13 11:42:39 2003
[/tt]
...which has all the date information you need. There are other time formatting functions that can give it to you in any format you want.

Hope this helps.

 
Code:
$ cc filedate.c
(Bundled) cc: &quot;filedate.c&quot;, line 1: error 1705: Function prototypes are an ANSI feature.
(Bundled) cc: &quot;filedate.c&quot;, line 3: error 1574: Unknown size for &quot;filestats&quot;.
(Bundled) cc: &quot;filedate.c&quot;, line 7: error 1588: &quot;stderr&quot; undefined.
(Bundled) cc: &quot;filedate.c&quot;, line 13: error 1588: &quot;stderr&quot; undefined.
(Bundled) cc: &quot;filedate.c&quot;, line 17: error 1530: Undefined struct or union.
(Bundled) cc: &quot;filedate.c&quot;, line 17: error 1555: Address operator requires an lvalue.

Sambones,

New to this area. Could you see what may be wrong?

Also, once I compile this, can I just call it from Shell?

Thx,
Sri
 
Oops!!!!

I forgot to include the #include files,

Yet, the following error remains,

(Bundled) cc: &quot;filedate.c&quot;, line 6: error 1705: Function prototypes are an ANSI feature.

Please advise.

Thx,
Sri
 
Changed the main funciton as the following,

main(argc, argv)
int argc;
char *argv[];
{

...



And it works like a charm!!

Thx a LOT!!

Sri
 
I compiled it originally with the GNU C Compiler. The command was...
[tt]
gcc filedate.c -o filedate
[/tt]
You're apparently using an older K&R C Compiler. BUT, if you get it to work, that's all that matters.

Usage would be...
[tt]
THEFILE=myfile.txt
FILEDATE=`filedate ${THEFILE}`
[/tt]
...or something like that.

Hope this helps.

 
Keep in mind that this displays the last time the file was modified. If you want to have it display the last time the file was accessed, change the [tt]st_mtime[/tt] to [tt]st_atime[/tt]. Or, to get the last time there was a status change on the file, change it to [tt]st_ctime[/tt].

As for what these times are, here's what the [tt]man[/tt] page for [tt]stat[/tt]...
[tt]
st_atime Time when file data was last accessed. Changed by
the following functions: creat(), mknod(),
pipe(), utime(2), and read(2).

st_mtime Time when data was last modified. Changed by the
following functions: creat(), mknod(), pipe(),
utime(), and write(2).

st_ctime Time when file status was last changed. Changed
by the following functions: chmod(), chown(),
creat(), link(2), mknod(), pipe(), unlink(2),
utime(), and write().
[/tt]
Hope this helps.

 
Sambones,

Very helpful!! Thanks again!!

Could you tell me the difference of these two,

Code:
 time_t   st_mtime;     /* Last modification time */
 time_t   st_ctime;     /* Last file status change time */

What I need is the last time it is modified and saved DATE.

Thx,
Sri
 
Actually, I think I got it.

Please ignore my previous post.

Thanks a lot for all the help.

Thx,
Sri
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top