Link to home
Start Free TrialLog in
Avatar of jvossler
jvosslerFlag for United States of America

asked on

Shell scripting (ksh) to get file age


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.

Avatar of ozo
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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}'`
Avatar of jvossler



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.

Avatar of Tintin

If you had GNU ls, then you can simply do

ls --full-time


ls --time-style=+%Y%m%d%H%S
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}'"
echo "/NAS/${BKHOST}/${FNAME}.${SUFFIX}"


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 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.


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.