Solved

Get the last modification date and print the number of years and months since last update

Posted on 2004-10-17
7
508 Views
Last Modified: 2010-04-20
How do you get the last modification date of a file and print the number of years and months since that last update in the format

filename has last been updated YY years and MM months ago.

?

0
Comment
Question by:kevin_ma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 12333017
you can get it by using the output of the ls command ,or you can do it programmatically by using the
stat() function to get the stat structure which is

struct stat {
    dev_t         st_dev;      /* device */
    ino_t         st_ino;      /* inode */
    mode_t        st_mode;     /* protection */
    nlink_t       st_nlink;    /* number of hard links */
    uid_t         st_uid;      /* user ID of owner */
    gid_t         st_gid;      /* group ID of owner */
    dev_t         st_rdev;     /* device type (if inode device) */
    off_t         st_size;     /* total size, in bytes */
    blksize_t     st_blksize;  /* blocksize for filesystem I/O */
    blkcnt_t      st_blocks;   /* number of blocks allocated */
    time_t        st_atime;    /* time of last access */
    time_t        st_mtime;    /* time of last modification */
    time_t        st_ctime;    /* time of last change */
};

from which you can get the time of last modification

the stat manpage is here

http://www.netadmintools.com/html/2stat.man.html
0
 

Author Comment

by:kevin_ma
ID: 12333157
I'll rephrase my question

How do you get the last modification date of a file and print the number of years and months since today ( the current date ) in the format

filename has last been updated YY years and MM months ago.

?
0
 
LVL 11

Expert Comment

by:avizit
ID: 12333275
You call the function stat() with the filename as the argument and also pointer to a buffer to have the stat values filled in

after stat() returns , the stat values would be filled .. so you can get the value of  st_mtime  which is the time the file was last modified.

>>the time_t data type is a compact representation, typically giving the number of seconds elapsed since some implementation-specific base time.

you can use gmtime() to get the usual fields i.e the month , day , year etc etc

now that you have the file modification time
you can use the tim() function to get the current time

do read the part titled "date and time" in http://snap.nlc.dcccd.edu/learn/fuller3/chap4/chap4.html

after that its just a question of finding the time difference between the two dates



0
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 

Author Comment

by:kevin_ma
ID: 12334650
Can you write out the code with the correct syntax so I can have it printed as

filename has last been updated YY years and MM months ago.

I'll reward you with the 500 then =)
0
 
LVL 1

Accepted Solution

by:
pokui earned 500 total points
ID: 12339042
well, the following bash script should do...(get rid of the extra lines where I've put backslashes...).

Note that this assumes a 30 day month. Catering for calendar months is your exercise.

#!/bin/bash

if [ $# -lt 1 ] ; then
        echo "Usage.. $0 filename"
        exit
fi

months=$(echo "$(echo `date +%s` -`date\ -r $1 +%s` | bc) / (60*60*24*30)" | bc)

echo "The file has last been updated \ $(echo $months / 12 | bc) years and  \ $(echo $months % 12 | bc) months ago"

0
 
LVL 1

Expert Comment

by:pokui
ID: 12339057
oops... formatting issues, anyhow after the if...fi part, there is a line that begins with "months" and another that begins with "echo".
0
 
LVL 2

Expert Comment

by:Sunjith
ID: 12344774
Please credit pokui. Just a few additions:
-----------------------------
#!/bin/bash

if [ $# -ne 1 ] ; then
        echo "Usage: $0 filename"
        exit
fi

months=$(echo "$(echo `date +%s` -`date -r $1 +%s` | bc) / (60*60*24*30)" | bc)

echo "$1 has last been updated $(echo $months / 12 | bc) years and $(echo $months % 12 | bc) months ago."
-----------------------------
Put the code above (between the lines) in a file, say, lastupdate.sh

chmod 755 lastupdate.sh
./lastupdate.sh filename
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question