w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How to get file creation time in unix with perl

You almost have it.

You just need to pass the return value from localtime instead of the filename to strftime:

perl -MPOSIX -e 'printf "%s %s
", $_, strftime("%d %b %Y %H:%M", localtime((lstat)[9])) for @ARGV'
file.txt

Outputs:

file.txt 10 Oct 2014 15:38

And even though it takes more code, I'd lean toward using Time::Piece and File::stat to make the code more modern and readable:

perl -MTime::Piece -MFile::stat -e '
    printf "%s %s
",  $_, localtime( stat($_)->mtime )->strftime("%d %b %Y %H:%M") for
@ARGV
  ' file.txt




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.