终端输出添加时间戳

ts命令

ts命令能够简单地完成时间戳的功能。本文将向你展示如何使用ts命令轻松地将时间戳添加到任何输出中。
要使用ts命令需要安装moreutils包。

apt install moreutils

参数 :

-s:从程序或操作开始显示增量时间戳。将显示从启动程序以来的时间。

-i:显示的时间是自从最后一个时间戳开始计算的

-r:它会将输入中现有的时间戳转换为相对时间,例如“15m5s ago”

命令使用

yjh@DESKTOP-C4M0UMF:~$ ping www.baidu.com | ts
Feb 23 16:19:32 PING www.a.shifen.com (112.80.248.75) 56(84) bytes of data.
Feb 23 16:19:32 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=1 ttl=54 time=9.13 ms
Feb 23 16:19:32 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=2 ttl=54 time=8.56 ms
Feb 23 16:19:33 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=3 ttl=54 time=9.69 ms
Feb 23 16:19:34 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=4 ttl=54 time=8.87 ms
Feb 23 16:19:35 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=5 ttl=54 time=20.3 ms
Feb 23 16:19:36 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=6 ttl=54 time=9.56 ms

增量时间戳

yjh@DESKTOP-C4M0UMF:~$ ping www.baidu.com | ts -s
00:00:01 PING www.a.shifen.com (112.80.248.75) 56(84) bytes of data.
00:00:01 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=1 ttl=54 time=9.82 ms
00:00:01 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=2 ttl=54 time=11.2 ms
00:00:03 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=3 ttl=54 time=151 ms
00:00:04 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=4 ttl=54 time=76.1 ms
00:00:04 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=5 ttl=54 time=51.9 ms
00:00:05 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=6 ttl=54 time=34.7 ms
00:00:06 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=7 ttl=54 time=23.6 ms
00:00:07 64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=8 ttl=54 time=16.9 ms

格式化时间戳

符号含义
%s显示自UNIX时代以来的秒数
%l
%M
%p上午\下午
%Y
%m
%d
%B用英文表示月
yjh@DESKTOP-C4M0UMF:~$ echo " - %B 完整打印月份名称" | ts %B
February  - %B 完整打印月份名称
yjh@DESKTOP-C4M0UMF:~$ echo " - %s 显示自UNIX时代以来的秒数" | ts %s
1677141472  - %s 显示自UNIX时代以来的秒数
yjh@DESKTOP-C4M0UMF:~$ echo " - %l\":\"%M%p 显示12h表示的时间" | ts %l":"%M%p
 4:38PM  - %l":"%M%p 显示12h表示的时间
yjh@DESKTOP-C4M0UMF:~$ echo " - %%Y-%m-%d 显示年-月-日" | ts %Y-%m-%d
2023-02-23  - %%Y-%m-%d 显示年-月-日
yjh@DESKTOP-C4M0UMF:~$  echo " - %B\" \"%d,%y 用英文表示月 日,年" | ts %B" "%d,%Y
February 23,2023  - %B" "%d,%y 用英文表示月 日,年
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值