About nixCraft

Topics

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

Posted by Vivek Gite [Last updated: August 13, 2007]

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

Tell us how we're doing: Please answer a few questions about your experience to help us improve nixCraft.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. frank Says:

    stat command example

    $ ls /etc/passwd

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

  2. vivek Says:

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

  3. pradeep Says:

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

    Thanks
    pradeep

  4. Gonçalo Says:


    => 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?

  5. vivek Says:

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

    Appreciate your post!

  6. RajaniKanth Says:

    Hi,

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

    Thanks
    RK

  7. ur2g2b4got Says:

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

  8. vivek Says:

    ur2g2b4got,

    use touch command to update date/time stamp.

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.