find creation date for file in linux

How to Get Creation Date for File & Directory in Linux

Sometimes you may need to determine creation date for file & directory in Linux. However, as per POSIX standard, a file is supposed to have only last date of access, modification and status change. Newer filesystems such as ext4, zfs, btrfs, JFS, and XFS store creation date also. This information is stored in file inodes. In this article, we will learn how to get creation date for file & directory in Linux. You can use these tools in almost every Linux distribution but they only work on newer filesystems mentioned earlier.


How to Get Creation Date for File & Directory in Linux

The simplest way to get creation date for file & directory is using stat command.

Here is an example where we create a file using echo & redirection operator, and use stat command to determine its creation date.

$ date; echo "Hello" > file
Fri Dec 17 11:26:25 IST 2021
$ cat file
Hello
$ stat file
  File: file
  Size: 6         	Blocks: 8          IO Block: 4096   regular file
...
Change: 2021-12-17 11:26:25.578441510 +0530
 Birth: 2021-12-07 11:26:25.0

The creation date for file is shown in Birth field. You can use stat to directly displayed required information such as creation date, using -format flag with ‘%w’ as its value. Here is the command to directly creation date for file using stat command.

$ stat -c '%w' file
2021-12-07 11:26:25.0

You can use –help option to find out all available options for stat function.

$ stat --help
...
The valid format sequences for files (without --file-system):
...
  %U   user name of owner
  %w   time of file birth, human-readable; - if unknown
  %W   time of file birth, seconds since Epoch; 0 if unknown
  %x   time of last access, human-readable
...

You can also use debugfs command to get creation date on Ext4 filesystems. But it does not provide a straightforward way to get this information since it is primarily built for debugging filesystem. Nevertheless, if you want to get creation date using debugfs command, first you need to get the inode number of that file using ls -i option.

$ ls -i ./file
5238705 ./file

We also need to find the filesystem of this file. You can do this using df command followed by the filepath.

$ df ./file
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda2             26056564   8381600  16328936  34% /

Next, you can get creation date using the following command.

$ debugfs -R 'stat <inode>' <filesystem>

You will see following kind of output.

$ sudo debugfs -R 'stat <5238705>' /dev/sda2
debugfs 1.46.4 (18-Aug-2021)
Inode: 5238705   Type: regular    Mode:  0644   Flags: 0x80000
Generation: 2975709199    Version: 0x00000000:00000001
...
crtime: 0x61bc2b65:71f8e150 -- Fri Dec 17 16:17:09 2021
...

The field crtime gives you the creation date of file.

In this article, we have learnt how to get creation date of file in Linux. Similarly, you can get creation date for directories, and folders by simply replacing file paths in all above commands with paths to directory.

Also read:

How to Delete Iptables Rules
How to Use Rsync With SSH Key
How to Run Python Scripts in Sequence
How to Download Gmail Attachment Using Shell Script
How to Do Google Search From Terminal

Leave a Reply

Your email address will not be published. Required fields are marked *