Over a million developers have joined DZone.

Ext4 Crtime (Creation Time) On Linux

·
This is a very quick-and-dirty implementation of a simple script which allows you to access ext4's crtime on Linux ...

Might be useful, especially if your GNU coreuitls does not have this functionality available in its version of stat yet ...


!#/usr/bin/env ruby

DEBUGFS_BINARY='/sbin/debugfs'  # Change this when needed ...

STDERR.reopen(STDOUT)

STDOUT.sync = true
STDERR.sync = true

def die(message); puts message; exit(1); end

unless File.exists?(DEBUGFS_BINARY)
  die "#{$0}: #{DEBUGFS_BINARY} not found. Aborting ..."
end

if ARGV.length < 1 or ARGV.first == '-'
  die <<-EOS

Usage:

  #{$0} 

  Specify  for which you wish to display its crtime (Creation Time).

  Please note that this is for ext4 file system only ...

  EOS
end

file = ARGV.shift

unless File.exists?(file)
  die "#{$0}: file `#{file}' does not exists ..."
end

unless (Process.uid or Process.euid).zero?
  die "#{$0}: you have to be super-user to run this script ..."
end

device_lookup = Dir['/dev/*'].inject({}) { |h, v|
  h.update(File.stat(v).rdev => v)
}

stat = File.stat(file)

this_process = \
  "-R \"stat <#{stat.ino}>\" #{device_lookup[stat.dev].first} 2>/dev/null"

this_process = "#{DEBUGFS_BINARY} #{this_process}"

IO.popen(this_process).each do |p|
  (puts "#{file}: #{p.split('--').last.strip}"; break) if p.match(/^crtime/)
end

exit(0)


Please note that due to its dependence on the /dev file system to perform device lookup it may not be portable enough ...
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}