Solved

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

Posted on 2004-10-17
7
502 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now