How to get access time of a file upto the precision of seconds?

Hi ,

How can I get the last access time of a file upto the precesion of seconds in Unix.
I cannot use stat as this is not supported.

Perl has a built in 'stat' command. Is this available to you?

1 Like

No the stat command is not avaiable to me..
Neither is the anewer command avaiable.

Actually I want to find out if the file was just accessed at the moment..
So to use atime with find is an option but how can I giive 1 sec with it.
PLease help.

Hi Kanus,

You can use certainly find command with seconds precision as follow:

 # 10s/(60sXmin)=0.166666667 min
# 1s/(60sXmin)=0.0166666667 min
Then:
# 1-) Looking files modified less than 10 seconds ago:
find . -type f -amin -0.166666667

# 2-) Looking files modified less than 1 seconds ago:
find . -type f -amin -0.0166666667

# 3-) Looking files modified between 1 sec and 10 sec ago:
find . -type f -amin +0.01666667 -amin 0.166666667

Options to include this in a script would be:

# Script 1: Print files modified $ns seconds ago:
ns=$(echo "1/60" | bc -l) # ns=Number of seconds=1 in this case
find . -type f -amin -$ns

# Script 2: Print files modified $ns seconds ago, showing date and time:
ns=$(echo "10/60" | bc -l)
last=$(find . -type f -amin -$ns)

for each in $last
do
ls -l $(basename $each) | awk '{print $6,$7,$8}' 
done

I hope this can be a start about what you need.

Regards.

Thanx so much for your explaination !!

But the problem is my find coomand doent supoort atime..It gave me the error :

find: bad option -amin

Is there any other method..

What Operating System and version do you have?

uname -a

Do you have "root" access and the administrative authority to install aditional software?
The suggestion about using Perl is a good one.

Your post does not imply that you are a "C language" programmer.

Hi Kanus

If you want to get only last access time of specific file you could use "ls" command as follow:

ls -ltu --time-style=+%H:%M:%S yourfile # print last access time of yourfile with seconds precision.

Or

ls -l --time=access -u --time-style=+%H:%M:%S yourfile # print last access time of yourfile with seconds precision.

If you want to get recursively (in subfolders too), last access time all files/folders you can do:

ls -lR --time=access -u --sort=time --time-style=+%H:%M:%S # Sorted by time, last accessed on top

Other examples (if "--time, --sort" options don't work in your system) try:
(*All the following examples aren't printed with seconds precision, because --time-style=+FORMAT is missing.

ls -ltu yourfile #access time of yourfile
 
ls -ltu #sort by, and show, access time all files within folder, except subfolders

ls -ltuR #sort by, and show, access time all files within folder and subfolders

I hope this is near what you need and work in your system.

Regards.

I dont understand what works here..This is the output when I ran the above command..What do I do ?????

ls -ltu --time-style=+%H:%M:%S abc
ls: illegal option -- -
ls: illegal option -- e
ls: illegal option -- -
ls: illegal option -- y
ls: illegal option -- e
ls: illegal option -- =
ls: illegal option -- +
ls: illegal option -- %
ls: illegal option -- H
ls: illegal option -- :
ls: illegal option -- %
ls: illegal option -- M
ls: illegal option -- :
ls: illegal option -- %
ls: illegal option -- S
usage: ls -1RaAdCxmnlhogrtucpFbqisfL@ [files]

---------- Post updated at 01:34 AM ---------- Previous update was at 01:32 AM ----------

@methyl :

uname -a
SunOS ohclexor4022 5.9 Generic_122300-08 sun4u sparc SUNW,Sun-Fire-880

I dont have the root level access.
I have never worked on perl before.Can I use it in my shell script.
??
Please help....... :frowning: :frowning:

See the last post in this thread for my perl solution.

1 Like

Hi Kanus,

I don't have access to SunOS System, but I found man page of ls in the web. I see in SunOS doesn't have --time-style option. For that reason you receive that errors.

Reading the options you have, instead of:

ls -ltu --time-style=+%H:%M:%S yourfile

try with the following 2 command and see the difference:

ls -ltu yourfile

and

ls -etu yourfile # I guess this could be near of what you want 
Reference: man ls in SunOS 5.10
     -e           The same as -l, except  displays  time  to  the
                  second,  and  with  one  format  for  all files
                  regardless of age: mmm dd hh:mm:ss yyyy.

     -E           The same as -l, except  displays  time  to  the
                  nanosecond  and  with  one format for all files
                  regardless       of       age:       yyyy-mm-dd
                  hh:mm:ss.nnnnnnnnn (ISO 8601:2000 format).

                  In addition, this option  displays  the  offset
     -l           Lists in long format, giving mode, ACL  indica-
                  tion,  number  of  links, owner, group, size in
                  bytes, and time of last modification  for  each
                  file  (see  above).  If  the  file is a special
                  file, the size field instead contains the major
                  and  minor  device numbers. If the time of last
                  modification is greater than six months ago, it
                  is  shown  in  the format `month date year' for
                  the  POSIX  locale.  When  the  LC_TIME  locale
                  category is not set to the POSIX locale, a dif-
                  ferent format of the time field  can  be  used.
                  Files  modified  within  six months show `month
                  date time'. If the file is a symbolic link, the
                  filename  is  printed  followed by "->" and the
                  path name of the referenced file.

     -t           Sorts by time stamp (latest first)  instead  of
                  by  name.  The default is the last modification
                  time. (See -u and -c.)

     -u           Uses time of last access instead of last modif-
                  ication  for  sorting  (with  the -t option) or
                  printing (with the -l option).

Hope it helps.

Regards.

1 Like

Thanks for helping me guys..
I really appreciate it..But what do I do..nothing works here..
this is what I got..as output..

ls -ltu --time-style=+%H:%M:%S abc
ls: illegal option -- -
ls: illegal option -- e
ls: illegal option -- -
ls: illegal option -- y
ls: illegal option -- e
ls: illegal option -- =
ls: illegal option -- +
ls: illegal option -- %
ls: illegal option -- H
ls: illegal option -- :
ls: illegal option -- %
ls: illegal option -- M
ls: illegal option -- :
ls: illegal option -- %
ls: illegal option -- S
usage: ls -1RaAdCxmnlhogrtucpFbqisfL@ [files]

m using the following version :

uname -a

SunOS ohclexor4022 5.9 Generic_122300-08 sun4u sparc SUNW,Sun-Fire-880

:wall: :wall:

---------- Post updated at 02:58 AM ---------- Previous update was at 02:51 AM ----------

ls -etu abc
ls: illegal option -- e
usage: ls -1RaAdCxmnlhogrtucpFbqisfL@ [files]

:confused:

---------- Post updated at 02:59 AM ---------- Previous update was at 02:58 AM ----------

The man page u attaches is og 5.10 mine is 5.9///
thats y.. :frowning:

---------- Post updated at 04:54 AM ---------- Previous update was at 02:59 AM ----------

Thanx a milllion Perderabo..
Ur perl command is working...M sooooooo happy..Thanks every1 for your time and help !!!