Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Showing age of a file

  • 10-10-2005 12:24pm
    #1
    Registered Users, Registered Users 2 Posts: 7,497 ✭✭✭


    Is there any way to show the age of a file in the shell rather than the date it was modified/created/

    some option with 'ls' maybe?


Comments

  • Closed Accounts Posts: 95 ✭✭krinDar


    quarryman wrote:
    Is there any way to show the age of a file in the shell rather than the date it was modified/created/

    I am not aware of any single utility to do it, but you can figure it out yourself.
    
    #!/bin/bash 
    
    if [ ! -f $1 ]
    then
            echo "usage: $0 <file>"
            exit 1;
    fi
    
    # determine modification date in seconds since epoch
    mt=$(stat -c %Y $1);
    
    # determine the current time
    ct=$(date "+%s")
    
    dt=$(( ct - mt));
    
    echo "$1 is $dt seconds old"
    
    

    This works for me on SuSe 9.

    Requires gnu core utilities - most recent Linux flavours should have it,


  • Registered Users, Registered Users 2 Posts: 7,497 ✭✭✭quarryman


    looks good but what is '-c'?

    I get 'invalid arguement".


  • Closed Accounts Posts: 95 ✭✭krinDar


    quarryman wrote:
    looks good but what is '-c'?

    I get 'invalid arguement".

    It tells stat how to format the output. We are looking for the
    modification time in seconds since epoch.

    Read the man page for stat and see if it lets you define the
    format. Also, what OS are you using ?


  • Registered Users, Registered Users 2 Posts: 7,497 ✭✭✭quarryman


    here's a screen of my man stat page.

    not sure which, i think its Red Hat.


  • Closed Accounts Posts: 95 ✭✭krinDar


    quarryman wrote:
    here's a screen of my man stat page.

    not sure which, i think its Red Hat.

    to find out do the following

    uname -a
    cat /etc/issue

    Having said that it looks like the version of stat you have does not
    support the formatting option, you can change the script I attached
    to the following, it might work, it is difficult to know considering we
    don't know the OS yet:
    #!/bin/bash 
    
    if [ ! -f $1 ]
    then
            echo "usage: $0 <file>"
            exit 1;
    fi
    
    # determine modification date in seconds since epoch
    set -- $(stat -t $1);
    mt=${13}
    
    # determine the current time
    ct=$(date "+%s")
    
    dt=$(( ct - mt));
    
    echo "$1 is $dt seconds old"
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 7,497 ✭✭✭quarryman


    that works perfect! thanks.

    can you tell me what these lines do exactly so?

    set -- $(stat -t $1);
    mt=${13}

    thanks again


  • Closed Accounts Posts: 95 ✭✭krinDar


    quarryman wrote:
    that works perfect! thanks.

    can you tell me what these lines do exactly so?

    Can you tell me the OS ?

    It makes it a lot easier to answer questions of this nature if you
    include as much information as possible, such as the OS, some
    examples etc.
    set -- $(stat -t $1);
    mt=${13}

    Stat, as the manpage explained, displays information about the file specified.
    Run it (without the -t argument) and you will see what it does. Parsing the
    normal output of stat is awkward, -t makes it easier to parse, all the
    information is on one line and is space separated.

    The 'set --' part takes the output of the 'stat' command and puts it into
    the positional parameters, so $1 is the first part of the output from
    stat - the filename, $2 the second part - the sized ... and $13 is the
    modified time in seconds since epoch.


  • Registered Users, Registered Users 2 Posts: 7,497 ✭✭✭quarryman


    from uname -a i get:

    Linux siexxxxx xxxxxxxxx SMP Tue Jan 27 00:35:25 PST 2004 i686 unknown

    let me know if the xxx'd data is required.
    The 'set --' part takes the output of the 'stat' command and puts it into
    the positional parameters, so $1 is the first part of the output from
    stat - the filename, $2 the second part - the sized ... and $13 is the
    modified time in seconds since epoch.

    ok so this is similar to awk then? i might try using that instead.


  • Closed Accounts Posts: 95 ✭✭krinDar


    quarryman wrote:
    from uname -a i get:

    What about the content of /etc/issue
    ok so this is similar to awk then? i might try using that instead.

    No. Not at all. 'set' is part of the shell. Awk is a totally different utility
    that is much more powerful.


Advertisement