≡ Menu

Linux display the date when a file was accessed with stat command

A quick question from my mail bag:

How do I display or get the date when a file was last time accessed?

The best and simplest way is to use stat command. It displays file or file system status such as:

=> File size

=> File type

=> Inode number

=> UID/GID

=> File access , modify and creation time etc.

stat command example

$ stat /etc/passwd
Output

  File: `/etc/passwd'
  Size: 2453            Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d      Inode: 25298826    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2007-08-06 04:45:18.000000000 -0500
Modify: 2007-07-17 16:25:12.000000000 -0500
Change: 2007-07-17 16:25:12.000000000 -0500

Display file system status

You can display file system status instead of file status. For example, display status for / file system:
$ stat -f /
Output:

  File: "/"
    ID: 0        Namelen: 255     Type: ext2/ext3
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 32161831   Free: 30458361   Available: 28798245
Inodes: Total: 33226752   Free: 33135357

stat command and shell scripts

You can use stat in a shell script. It supports -c option. By default it shows all information about file. Use -c option to specify FORMAT instead of the default. For example store access time in a shell variable:

ATIME=$(stat -c "%x" /etc/passwd)
echo $ATIME

See the stat command man page for full details for the valid format sequences for files:
man stat

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 17 comments… add one }

  • frank August 13, 2007, 1:43 am

    stat command example

    $ ls /etc/passwd

    this could be “$ stat /etc/passwd” ?

  • nixCraft August 13, 2007, 4:33 am

    Thanks for the heads up! The post has been updated.

  • pradeep August 13, 2007, 6:58 am

    Dear Sir,
    I want know the time of command when command run my any user or root on linux system

    Thanks
    pradeep

  • Gonçalo August 13, 2007, 9:53 am


    => File size

    => File type

    => Inode number

    => UID/GID

    => File access , modify (change) and creation time etc.

    I though that stat command never show creation time. Only access, modify and change. Am I wrong?

  • nixCraft August 13, 2007, 10:07 am

    No, you are not. UNIX and Linux never stores creation time. It was a typo.

    Appreciate your post!

  • RajaniKanth September 6, 2007, 6:47 pm

    Hi,

    I just want to find the list of users who are currently accessing/accessed a purticular file from linux server?

    Thanks
    RK

    • Nidhin Tomson August 30, 2011, 6:02 am

      fuser -muv will give you the details who / what process is holding a file. though it will not show file accessed details.

  • ur2g2b4got October 24, 2007, 5:20 pm

    is there a way to modify the property of the last time a file was accessed?

  • nixCraft October 24, 2007, 6:10 pm

    ur2g2b4got,

    use touch command to update date/time stamp.

  • Mohit June 19, 2009, 4:02 pm

    Is there any command on Linux that tells when the file or folder was created ?

    • nixCraft June 19, 2009, 4:25 pm

      No UNIX / Linux file system do not store creation date.

  • rajbhatia January 22, 2010, 5:40 pm

    No UNIX / Linux file system do not store creation date while windows do.
    why?
    if anybody knws plzz reply me on my email.

  • Alex September 8, 2010, 6:06 pm

    Thanks for the tip! Very useful

  • sonal January 21, 2011, 9:09 am

    how can i get the names of all the recently modified files in any directory?

  • Truong October 8, 2012, 3:10 pm

    Hi,

    On Linux, I want to output a summary list with these info on file datasets:

    Filename Size (in Bytes) Last Active Dates

    If I use “psz *.js” I will get:
    —Bytes—- —Mbytes— –Gbytes– Filename
    4096 0.00391 0.00000381 conv_10ktmigstack.js
    28672 0.02734 0.00002670 convdenoiseddatastk.js
    4096 0.00391 0.00000381 convdenoiseddataxy10ofb.js
    4096 0.00391 0.00000381 convdesigdenoiseddataxy10.js
    28672 0.02734 0.00002670 convdesigdenoisedstk.js

    If I use “stat *.js” I will get:
    [buitv@hololw54 conventional_t]> stat *.js
    File: `conv_10ktmigstack.js’
    Size: 4096 Blocks: 128 IO Block: 4096 directory
    Device: 32h/50d Inode: 3154237336 Links: 2
    Access: (0775/drwxrwxr-x) Uid: (115419/ buitv) Gid: ( 62/landmark)
    Access: 2012-10-08 09:31:35.847520000 -0500
    Modify: 2011-02-01 09:57:53.905232000 -0600
    Change: 2011-11-10 15:13:49.274708746 -0600

    How do I do this to combine the info (Filename, Size & Last Active Date) in 3 columns in one run?

    Thanks,

    • nixCraft October 8, 2012, 5:52 pm
      stat --format "%n %s %x" filename-here

      See stat man page for all format codes.

    • Alex October 22, 2012, 4:59 pm

      Check the find command it gets old the time stamps that you want

Leave a Comment