54
21
Is there a command in Linux to check all timestamps of a file?
I'm trying to see the last modified, created, and touched dates on the file.
54
21
Is there a command in Linux to check all timestamps of a file?
I'm trying to see the last modified, created, and touched dates on the file.
75
The command is called stat
.
$ stat test
234881026 41570368 -rw-r--r-- 1 werner staff 0 0 "Feb 7 16:03:06 2012" "Feb 7 16:03:06 2012" "Feb 7 16:03:06 2012" "Feb 7 16:03:06 2012" 4096 0 0 test
If you want to adjust the format, refer to the man pages, since the output is OS-specific and varies under Linux/Unix.
Generally, you can get the times through a normal directory listing as well:
ls -l
outputs last time the file content was modified, the mtime
ls -lc
outputs last time of file status modification, the ctime
(What's the difference?)ls -lu
outputs last access time, the atime
(although the usefulness of this concept is subject to discussion)And of course, ctime
does not record when a file was "created".
The POSIX specification defines only three timestamps, but some Linux filesystems store Birth Time/Creation Time. How to find creation date of file? On such a supported configuration, one could use
stat --printf '%n\nmtime: %y\nctime: %z\natime: %x\ncrtime:%w\n'
stat
is really detailed. But ls
only needs one line. It would be good if it could also display seconds. However, when creating lists of files, the former one is perfectly suitable. – neverMind9 – 2018-11-07T20:28:35.903
ls -l was the quick solution for me – Andrew – 2018-11-23T15:08:06.867
I've noticed that the result of ls -l
can show a different date format when there is BusyBox installed (on Android). I think that without it , it's like "2019-07-26 14:41" , and with it, it's like "May 6 21:27" . How come the year is missing ? Is there a way to force it using the format of without it? – android developer – 2019-07-26T12:21:04.393
@androiddeveloper Like I said, the answer depends on the OS. I think you should open a new question. If you are talking about Android specifically, perhaps [SO] or [Android.SE] would be more fitting. – slhck – 2019-07-26T12:37:59.477
@slhck Well it's the same OS, just with BusyBox installed. I asked if it's possible (meaning : is there a command to use) to get the format that will be shown. – android developer – 2019-07-27T06:32:34.450
21
There are only THREE distinct times values stored for each of your files, as defined by the POSIX Standard : http://pubs.opengroup.org/onlinepubs/9699919799/ (see Base Definitions section -> 4. General Concepts -> 4.8 File Times Update)
Each file has three distinct associated timestamps: the time of last data access, the time of last data modification, and the time the file status last changed. These values are returned in the file characteristics structure struct stat, as described in <sys/stat.h>.
And from <sys/stat.h> :
atime is for Last data access timestamp.
mtime is for Last data modification timestamp.
ctime is for Last file status change timestamp.
Following examples show the difference among the atime, mtime and ctime, these examples are in GNU/Linux BASH. You can use stat -x
in Mac OS X or other BSD Dist. to see the similar output format.
$ stat --version
stat (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Michael Meskes.
$
$ touch test
$ stat test
File: `test'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 811h/2065d Inode: 98828525 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 514/ rank) Gid: ( 514/ rank)
Access: 2014-03-16 10:58:28.609223953 +0800
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 10:58:28.609223953 +0800
When the file just be created, three timestamps are the same.
First, let's access the file's data by reading it (less
or vim
), printing it out (cat
) or copy it to another file (cp
).
$ cat test #Nothing will be printed out, since the file is empty
$ stat test
File: `test'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 811h/2065d Inode: 98828525 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 514/ rank) Gid: ( 514/ rank)
Access: 2014-03-16 10:59:13.182301069 +0800 <-- atime Changed!
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 10:58:28.609223953 +0800
Now let's change the file status, by changing the permission (chmod
) or renaming it (mv
)
$ chmod u+x test
$ stat stet
File: `test'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 811h/2065d Inode: 98828525 Links: 1
Access: (0764/-rwxrw-r--) Uid: ( 514/ rank) Gid: ( 514/ rank)
Access: 2014-03-16 10:59:13.182301069 +0800
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 11:04:10.178285430 +0800 <-- ctime Changed!
$
$ mv test testing
$ stat testing
File: `testing'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 811h/2065d Inode: 98828525 Links: 1
Access: (0764/-rwxrw-r--) Uid: ( 514/ rank) Gid: ( 514/ rank)
Access: 2014-03-16 10:59:13.182301069 +0800
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 11:06:33.342207679 +0800 <-- ctime Changed again!
Note that until now, the contents (data) of the file is still the same as when it created.
Finally, let's modify the contents of the file by editing the file.
$ echo 'Modify the DATA of the file' > testing
$ echo 'Modify the DATA of the file also change the file status' > testing
$ stat testing
File: `testing'
Size: 56 Blocks: 8 IO Block: 4096 regular file
Device: 811h/2065d Inode: 98828525 Links: 1
Access: (0764/-rwxrw-r--) Uid: ( 514/ rank) Gid: ( 514/ rank)
Access: 2014-03-16 10:59:13.182301069 +0800
Modify: 2014-03-16 11:09:48.247345148 +0800 <-- mtime Changed!
Change: 2014-03-16 11:09:48.247345148 +0800 <-- ctime also Changed!
Also note that the newer version of stat
(e.g. stat --version 8.13
in Ubuntu 12.04) has 4th timestamp information - the Birth Time (file creation time). Although it may not show the correct time for now:
$ stat --version
stat (GNU coreutils) 8.13
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Michael Meskes.
$
$ stat birth_time
File: `birth_time'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 805h/2053d Inode: 4073946 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ bingyao) Gid: ( 1000/ bingyao)
Access: 2014-03-16 10:46:48.838718970 +0800
Modify: 2014-03-16 10:46:48.838718970 +0800
Change: 2014-03-16 10:46:48.838718970 +0800
Birth: -
What is the meaning of 10:46:48.838718970? HH:MM:SS. nanoseconds? – Dzung Nguyen – 2016-09-13T17:35:36.317
2Yes. "Since kernel 2.5.48, the stat structure supports nanosecond resolution for the three file timestamp fields...." – Tom Lord – 2016-10-04T09:29:19.133
Creation time can be figured out with root access and debugfs if needed: http://moiseevigor.github.io/software/2015/01/30/get-file-creation-time-on-linux-with-ext4/
– Mikko Rantalainen – 2018-03-14T14:36:53.4032Birth time… gladly. Because last modified and last change is nearly for the same purpose but still distinct. It is good if *all four* exist. – neverMind9 – 2018-11-07T20:29:35.907
1
@FatalError, Birth time can be displayed with Linux
– Franklin Piat – 2015-03-27T21:23:23.053stat
command, see How to find creation date of file? and What file systems on Linux store the creation time?.4Just to point out, Linux files don't have birth dates. Thus, it's not possible to determine the date a file was created. – FatalError – 2012-02-07T15:12:24.733
noticed that =(. Thanks for pointing it out to me. – Mechaflash – 2012-02-07T15:25:48.570
4@FatalError: Various filesystems already support birth/creation timestamps; the real trouble is in accessing such extra information. (One can't just extend
struct stat
without breaking things, unfortunately...) You can try outdebugfs -R "stat <1234>" /dev/sdXY
for ext4, replacing1234
with an ino. – user1686 – 2012-02-07T17:03:03.123@grawity: Neat! I always wondered why no fs had it... but I guess they do, but like you said, can't just go breaking the ABI for existing binaries. Thanks for the tip :). – FatalError – 2012-02-07T17:45:45.870
@FatalError: So why does
stat <file>
show aBirth
timestamp that is the time when<file>
was created? Is it just a coincidence? Is it the birthdate of the inode? – davemyron – 2013-12-31T23:32:49.440