Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File Unix命令(除';stat';和';ls';)用于获取文件修改日期而不进行分析_File_Unix_Last Modified - Fatal编程技术网

File Unix命令(除';stat';和';ls';)用于获取文件修改日期而不进行分析

File Unix命令(除';stat';和';ls';)用于获取文件修改日期而不进行分析,file,unix,last-modified,File,Unix,Last Modified,我正在编写一个shell脚本,其中必须找到文件的最后修改日期 Stat命令在我的环境中不可用 因此,我使用下面的'ls'来获得所需的结果 ls -l filename | awk '{print $6 $7 $8}' 但我在很多论坛上都读到了这一点。虽然它(可能)在大多数情况下都能正常工作,但并不保证每次都能正常工作 是否有其他方法可以在shell脚本中获取文件修改日期。使用find命令如何 e、 g $ find filenname -maxdepth 0 -printf "%TY-%Tm

我正在编写一个shell脚本,其中必须找到文件的最后修改日期

Stat
命令在我的环境中不可用

因此,我使用下面的
'ls'
来获得所需的结果

ls -l filename | awk '{print $6 $7 $8}'
但我在很多论坛上都读到了这一点。虽然它(可能)在大多数情况下都能正常工作,但并不保证每次都能正常工作


是否有其他方法可以在shell脚本中获取文件修改日期。

使用
find
命令如何

e、 g

 $ find filenname -maxdepth 0 -printf "%TY-%Tm-%Td %TH:%TM\n"
此特定格式字符串提供如下输出:
2012-06-13 00:05

显示了可与
printf
一起使用的格式化指令,以根据需要/需要定制输出。第
-printf format
节包含所有详细信息

ls
输出与
find
进行比较:

$ ls -l uname.txt | awk '{print  $6 , "", $7}'
2012-06-13  00:05

$ find uname.txt -maxdepth 0 -printf "%TY-%Tm-%Td %TH:%TM\n"
2012-06-13 00:05
当然,您可以用Python或Perl等任何语言编写脚本,以获得相同的信息,但是要求使用“unix命令”听起来好像是在寻找“内置”shell命令

编辑

您也可以像这样从命令行inovke Python:

$ python -c "import os,time; print time.ctime(os.path.getmtime('uname.txt'))"
或者如果与其他shell命令结合使用:

$ echo 'uname.txt' | xargs python -c "import os,time,sys; print time.ctime(os.path.getmtime(sys.argv[1]))"
两者都返回:
Wed Jun 13 00:05:29 2012

您有perl吗

如果是这样,您可以使用其内置的
stat
函数获取命名文件的mtime(和其他信息)

下面是一个小脚本,它获取文件列表并打印每个文件的修改时间:

#!/usr/bin/perl

use strict;
use warnings;

foreach my $file (@ARGV) {
    my @stat = stat $file;
    if (@stat) {
        print scalar localtime $stat[9], " $file\n";
    }
    else {
        warn "$file: $!\n";
    }
}
样本输出:

$ ./mtime.pl ./mtime.pl nosuchfile
Tue Jun 26 14:58:17 2012 ./mtime.pl
nosuchfile: No such file or directory
File::stat
模块使用更为用户友好的版本覆盖
stat
调用:

#!/usr/bin/perl

use strict;
use warnings;

use File::stat;

foreach my $file (@ARGV) {
    my $stat = stat $file;
    if ($stat) {
        print scalar localtime $stat->mtime, " $file\n";
    }
    else {
        warn "$file: $!\n";
    }
}

根据您的操作系统,您可以使用

date -r FILENAME
唯一的unix版本似乎不适用于Mac OS,根据man文件,-r选项是:

 -r seconds
         Print the date and time represented by seconds, where seconds is
         the number of seconds since the Epoch (00:00:00 UTC, January 1,
         1970; see time(3)), and can be specified in decimal, octal, or
         hex.
而不是

   -r, --reference=FILE
          display the last modification time of FILE

如果
stat
不可用,则很有可能
find-printf
选项也不可用
stat
不是标准的Unix命令,而
-printf
是Gnu扩展。@jlliagre好吧,希望OP能告诉我们这是不是一个可行的解决方案。如果没有,我们可以考虑其他选项。OSX(10.9):我们有stat,但没有printf。
date-r
在OSX上不可用:(
#!/bin/bash 
FILE=./somefile.txt

modified_at=`perl -e '$x = (stat("'$FILE'"))[9]; print "$x\n";'`

not_changed_since=`perl -e '$x = time - (stat("'$FILE'"))[9]; print "$x\n";'`

echo "Modified at $modified_at"
echo "Not changed since $not_changed_since seconds"