Last active
December 15, 2015 12:16
-
-
Save zlx/c8b1cedc797028d6bc82 to your computer and use it in GitHub Desktop.
Get file creation time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 支持 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