Linux-touch

语法

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值