LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 04-01-2004, 07:21 AM   #1
fluppi
Member
 
Registered: Oct 2003
Location: Switzerland (Europe)
Distribution: OpenSuSE, RedHat, Knoppix, IRIX + MacOSX
Posts: 198

Rep: Reputation: 30
How to calculate file age ? "File xy is 2 days 3 hours old"


Hello

I'm trying to make a list of the age or the elapsed time since the last modification of my files.

It should look this way:
File xy 2 days 3 hours 11 minutes
File yz 1 month 5 days 7 hours 23 minutes

I tried to do this with "ls -l" and awk for the values, but it's the wrong way.
I did not found anything like filedate - sysdate.


I prefer bash, put perl will be fine too :-)

TIA
Fluppi
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-01-2004, 11:46 PM   #2
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Rep: Reputation: 46
la -la

it will output like this:

drwxr-xr-x 2 exodist users 4096 Mar 31 00:45 azureus/

make a script that takes the date from the output and subtracts it from the current date, then have it output what you need. I cannot write this script inless than 1/2 hour, if u need help with scripting u can ask in the programming forum
 
Old 04-02-2004, 08:51 AM   #3
fluppi
Member
 
Registered: Oct 2003
Location: Switzerland (Europe)
Distribution: OpenSuSE, RedHat, Knoppix, IRIX + MacOSX
Posts: 198

Original Poster
Rep: Reputation: 30
I found the solution.

I get the UNIX-Systemdate in seconds with NOW=`date +%s`
Then I get the UNIX-Timestamp of the file with OLD=`stat -c %Z filename`

And finally I get the age by expr $NOW - $OLD

(Using ls-la will give you sometimes -32 minutes or too much to code ;-)
 
Old 06-23-2011, 01:57 PM   #4
aniserg
LQ Newbie
 
Registered: Jun 2011
Posts: 1

Rep: Reputation: Disabled
thx you fluppi!

this topic helped me! THX!
 
Old 03-14-2012, 06:45 AM   #5
voip.linux.expert
LQ Newbie
 
Registered: Mar 2012
Posts: 1

Rep: Reputation: Disabled
Thanks

This top helped me !

for oneliners, display age of the file in minutes:
OLD=`stat -c %Z file_name`; NOW=`date +%s`; (( DIFF = (NOW - OLD)/60 )) ; echo "This file is $DIFF m old"
 
2 members found this post helpful.
Old 06-03-2013, 11:27 AM   #6
CJTC
LQ Newbie
 
Registered: Jun 2013
Posts: 1

Rep: Reputation: Disabled
Lightbulb Full Answer

This code will get the age in seconds and format the answer in days, hours, minutes and secondes (For demonstration I used the age of /etc/hosts) :


#!/bin/bash

#===============================================================================
# Gets a quantity of seconds and formats the output into Days, Hours, Minutes and seconds
#
# @parm Seconds
# @return string containing days, hours Minutes and seconds example: 0D 0H 0M 1S
#
# dependencies:none
#
#===============================================================================
function SecondsToDaysHoursMinutesSeconds()
{
local seconds=$1
local days=$(($seconds/86400))
seconds=$(($seconds-($days*86400) ))
local hours=$(($seconds/3600))
seconds=$((seconds-($hours*3600) ))
local minutes=$(($seconds/60))
seconds=$(( $seconds-($minutes*60) ))
echo -n "${days}D ${hours}H ${minutes}M ${seconds}S"
}

#===============================================================================
# Gets the age of a file in seconds
#
# @parm a file name (example: /etc/hosts
# @return Age in seconds
#
# dependencies:none
#
#===============================================================================
function FileAge()
{
echo $((`date +%s` - `stat -c %Z $1`))
}

echo $(SecondsToDaysHoursMinutesSeconds $(FileAge /etc/hosts) )


The output will look something like this:

36D 8H 46M 9S

Hope this helps.

Last edited by CJTC; 06-03-2013 at 11:31 AM. Reason: Clerification and spelling correction
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"bad interpreter : no such file or directory" when configure "flex" acer_peri Linux - Software 10 11-10-2010 02:19 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 10:00 AM
decrypting a file using "crypt" & "rot13" JAMZM101 Solaris / OpenSolaris 1 03-07-2004 10:32 AM
Java does "age" or "Age" matter when declaring an Int?? Laptop2250 Programming 3 10-13-2003 01:34 PM
Can't locate object method "splitpath" via package "File::Spec" RobJohnston Linux - General 2 06-28-2003 10:59 AM


All times are GMT -5. The time now is 08:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration