命令介绍
命令:touch 将每个文件的访问和修改时间更新为当前时间;文件不存在则创建
用法: touch [OPTION]… FILE… touch 选项 文件
命令选项
[root@fp-21 ~]# touch --help
-a # 只更改访问时间( atime )
-c, --no-create # 不创建任何文件
-d, --date=STRING # 设置时间和日期,可以使用各种不同的格式
-f # 不使用(户忽略)
-m # 只更改修修改时间( mtime )
-r, --reference=FILE # 使用参考文档的时间记录
-t STAMP # 使用"[[CC]YY]MMDDhhmm[.ss]"格式的时间替代当前时间
--help # 帮助文档
--version # 版本信息
命令实例
# 只更改访问时间( atime )
[root@fp-21 tmp]# ll a.txt
-rw-r--r--. 1 tom jack 385 Feb 21 15:29 a.txt
[root@fp-21 tmp]# touch a.txt
[root@fp-21 tmp]# ll a.txt
-rw-r--r--. 1 tom jack 385 Feb 23 13:48 a.txt
# 不创建任何文件
[root@fp-21 tmp]# touch -c test_file
[root@fp-21 tmp]# ll test_file
ls: cannot access test_file: No such file or directory
# 使用"[[CC]YY]MMDDhhmm[.ss]"格式的时间替代当前时间
[root@fp-21 tmp]# touch -t "202001231351" test_file
[root@fp-21 tmp]# ll test_file
-rw-r--r--. 1 root root 0 Jan 23 13:51 test_file
# 创建多个文件
[root@fp-21 opt]# touch test{1..3}.txt
[root@fp-21 opt]# ll
-rw-r--r--. 1 root root 0 Feb 23 15:00 test1.txt
-rw-r--r--. 1 root root 0 Feb 23 15:00 test2.txt
-rw-r--r--. 1 root root 0 Feb 23 15:00 test3.txt
# 混合创建多个文件
[root@fp-21 opt]# touch {a,b}.{txt,sql}
[root@fp-21 opt]# ll
-rw-r--r--. 1 root root 0 Feb 23 15:01 a.sql
-rw-r--r--. 1 root root 0 Feb 23 15:01 a.txt
-rw-r--r--. 1 root root 0 Feb 23 15:01 b.sql
-rw-r--r--. 1 root root 0 Feb 23 15:01 b.txt
注
文件的时间戳:
- atime:access time,上一次的访问时间
- mtime:modify time,文件的修改时间
- ctime:change time,修改元数据时间( 重命名文件,更改权限,更改属组、属主,移动文件,修改文件内容 )
atime 只在文件被打开访问时才改变,若不是打开文件编辑内容( 如重定向内容到文件中 ),则 ctime 和 mtime 的改变不会引起 atime 的改变。
mtime 的改变一定引起 ctime 的改变,而访问文件时( cat、less、more ),atime 不一定会改变,所以 atime “改变” 不一定会影响ctime。
如何查看一个文件的时间戳
[root@fp-21 tmp]# stat test_file |sed -n 6,8p
Access: 2020-01-23 13:51:00.000000000 +0800
Modify: 2020-01-23 13:51:00.000000000 +0800
Change: 2020-02-23 13:52:08.086979833 +0800