Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
513 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
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 several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…
Suggested Courses

715 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