gpt4 book ai didi

linux - 自上次更新人类可读格式的文件以来的时间

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:32 25 4
gpt4 key购买 nike

我想以人类可读的格式获取自上次更新文件以来的时间。在网上我发现:

now=$(date +%s) # to get timestamp of now
last=$(stat -c %Y /var/cache/apt/) # to get the timestamp of the last update

但现在我想将 nowlast 分开,并在 Debian shell 中以人类可读的格式打印出来?

最佳答案

你可以写一个函数:

time_since() {
file=$1
now=$(date +%s)
file_time=$(stat -c %Y "$file")
seconds=$((now - file_time))
hh=$(( $seconds / 3600 ))
mm=$(( ($seconds - $hh * 3600) / 60 ))
ss=$(( $seconds - $hh * 3600 - $mm * 60 ))
printf '%d:%02d:%02d\n' $hh $mm $ss
}

time_since /var/cache/apt
# output => 332:17:07

关于linux - 自上次更新人类可读格式的文件以来的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42910399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com