简体   繁体   English

如何使用bash脚本检查AIX中文件的最后修改日期?

[英]How to check last modified date of a file in AIX using bash script?

I want to check last modified date of a file in AIX using bash script. 我想使用bash脚本检查AIX中文件的最后修改日期。

I ve tried, 我试过了,

date -r file.txt "+%m-%d-%Y"
stat -c %y file.txt

but not working in AIX. 但不能在AIX中使用。

If you can't find an appropriately specific utility, you can always fall back on perl: 如果找不到合适的特定实用程序,则可以始终使用perl:

perl -MPOSIX=strftime -e 'printf "%s\n", strftime("%c", localtime((stat(shift @ARGV))[8]))' filename

or more legibly: 或更清晰:

perl -e '
    use POSIX qw(strftime);
    my $filename  = shift @ARGV;
    my @filedata  = stat $filename;
    my $mtime     = $filedata[8];
    my @timedata  = localtime $mtime;
    my $timestamp = strftime "%c", @timedata;
    print "$timestamp\n";
' filename

Choose a strftime format to your liking. 选择您喜欢的strftime格式。 If you just need the epoch time, print $mtime . 如果只需要纪元时间,请打印$mtime

How about: 怎么样:

istat file
Inode 263 on device 10/8        File
Protection: rw-r--r--
Owner: 0(root)          Group: 0(system)
Link count:   1         Length 14682 bytes

Last updated:   Tue Sep 15 10:50:15 PDT 2018
Last modified:  Tue Sep 15 10:50:15 PDT 2018
Last accessed:  Tue Nov  3 12:01:12 PST 2018

In case you haven't figured it out yet: 如果您还没有弄清楚,请执行以下操作:

date -d "$( istat file | awk '/modified/{gsub( /^[^:]+: +/,"",$0); print}' )"   "+%m-%d-%Y"

PS: "+%m-%d-%Y" is a silly date format, because it doesn't sort naturally. PS: "+%m-%d-%Y"是一种愚蠢的日期格式,因为它不是自然排序的。 "+%Y-%m-%d" would make far more sense. "+%Y-%m-%d"更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM