Skip to content

Instantly share code, notes, and snippets.

@zlx
Last active December 15, 2015 12:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zlx/c8b1cedc797028d6bc82 to your computer and use it in GitHub Desktop.
Save zlx/c8b1cedc797028d6bc82 to your computer and use it in GitHub Desktop.
Get file creation time
# 支持 MacOS, ext4 文件系统的 Linux
# 参考:http://moiseevigor.github.io/software/2015/01/30/get-file-creation-time-on-linux-with-ext4/
#
def creation_time(file_path)
case RUBY_PLATFORM
when /bsd/, /darwin/
return %x{mdls -raw -name kMDItemFSCreationDate #{file_path}}
when /linux/
inode = %x{ls -di "#{file_path}" | cut -d ' ' -f 1}.strip
fs = %x{df "#{file_path}" | tail -1 | awk '{print $1}'}.strip
out = %x{sudo debugfs -R 'stat <'"#{inode}"'>' "#{fs}" 2>/dev/null}.strip
return out.match(/crtime.*--\s*\K.*/)[0]
end
end
if $0 == __FILE__
puts creation_time(ARGV.first)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment