Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-10-17
7
Medium Priority
?
514 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1500 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

916 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