我确实是 Linux(Fedora-20) 的新手,我正在尝试学习基础知识 我有以下命令
echo "`stat -c "The file "%n" was modified on ""%y" *Des*`"
此命令返回此输出
The file Desktop was modified on 2014-11-01 18:23:29.410148517 +0000
我想将其格式化为:
The file Desktop was modified on 2014-11-01 at 18:23
我该怎么做?
最佳答案
您实际上无法使用 stat
做到这一点(除非您有我不知道的智能版本 stat
)。
带有日期
很可能,您的日期
足够聪明,可以处理-r
开关。
date -r Desktop +"The file Desktop was modified on %F at %R"
由于您的 glob,您需要一个循环来处理与 *Des*
匹配的所有文件(在 Bash 中):
shopt -s nullglob
for file in *Des*; do
date -r "$file" +"The file ${file//%/%%} was modified on %F at %R"
done
使用查找
很可能您的 find
有丰富的 -printf
选项:
find . -maxdepth 1 -name '*Des*' -printf 'The file %f was modified on %TY-%Tm-%Td at %TH:%TM\n'
我想使用stat
(因为您的 date
无法处理 -r
开关,您不想使用 find
或者只是因为您喜欢尽可能多地使用工具来给你的小妹妹留下深刻的印象)。那么,在这种情况下,最安全的做法是:
date -d "@$(stat -c '%Y' Desktop)" +"The file Desktop was modified on %F at %R"
以及您的 glob 要求(在 Bash 中):
shopt -s nullglob
for file in *Des*; do
date -d "@$(stat -c '%Y' -- "$file")" +"The file ${file//%/%%} was modified on %F at %R"
done
关于linux - 如何在 Linux Gnome 终端中格式化 stat 表达式的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26693387/