2

I need to creation date and time of file in linux.

I have tried below two command but did not get any output.

  1. sudo debugfs -R 'stat <inode_no of file>' "mount point name".

    This gives me the following error:

    Bad magic number in super-block while opening the file system
    
  2. stat -c '%w' file_name

This gives me - as output.

So someone please suggest appropriate command to get create date and time of file in linux.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Wakar Khan
  • 21
  • 1
  • 2
  • 1
    Recording the creation time is not supported on all file systems. Please specify how the partition you are working on is formatted. If it is ext4, it is unfortunately impossible. – AdminBee Nov 11 '21 at 09:27
  • Some filesystems carry birth time (btime - creation date). Most do not, and you have to revert to modification time (mtime). If you're getting - from stat -c %w then it's probably your filesystem is one of those without birth time – Chris Davies Nov 11 '21 at 09:32
  • What are you giving as "mount point name"? If you have /dev/sda1 mounted at /, are you giving / or /dev/sda1 as the name? And what file system are you using? Also see Birth is empty on ext4. – terdon Nov 11 '21 at 10:29
  • I am giving "mount point name" as /dev/sda1. And I am using xfs file system. So xfs file system is not supported creation or birth time of file in linux? – Wakar Khan Nov 12 '21 at 09:18
  • Wikipedia claims XFS supports crtime from Version 5. However, I searched the 369-page online RHEL Admin Guide for the terms crtime and stamp and creation and got nothing relevant. Wiki "comparison_of_file_systems" says XFS supports "Creation time stored since June 2015, xfsprogs version 3.2.3". – Paul_Pedant Nov 15 '21 at 09:15
  • I have checked and found XFS file system version is 4.5 in my system. So XFS file system 4.5 version will not support creation date and time? – Wakar Khan Nov 16 '21 at 10:50
  • I have checked this in XFS version 5 file as well but getting same output. – Wakar Khan Nov 25 '21 at 11:24

1 Answers1

4

From the man page for stat:

%w     time of file birth, human-readable; - if unknown

Most file systems do not hold the "creation date" of files. The concept just does not exist. It is also fairly meaningless: you can create a file, and then change every single byte in it at any time without altering the creation date.

This is always available, and usually much more relevant.

%y     time of last data modification, human-readable
Paul_Pedant
  • 8,679
  • I want modification date and time of file as well which I got and If we change every single byte in it at any time without altering the creation date then I will not get modification date and time of file. So please provide suitable solution for this. – Wakar Khan Nov 15 '21 at 07:19
  • Changing any of the data (from a single bit to every byte) changes the modification timestamp. In fact, even writing zero-length data to a zero-length file changes the mod date (although appending zero bytes to a file does not). There is no creation date, and the system cannot modify something that does not exist. – Paul_Pedant Nov 15 '21 at 08:53
  • I want modification and creation date and time of file but I I got modification date and time but not getting creation date and time of file. If we change every single byte in it at any time without altering the creation date then I will get the creation date and time but will not get modification date and time of file. So please provide proper solution to get the creation date and time of file. – Wakar Khan Nov 16 '21 at 10:57
  • @WakarKhan I reiterate: There is no creation date, and the system cannot modify something that does not exist. If your file system reports the crtime as -, then get a different file system that does support crtime. But most likely, your dump/restore system will not retain the crtime either, so any file you recover will still not have the value. – Paul_Pedant Nov 17 '21 at 22:34
  • Previously have checked in XFS file system and now have checked in ext4 file but system but getting same result. – Wakar Khan Nov 25 '21 at 11:47