Solved

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

Posted on 2004-10-17
7
501 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:kevin_ma
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

16 Experts available now in Live!

Get 1:1 Help Now