Linux基础_时间相关

蛋蛋 2023年06月05日 43 0

时间戳在线转换

https://tool.lu/timestamp/

Linux 上的3个时间

mtime

last modification

文件内容修改时间

ctime

last change time

变动时间,例如文件内容、文件权限权限等的变更时间

atime

last access time

上次访问时间

crtime

linux中对于对于EXT4文件系统类型,我们也可以查看文件的创建时间crtime

create time

文件创建时间

查看命令

# stat file
  File: `file'
  Size: 68176035  	Blocks: 133168     IO Block: 4096   regular file
Device: 803h/2051d	Inode: 17039367    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2022-06-23 10:40:02.038129410 +0800
Modify: 2022-06-23 10:31:35.100129958 +0800
Change: 2022-06-23 10:31:35.281129958 +0800

Linux上做时间比较

思路:

1、转换为时间戳再做比较

check_file="/www/oip_map.txt"

#1, Check if oipmap is not updated in time
current_timestamp=$(date +%s)
file_modification_timestamp=$(stat -c %Y ${check_file})
time_difference=$(echo "$current_timestamp - $file_modification_timestamp" | bc)
[ $time_difference -ge 86400 ] && oipmap_time=1 || oipmap_time=0

Linux date

时间格式转换

命令 展示 详解
date '+%Y%m%d%H%M%S'
date +%Y%m%d%H%M%S
20211208184056 年月日时分秒
date +%s 1638960191 时间戳,从1970年1月1日零点开始到现在经历的秒数
date Wed Dec 8 18:43:42 CST 2021
date '+%c'
date +%Y%m%d 显示现在的年月日
date +%Y%m%d --date="+1 day"
date +%Y%m%d --date="+1 month"
date +%Y%m%d --date="+1 year"
显示后一天的日期
显示下一月的日期
显示下一年的日期
date +%Y%m%d --date="-1 day"
date +%Y%m%d --date="-1 month"
date +%Y%m%d --date="-1 year"
显示前一天的日期
显示上一月的日期
显示前一年的日期
date -d @1652123558 +"%Y-%m-%d %H:%M:%S" 2022-05-10 03:12:38 时间戳转换成时间
for i in awk -F'.' '{print $3}' a.txt;do date -d @$i +"%Y-%m-%d %H:%M:%S";done 批量转换时间戳

Linux stat

display file or file system status

显示文件或文件系统状态

参数:

-L, --dereference 取消引用
follow links 按照链接

-Z, --context 上下文
print the SELinux security context

​ 打印 SELinux 安全上下文

-f, --file-system
display file system status instead of file status

-c --format=FORMAT
use the specified FORMAT instead of the default; output a newline after each use of FORMAT

--printf=FORMAT
like --format, but interpret backslash escapes, and do not output a mandatory trailing newline. If you want a newline, include \n in FORMAT.

-t, --terse
print the information in terse form

--help display this help and exit

--version
output version information and exit

The valid format sequences:

files file systems 案例
%a Access rights in octal
八进制访问权限
Free blocks available to non-superuser
非超级用户可用的空闲块
%A Access rights in human readable form
人类可读形式的访问权限
%b Number of blocks allocated (see %B)
分配的块数(见 %B)
Total data blocks in file system
文件系统中的总数据块
%B The size in bytes of each block reported by %b
%b 报告的每个块的大小(以字节为单位)
%c Total file nodes in file system
文件系统中的文件节点总数
%C SELinux security context string
SELinux 安全上下文字符串
同左
%d Device number in decimal
十进制设备编号
Free file nodes in file system
文件系统中有 %d 个空闲文件节点
%D Device number in hex
十六进制的设备编号
%f Raw mode in hex
十六进制的原始模式
Free blocks in file system
文件系统中的空闲块
%F File type
文件类型
%g Group ID of owner
所有者的组 ID
%G Group name of owner
所有者的组名
%h Number of hard links
硬链接数
%i Inode number
索引节点号
File System ID in hex
十六进制文件系统 ID
%l Maximum length of filenames
文件名的最大长度
%n File name 同左
%N Quoted file name with dereference if symbolic link
引用的文件名,如果是符号链接,则取消引用
%o I/O block size
I/O 块大小
%s Total size, in bytes
总大小,以字节为单位
Block size (for faster transfers)
块大小(用于更快的传输)
%S Fundamental block size (for block counts)
基本块大小(用于块计数)
%t Major device type in hex
十六进制的主要设备类型
Type in hex
输入十六进制
%T Minor device type in hex
十六进制的次要设备类型
Type in human readable form
以人类可读的形式输入
%u User ID of owner
所有者的用户 ID
%U User name of owner
所有者的用户名
%x Time of last access
上次访问时间
%X Time of last access as seconds since Epoch
自 Epoch 以来的最后一次访问时间,以秒为单位
%y Time of last modification
上次修改时间
# stat -c %y /www/oip_map.txt
2022-06-23 10:31:35.100129958 +0800
%Y Time of last modification as seconds since Epoch
自 Epoch 以来最后一次修改的时间,以秒为单位
# stat -c %Y /www/oip_map.txt
1655951495
%z Time of last change
上次更改时间
%Z Time of last change as seconds since Epoch
自 Epoch 以来最后一次更改的时间,以秒为单位

批量转换时间戳

shell命令

sort -u timestamp.txt | awk '{print $0 " (" strftime("%Y-%m-%d %H:%M:%S", $0) ")"}'
# 或
sort -u timestamp.txt | while read ts; do echo -e "$ts\t$(date -d @$ts '+%Y-%m-%d %H:%M:%S')"; done

Excel 实现时间戳格式与日期时间格式互转

时间戳10位数(秒)=(A1+8*3600)/86400+70*365+19
时间戳13位数(毫秒)=(A1+8*3600000)/86400000+70*365+19
或
时间戳10位数(秒)=TEXT((A1/86400) + DATE(1970,1,1), "yyyy-mm-dd hh:mm:ss")
时间戳13位数(毫秒)=TEXT((A1/86400000) + DATE(1970,1,1), "yyyy-mm-dd hh:mm:ss")
或
时间戳10位数(秒)=TEXT(((A1/86400) + DATE(1970,1,1) + TIME(8,0,0)), "yyyy-mm-dd hh:mm:ss")
时间戳13位数(毫秒)=TEXT(((A1/86400000) + DATE(1970,1,1) + TIME(8,0,0)), "yyyy-mm-dd hh:mm:ss")
或
时间戳10位数(秒)=TEXT(((A1/86400) + DATE(1970,1,1) + TIME(0,0,0)), "yyyy-mm-dd hh:mm:ss")
时间戳13位数(毫秒)=TEXT(((A1/86400000) + DATE(1970,1,1) + TIME(0,0,0)), "yyyy-mm-dd hh:mm:ss")
  1. 首先,我们看到A列数据显示为时间戳格式,选中B列单元格,单击右键选择设置单元格格式。

  2. 弹出单元格格式的对话框,在分类里选择自定义,然后在类型里选择YYYY-M-D H:MM格式,单击确定。

  3. 然后在单元格当中输入公式=(A1+8*3600)/86400+70*365+19,如下图所示。

  4. 单击回车,我们可以看到单元格当中就转换为日期格式了,如下图所示。

  5. 下拉单元格填充框,完善整个表格,这样就实现了时间戳格式与日期时间格式互转,如下图所示。

Last Updated: 2025/09/17 14:00:52
[转]linux基础_设置别名alias Linux硬件相关排查