Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-06-2014, 04:23 AM   #1
Registered: Dec 2013
Posts: 35

Rep: Reputation: Disabled
convert st_mtime to minutes from now

hello all,

ive been trying to search for an answer but its so hard to find a proper answer, here is my problem. In a linux machine, i want to check for files age and remove them if they are older than 2 hours. but i can get the st_mtime but dont know how to convert that to "how may minutes from now". this is the python script i created so far :

#!/usr/bin/env python

import os
import glob
import commands
import time

location = "/usr/local/testlogs/text/*"

for file in glob.glob(location) :
    #print file
    st = os.stat(file)
    mtime = st.st_mtime
    mtime2 = time.gmtime(mtime)
    mtime2_hr = time.strftime("%m/%d/&Y %H:%M:%S", mtime2)
    print mtime2_hr
the output i get show's when the file was last modified in "%m/%d/&Y %H:%M:%S" format. how to get it to ONLY minutes from now format. please help im new to python

Last edited by simhumcon; 11-06-2014 at 04:30 AM.
Old 11-06-2014, 09:49 AM   #2
Senior Member
Registered: Aug 2009
Distribution: CentOS
Posts: 3,519

Rep: Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546
Why on Earth do you take a time stamp that is already in its simplest form (seconds since the epoch), laboriously convert it to the form least suitable for computation, and then try to do arithmetic on it? All you need to do is subtract st.st_mtime from the current time and divide by 60 to convert seconds to minutes.
age_minutes = (time.time() - st.st_mtime) / 60
Old 11-06-2014, 07:06 PM   #3
Registered: Dec 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
wow i didnt know we could to do that! thank you very much rknichols! im really new to python. thanks again


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
convert total no of seconds in the format hour minutes and seconds suchi_s Programming 15 03-15-2011 11:34 AM
[SOLVED] bash: Convert decimal to minutes:seconds? DJCharlie Programming 10 01-26-2011 07:05 PM
[SOLVED] Using convert from imagemagick package to convert multiple jpg files janis_169 Linux - Newbie 5 07-18-2010 04:22 PM
[SOLVED] X takes 1.5 minutes to start xterm and 3.5 minutes to start GNOME gregorian Linux - Newbie 16 12-25-2009 05:56 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:45 PM.

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