linux - 如何在 Linux Gnome 终端中格式化 stat 表达式的输出?

标签 linux bash terminal fedora stat

我确实是 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/

相关文章:

python - apt-get purge 做了什么?

python - 如何在 bash 脚本中导入 python 文件? (在我的 bash 脚本中使用 python 值)

python - Google 时区 API 限制

bash - 以秒为单位获取 UTC 时间

python - 在谷歌云上训练神经网络时出现 "Unable to get Filesystem for path"错误

linux - 系统调用劫持 x64 - 无法在 ffffffff91000018 处处理内核分页请求

bash - 用于截断大文件的 Unix shell 脚本

linux - 使用 bash 迭代排序和就地替换文件

postgresql - 无法从终端在 Psql 中执行命令

unix - 仅在终端中显示当前路径