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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-01-2004, 06:21 AM
|
#1
|
Member
Registered: Oct 2003
Location: Switzerland (Europe)
Distribution: OpenSuSE, RedHat, Knoppix, IRIX + MacOSX
Posts: 198
Rep:
|
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.
|
04-01-2004, 10:46 PM
|
#2
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374
Rep:
|
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
|
|
|
04-02-2004, 07:51 AM
|
#3
|
Member
Registered: Oct 2003
Location: Switzerland (Europe)
Distribution: OpenSuSE, RedHat, Knoppix, IRIX + MacOSX
Posts: 198
Original Poster
Rep:
|
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 ;-)
|
|
1 members found this post helpful.
|
06-23-2011, 12:57 PM
|
#4
|
LQ Newbie
Registered: Jun 2011
Posts: 1
Rep: 
|
thx you fluppi!
this topic helped me! THX!
|
|
|
03-14-2012, 05:45 AM
|
#5
|
LQ Newbie
Registered: Mar 2012
Posts: 1
Rep: 
|
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.
|
06-03-2013, 10:27 AM
|
#6
|
LQ Newbie
Registered: Jun 2013
Posts: 1
Rep: 
|
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 10:31 AM.
Reason: Clerification and spelling correction
|
|
|
All times are GMT -5. The time now is 06:45 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|