Ano*_*eek 12 unix file last-modified
我正在编写一个shell脚本,我必须在其中找到文件的最后修改日期.
Stat 命令在我的环境中不可用.
所以我使用'ls'如下获得所需的结果.
ls -l filename | awk '{print $6 $7 $8}'
Run Code Online (Sandbox Code Playgroud)
但我在很多论坛上都读过,解析ls通常被认为是不好的做法.虽然它(可能)大部分时间都可以正常工作,但并不能保证每次都能正常工作.
有没有其他方法可以在shell脚本中获取文件修改日期.
Lev*_*von 20
使用find命令怎么样?
例如,
$ find filenname -maxdepth 0 -printf "%TY-%Tm-%Td %TH:%TM\n"
Run Code Online (Sandbox Code Playgroud)
这个特殊的格式字符串给出如下输出: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
Run Code Online (Sandbox Code Playgroud)
当然,您可以使用任意数量的语言(如Python或Perl等)编写脚本来获取相同的信息,但是要求" unix命令 "听起来好像您正在寻找"内置"shell命令.
编辑:
你也可以从命令行中输入Python,如下所示:
$ python -c "import os,time; print time.ctime(os.path.getmtime('uname.txt'))"
Run Code Online (Sandbox Code Playgroud)
或者如果与其他shell命令结合使用:
$ echo 'uname.txt' | xargs python -c "import os,time,sys; print time.ctime(os.path.getmtime(sys.argv[1]))"
Run Code Online (Sandbox Code Playgroud)
两者都回归: Wed Jun 13 00:05:29 2012
根据您的操作系统,您可以使用
date -r FILENAME
Run Code Online (Sandbox Code Playgroud)
唯一似乎无法使用的 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.
Run Code Online (Sandbox Code Playgroud)
代替
-r, --reference=FILE
display the last modification time of FILE
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68171 次 |
| 最近记录: |