我有一个名为A的目录,我想知道里面一个文件/目录的修改日期.这是我的代码
#!/bin/bash
find A | sort -d
var="$(head -2 | tail -1)"
echo "$var"
date -r $var '+%S'
Run Code Online (Sandbox Code Playgroud)
这基本上就是我想要我的代码的方式,当我这样做时,它只显示我的目录A中的内容(查找命令),回声不起作用,日期也没有.我有一条消息说:date +%s没有这种类型的文件或目录.
我已经看到了一些关于将变量用作文件的问题,但在我的情况下,我没有看到任何可能导致问题的问题(比如使用斜杠).所以我想知道是否有问题.
find A | sort -d 将排序的文件列表打印到标准输出.
head -2 | tail -1让head等待从标准输入数据.
没有任何来自标准输入,我认为脚本应该永远挂起(?)
var可能是一个空字符串.所以echo打印一个空字符串(没有)和一个新行.
无论存储什么var,都无法在磁盘上找到文件名.这就是date生成此错误的原因.
尝试一下:
#!/bin/bash --
var="$(find A | sort -d | head -2 | tail -1)"
printf "%s " "$var"
date -r "$var" '+%s'
Run Code Online (Sandbox Code Playgroud)
find用-printf和打印文件的最后修改时间%T.
尝试一下:
find A -printf "%p %T@\n" | sort -d | awk 'NR==2{print ; quit}'
Run Code Online (Sandbox Code Playgroud)
-printf "%p %T@\n":打印文件名+ (space char) + last modification time of the file as seconds since Jan. 1, 1970, 00:00 GMT, with fractional part.
awk 'NR==2{print ; quit} 仅打印第二行.
| 归档时间: |
|
| 查看次数: |
97 次 |
| 最近记录: |