语法
touch [-acfm][-d<日期时间>][-r<参考文件或目录>] [-t<日期时间>][--help][--version][文件或目录…]
作用
用于修改文件或者目录的时间属性,包括存取时间和更改时间。若文件不存在,系统会建立一个新的文件。
参数
- a
改变档案的读取时间记录(atime)。 - m
改变档案的修改时间记录(mtime)。 - c
假如目的档案不存在,不会建立新的档案。与 –no-create 的效果一样。 - f
不使用,是为了与其他 unix 系统的相容性而保留。 - r
使用参考档的时间记录,与 –file 的效果一样。 - d
设定时间与日期,可以使用各种不同的格式。 - t
设定档案的时间记录,格式与 date 指令相同。格式”[YYYYMMDDhhmm]” - –no-create
不会建立新档案。 - –help
列出指令格式。 - –version
列出版本讯息。
使用实例
建立一个空文档并查看时间
[root@Ethan Ethan]# touch a.txt; ll a.txt; ll --time=atime a.txt; ll --time=ctime a.txt
-rw-r--r-- 1 root root 0 Feb 27 18:31 a.txt
-rw-r--r-- 1 root root 0 Feb 27 18:31 a.txt
-rw-r--r-- 1 root root 0 Feb 27 18:31 a.txt
当此文档存在时,再使用【touch】命令,会更新所有时间为当前时间。
[root@Ethan Ethan]# touch a.txt; ll a.txt; ll --time=atime a.txt; ll --time=ctime a.txt
-rw-r--r-- 1 root root 0 Feb 27 18:33 a.txt
-rw-r--r-- 1 root root 0 Feb 27 18:33 a.txt
-rw-r--r-- 1 root root 0 Feb 27 18:33 a.txt
将日期修改为两天前
方法一:
[root@Ethan Ethan]# touch -d '2 days ago' a.txt
[root@Ethan Ethan]# ll a.txt; ll --time=atime a.txt; ll --time=ctime a.txt
-rw-r--r-- 1 root root 0 Feb 25 18:55 a.txt
-rw-r--r-- 1 root root 0 Feb 25 18:55 a.txt
-rw-r--r-- 1 root root 0 Feb 27 18:55 a.txt
可以发现“ctime”没有跟着改变,而是变成当前时间。
方法二:
[root@Ethan Ethan]# touch -t 200001010000 a.txt
[root@Ethan Ethan]# ll a.txt; ll --time=atime a.txt; ll --time=ctime a.txt
-rw-r--r-- 1 root root 0 Jan 1 2000 a.txt
-rw-r--r-- 1 root root 0 Jan 1 2000 a.txt
-rw-r--r-- 1 root root 0 Feb 27 18:58 a.txt