Linux show all file times

last time file opened

Is it possible to get the time when file was opened last time and sort all files in a directory by those times?

5 Answers 5

This depends on exactly what you mean by «opened», but in general, yes. There are three timestamps normally recorded:

  • mtime — updated when the file contents change. This is the «default» file time in most cases.
  • ctime — updated when the file or its metadata (owner, permissions) change
  • atime — updated when the file is read

So, generally, what you want to see is the atime of a file. You can get that with stat or with ls . You can use ls -lu to do this, although I prefer to use ls -l —time=atime (which should be supported in almost all modern Linux distributions) because I don’t use it often, and when I do I can remember it better. And to sort by time, add the -t flag to ls. So there you go.

There is a big caveat, though. Updating the atime every time a file is read causes a lot of usually-unnecessary IO, slowing everything down. So, most Linux distributions now default to the noatime filesystem mount option, which basically kills atimes, or else relatime , which only updates atimes once a limit has passed (normally once per day) or if the file was actually modified since the previous read. You can find if these options are active by running the mount command.

Also, note that access times are by inode, not by filename, so if you have hardlinks, reading from one will update all names that refer to the same file.

And, be aware that c is not «creation»; creation isn’t tracked by Unix/Linux filesystems, which seems strange but actually makes sense because the filesystem has no way of knowing if it is the original — maybe the file was created forty years ago and copied here. And, in fact, many file editors work by making copies over the original. If you need that information, it’s best to use a version control system like git .

A little update, a decade later: some filesystems, like btrfs, include a fourth timestamp:

. at least, in the statx(2) system call man page. It seems to be pretty sparsely documented overall. It really suffers from the same problem that made it get left out in the first place: what exactly does it mean? Consider:

$ echo > foo; stat foo|grep Birth Birth: 2022-04-30 02:38:19.084919920 -0400 $ cp foo foo.tmp; mv foo.tmp foo; stat foo|grep Birth Birth: 2022-04-30 02:39:00.950269045 -0400 

Here, I’ve created a file, and then copied it to a temporary file, and then moved that back. This changes the «birth» time. Is that right? Maybe! I’m not sure that’s really useful, at least not for humans. But anyway, for completeness: that’s a thing you might have available on a modern system.

Источник

How to get file creation date/time in Bash/Debian?

I’m using Bash on Debian GNU/Linux 6.0. Is it possible to get the file creation date/time? Not the modification date/time. ls -lh a.txt and stat -c %y a.txt both only give the modification time.

13 Answers 13

