90

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?

muru
  • 207,228
nux
  • 39,152

8 Answers8

83

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 <X>' /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 .
nux
  • 39,152
67

@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' "${target}")
        fs=$(df  --output=source "${target}"  | tail -1)
        crtime=$(sudo debugfs -R 'stat <'"${inode}"'>' "${fs}" 2>/dev/null | 
        grep -oP 'crtime.*--\s*\K.*')
        printf "%s\t%s\n" "${target}" "${crtime}"
    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
terdon
  • 104,119
21

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).

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 <https://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.

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.

First, clone it:

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 <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include "statx.h"
#include <time.h>
#include <getopt.h>
#include <string.h>

// does not (yet) provide a wrapper for the statx() system call #include <sys/syscall.h>

/* 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, &quot;alfd&quot;)) != -1)
{
    switch(opt) {
        case 'a':
            flags |= AT_NO_AUTOMOUNT;
            break;
        case 'l':
            flags &amp;= ~AT_SYMLINK_NOFOLLOW;
            break;
        case 'f':
            flags &amp;= ~AT_STATX_SYNC_TYPE;
            flags |= AT_STATX_FORCE_SYNC;
            break;
        case 'd':
            flags &amp;= ~AT_STATX_SYNC_TYPE;
            flags |= AT_STATX_DONT_SYNC;
            break;
        default:
            exit(EXIT_SUCCESS);
            break;
    }
}

if (optind &gt;= argc) {
    exit(EXIT_FAILURE);
}

for (; optind &lt; argc; optind++) {
    memset(&amp;stxbuf, 0xbf, sizeof(stxbuf));
    ret = statx(dirfd, argv[optind], flags, mask, &amp;stxbuf);
    if( ret &lt; 0)
    {
        perror(&quot;statx&quot;);
        return EXIT_FAILURE;
    }
    printf(&quot;%lld.%u\n&quot;, *&amp;stxbuf.stx_btime.tv_sec, *&amp;stxbuf.stx_btime.tv_nsec);
}
return EXIT_SUCCESS;

}

Then:

$ 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.

muru
  • 207,228
7

TL;DR: Just run: sudo debugfs -R 'stat /path/to/your/file' /dev/<your fs>

(To figure out your fs, run df -T /path/to/your/file, most likely it's going to be /dev/sda1).

Long version:

We are going to run two commands:

  1. Find out the name of partition name for your file.

    df -T /path/to/your/file
    

    The output is going to look like this (partition name is first):

    Filesystem     Type 1K-blocks    Used Available Use% Mounted on
    /dev/<your fs> ext4   7251432 3481272   3509836  50% /
    
  2. Find out creation time for that file.

    sudo debugfs -R 'stat /path/to/your/file' /dev/<your fs>
    

    In the output, look for ctime.

wjandrea
  • 14,504
4

My OS (Ubuntu 20.04, which comes with Linux kernel 5.4.0-28 and GLIBC 2.31) only came with GNU coreutils 8.30, so I had to get it to work by compiling version 8.32 of GNU coreutils from source. It was a relatively pain-free procedure.

Afterwards, both ls and stat could work with birth time.

Output of ls -l --time=birth --time-style=full-iso --no-group: ls with birth time

Output of stat on an ext4 file system and a btrfs file system: stat with birth time

(Copied from my other answer on UNIX & Linux Stack Exchange)

Kevin Li
  • 319
0

You may need the file creation time in human readable but not standard format to use in bash shell script pipeline. For example, to prefixing the file name for appropriate sorting in the file manager. I modified @terdon solution for this purpose as following.

get_crtime() {
for target in &quot;${@}&quot;; do
    inode=$(stat -c '%i' &quot;${target}&quot;)
    fs=$(df  --output=source &quot;${target}&quot;  | tail -1)
    crtime=$(sudo debugfs -R 'stat &lt;'&quot;${inode}&quot;'&gt;' &quot;${fs}&quot; 2&gt;/dev/null |
    grep -oP 'crtime.*--\s*\K.*')
    printf $(date -d &quot;${crtime}&quot; | sed 's/://g'| sed 's/-//g')
done

}

  • date -d ... parses date time string;
  • sed removes special characters : and -;
  • no end of line added.

Which produses:

get_crtime .bashrc
20201023T181422
0
stat filenameuwantcreationdateof.xxx |grep Birth|cut -d' ' -f 3,4|cut -d. -f 1

is the one liner, were you are: running the stat commmand on the file in question and then piping that to |

a grep so you only see the line that says "Birth" and then piping that to |

the cut command with the space as the delimiter' and looking at the 3rd and 4h fields and then piping that to |

a second cut with the period as the delimiter and looking for the first field so the remainder of the seconds are cut off.

Wrapping this up as a shell script is left as an exercise to the user. 8)

2$ stat /tmp/shmem_fpga_reader_writer_tester.log
  File: /tmp/shmem_fpga_reader_writer_tester.log
  Size: 3000203         Blocks: 5872       IO Block: 4096   regular file
Device: 820h/2080d      Inode: 2364        Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/ jleslie)   Gid: ( 1000/ jleslie)
Access: 2024-11-25 20:02:52.385904749 -0500
Modify: 2024-11-25 20:11:23.009210519 -0500
Change: 2024-11-25 20:11:23.009210519 -0500
 Birth: 2024-11-21 13:58:38.142518388 -0500
2$ stat /tmp/shmem_fpga_reader_writer_tester.log |grep Birth|cut -d' ' -f 3,4|cut -d. -f 1
2024-11-21 13:58:38
2$
0

$ stat command
Code:

stat -c %w filename

-c --format=FORMAT
%w time of file birth, human-readable; - if unknown

Which produses:

2025-03-05 11:20:42.133634062 +0400