Avatar of jvossler
jvossler
Flag for United States of America asked on

Shell scripting (ksh) to get file age

Hello,

I am attempting to create a script that has as one of the function the determination of how old a file is.  I can use seconds, minutes, hours or days.  I have attempted to get the current epoch time via /usr/bin/perl -e 'printf "%d\n" , time;' and then use the stat perl function to get the files modifications date via

/usr/bin/perl -e 'printf "%d %d %d %d %d %d %d %d %d %d %d %d %d\n", stat("/NAS/${BKHOST}/${FNAME}.${SUFFIX}");' | awk '{print $11}'`

The problem is that the perl command will not make the variable substitutions and thus returns bogus information.

I am not set on using perl.  Any solution the I implement in ksh will work.  I just need to determine how old a file is in seconds, minutes, hours or days.  Parsing "ls" output is a problem due to older files having a year and newer files do not.

Any suggestions are helpful.

System Programming

Avatar of undefined
Last Comment
jvossler

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ozo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ozo

If you want to substitute shell variables, you need to use "
/usr/bin/perl -e "printf qq'%d %d %d %d %d %d %d %d %d %d %d %d %d\n', stat('/NAS/${BKHOST}/${FNAME}.${SUFFIX}');"| awk '{print $11}'`
jvossler

ASKER
ozo,

The "print -M shift" worked.  I'll still have to see about running this routine for dates other then the current date but I think I know how to do that.

I tried the "printf qq...." solution but the perl still returned the same incorrect value. I could run the command from the command line with the file name hard coded and get the correct value but in the ksh script it still would not work.

As an aside: any suggestions on how the get the age of a date specified by month and date (today is 9 17)

Thanks for the assistance.

Tintin

If you had GNU ls, then you can simply do

ls --full-time

or

ls --time-style=+%Y%m%d%H%S
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ozo

To get days since 09/17 you can do
perl -MDate::Manip -e 'print Delta_Format DateCalc("09/17","today"),1,"%dt"'

What do you get from
/usr/bin/perl -e "print '/NAS/${BKHOST}/${FNAME}.${SUFFIX}'"
or
echo "/NAS/${BKHOST}/${FNAME}.${SUFFIX}"

jvossler

ASKER
ozo,

From "/usr/bin/perl -e "print '/NAS/${BKHOST}/${FNAME}.${SUFFIX}'" I always get the file name using the variable substitutions.  The same with the echo.

The "Manip" command returns an error indicating I am missing the Manip.pm module.  I will persue this for apossible perl upgrade.  I am running the perl included with the Solaris 10 distribution and have not done any upgrades on it since installation.

jvossler

ASKER
ozo,

I got the "stat" command to work with variable substitution. Just like shell the meanings of the single quote and double quote are subtle but crutial to the outcome.

Many thanks for the assistance.

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.