Unfortunately your quest won’t be possible in general, as there are only 3 distinct time values stored for each of your files as defined by the POSIX standard (see Base Definitions section 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 .

EDIT: As mentioned in the comments below, depending on the filesystem used metadata may contain file creation date. Note however storage of information like that is non standard. Depending on it may lead to portability problems moving to another filesystem, in case the one actually used somehow stores it anyways.

Читайте также:  Linux информация о подключении

Such sad news. It’d be so useful right now to determine easily whether a file has been deleted and recreated or has been there all along.

Do you know how that works on a Mac? The Finder shows three timestamps as ‘Created’, ‘Modified’, ‘Last openend’.

ls -i file #output is for me 68551981 debugfs -R 'stat ' /dev/sda3 # /dev/sda3 is the disk on which the file exists #results - crtime value [root@loft9156 ~]# debugfs -R 'stat ' /dev/sda3 debugfs 1.41.12 (17-May-2010) Inode: 68551981 Type: regular Mode: 0644 Flags: 0x80000 Generation: 769802755 Version: 0x00000000:00000001 User: 0 Group: 0 Size: 38973440 File ACL: 0 Directory ACL: 0 Links: 1 Blockcount: 76128 Fragment: Address: 0 Number: 0 Size: 0 ctime: 0x526931d7:1697cce0 -- Thu Oct 24 16:42:31 2013 atime: 0x52691f4d:7694eda4 -- Thu Oct 24 15:23:25 2013 mtime: 0x526931d7:1697cce0 -- Thu Oct 24 16:42:31 2013 **crtime: 0x52691f4d:7694eda4 -- Thu Oct 24 15:23:25 2013** Size of extra inode fields: 28 EXTENTS: (0-511): 352633728-352634239, (512-1023): 352634368-352634879, (1024-2047): 288392192-288393215, (2048-4095): 355803136-355805183, (4096-6143): 357941248-357943295, (6144 -9514): 357961728-357965098 

Источник

How do I find the creation time of a file?

I need to find the creation time of a file, when I read some articles about this issue, all mentioned that there is no solution (like Site1, Site2). When I tried the stat command, it states Birth: — . So how can I find the creation time of a file?

Keep in mind that the ‘creation time’ of a file is not guaranteed to be accurate. There are many ways to ‘fudge’ the creation dates on a file.

6 Answers 6

There is a way to know the creation date of a directory , just follow these steps :

  1. Know the inode of the directory by ls -i command (lets say for example its X)
  2. Know on which partition your directory is saved by df -T /path command ( lets say its on /dev/sda1 )
  3. Now use this command : sudo debugfs -R ‘stat ‘ /dev/sda1

You will see in the output :

crtime: 0x4e81cacc:966104fc -- mon Sep 27 14:38:28 2013 

crtime is the creation date of your file .

What I tested :

  1. Created a directory at specific time .
  2. Accessed it .
  3. Modified it by creating a file .
  4. I tried the command and it gave an exact time .
  5. Then i modify it , and test again , the crtime remained the same , but modify and access time changed .

I post this , because i like to discuss so i can understand better , i am wonder why people say that Linux doesn’t support this feature

Because Linux itself does not. The ext4 filesystem does have this information but the kernel does not provide an API to access it. Apparently, debugfs extracts it directly from the filesystem so it does not need to use the kernel’s API. See here.

Читайте также:  Linux partition start sector

@Nux found a great solution for this which you should all upvote. I decided to write a little function that can be used to run everything directly. Just add this to your ~/.bashrc .

get_crtime() < for target in "$"; do inode=$(stat -c '%i' "$") fs=$(df --output=source "$" | tail -1) crtime=$(sudo debugfs -R 'stat "'>' "$" 2>/dev/null | grep -oP 'crtime.*--\s*\K.*') printf "%s\t%s\n" "$" "$" done > 

Now, you can run get_crtime to print the creation dates of as many files or directories as you like:

$ get_crtime foo foo/file foo Wed May 21 17:11:08 2014 foo/file Wed May 21 17:11:27 2014 

Note that the creation date is not the creation date of the original file if the file is a copy (like it is with the modification date). Once a file is copied, the modification date is from the original, but the creation date is from the copy. (theres is some misunderstanding in this question: askubuntu.com/questions/529885/…)

@JacobVlijm well, yes, of course. Isn’t that obvious? How could it be otherwise? A copy is a new file that just happens to have the same contents as another. The modification time also changes for a copy by the way. It is set to the moment the copy was created unless you explicitly choose for that not to happen using cp -p or similar.

Absolutely, but it at the same time, it wouldn’t be that unlogical if, like the mod. date, somewhere in the file the date would be stored when it originated. I must admit I didn’t know that was not the case until I answered the linked question.

Just tried by the way, I just copied files in nautilus, modification date stays as it is (was), m. date is earlier than creation date.

@demongolem yes, the CentOS version of df doesn’t seem to support the —output option. In that case, you can replace that line with fs=$(df foo | awk ‘END’ and the function will work as well. All I’m showing in this answer is a way to wrap the command from the accepted answer in a way that can be run directly for file/directory targets.

The inability of stat to show the creation time is due to limitation of the stat(2) system call, whose return struct doesn’t include a field for the creation time. Starting with Linux 4.11 (i.e., 17.10 and newer*), however, the new statx(2) system call is available, which does include a creation time in its return struct.

* And possibly on older LTS releases using the hardware enablement stack (HWE) kernels. Check uname -r to see if you are using a kernel at least at 4.11 to confirm.

Unfortunately, it’s not easy to call system calls directly in a C program. Typically glibc provides a wrapper that makes the job easy, but glibc only added a wrapper for statx(2) in August 2018 (version 2.28, available in 18.10). The stat command itself gained support for statx(2) only in GNU coreutils 8.31 (released in March 2019), however, even Ubuntu 20.04 only has coreutils 8.30.

But I don’t think this will be backported to LTS releases even if they do get, or are already on, newer kernels or glibcs. So, I don’t expect stat on any current LTS release (16.04, 18.04 or 20.04) to ever print the creation time without manual intervention.

On 18.10 and newer, you can directly use the statx function as described in man 2 statx (note that the 18.10 manpage is incorrect in stating that glibc hasn’t added the wrapper yet).

Читайте также:  Easy windows to linux

And in Ubuntu 20.10, you will be able to use stat directly:

# stat --version stat (GNU coreutils) 8.32 Copyright (C) 2020 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 / File: / Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 88h/136d Inode: 57279593 Links: 1 Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2020-08-18 06:57:46.912243164 +0000 Modify: 2020-08-18 06:57:06.768492334 +0000 Change: 2020-08-18 06:57:59.136165661 +0000 Birth: 2020-08-18 06:57:06.768492334 +0000 

For older systems, luckily, @whotwagner wrote a sample C program that shows how to use the statx(2) system call on x86 and x86-64 systems. Its output is the same format as stat ‘s default, without any formatting options, but it’s simple to modify it to print just the birth time.

git clone https://github.com/whotwagner/statx-fun 

You can compile the statx.c code, or, if you just want the birth time, create a birth.c in the cloned directory with the following code (which is a minimal version of statx.c printing just the creation timestamp including nanosecond precision):

#define _GNU_SOURCE #define _ATFILE_SOURCE #include #include #include #include #include #include "statx.h" #include #include #include // does not (yet) provide a wrapper for the statx() system call #include /* this code works ony with x86 and x86_64 */ #if __x86_64__ #define __NR_statx 332 #else #define __NR_statx 383 #endif #define statx(a,b,c,d,e) syscall(__NR_statx,(a),(b),(c),(d),(e)) int main(int argc, char *argv[]) < int dirfd = AT_FDCWD; int flags = AT_SYMLINK_NOFOLLOW; unsigned int mask = STATX_ALL; struct statx stxbuf; long ret = 0; int opt = 0; while(( opt = getopt(argc, argv, "alfd")) != -1) < switch(opt) < case 'a': flags |= AT_NO_AUTOMOUNT; break; case 'l': flags &= ~AT_SYMLINK_NOFOLLOW; break; case 'f': flags &= ~AT_STATX_SYNC_TYPE; flags |= AT_STATX_FORCE_SYNC; break; case 'd': flags &= ~AT_STATX_SYNC_TYPE; flags |= AT_STATX_DONT_SYNC; break; default: exit(EXIT_SUCCESS); break; >> if (optind >= argc) < exit(EXIT_FAILURE); >for (; optind < argc; optind++) < memset(&stxbuf, 0xbf, sizeof(stxbuf)); ret = statx(dirfd, argv[optind], flags, mask, &stxbuf); if( ret < 0) < perror("statx"); return EXIT_FAILURE; >printf("%lld.%u\n", *&stxbuf.stx_btime.tv_sec, *&stxbuf.stx_btime.tv_nsec); > return EXIT_SUCCESS; > 
$ make birth $ ./birth ./birth.c 1511793291.254337149 $ ./birth ./birth.c | xargs -I <> date -d @<> Mon Nov 27 14:34:51 UTC 2017 

In theory this should make the creation time more accessible:

  • more filesystems should be supported than just the ext* ones ( debugfs is a tool for ext2/3/4 filesystems, and unusable on others)
  • you don’t need root to use this (except for installing some required packages, like make and linux-libc-dev ).

Testing out an xfs system, for example:

$ truncate -s 1G temp; mkfs -t xfs temp; mkdir foo; sudo mount temp foo; sudo chown $USER foo $ touch foo/bar $ # some time later $ echo > foo/bar $ chmod og-w foo/bar $ ./birth foo/bar | xargs -I <> date -d @<> Mon Nov 27 14:43:21 UTC 2017 $ stat foo/bar File: foo/bar Size: 1 Blocks: 8 IO Block: 4096 regular file Device: 700h/1792d Inode: 99 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ muru) Gid: ( 1000/ muru) Access: 2017-11-27 14:43:32.845579010 +0000 Modify: 2017-11-27 14:44:38.809696644 +0000 Change: 2017-11-27 14:44:45.536112317 +0000 Birth: - 

However, this didn’t work for NTFS and exfat. I guess the FUSE filesystems for those didn’t include the creation time.

Источник

Оцените статью
Adblock
detector