touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time 更新时间戳
-
touch 默认改变了文件的 atime ctime mtime 这3个时间
access time,文件数据最近的一次访问时间.可使用stat查看三个时间
modify time,文件数据改变的时间(data).mtime发生变化,ctime一定变化
change time,文件状态变更的时间.及包含数据改变(mtime)以及metadata(权限,属主,属组...)
touch 硬链接,其中一个更改,另外的完全同步
touch 软链接本身无效,但软链接指向的源文件更新,同 >软链接 清空源文件一样的原理
-
常用选项:
-a:change only the access time 只修改atime -m:change only the modification time 只修改mtime(ctime一并修改) -c:do not create any files 文件已经存在的话不修改时间戳(只用于创建新文件) -t:use [[CC]YY]MMDDhhmm[.ss] instead of current time 使用具体时间修改 -r:use this file's times instead of current time 选择参照文件更改时间
-
touch按日期减一天来创建一个新文件 touch $(date -d "-1 day" +%F).log
[xzt7566@centos7-test Downloads]$ touch `date -d "-1 day" +%F`.log [xzt7566@centos7-test Downloads]$ ll total 0 -rw-rw-r--. 1 xzt7566 xzt7566 0 May 6 13:54 2019-05-05.log
-
更改file2同file1的时间一样
[xzt7566@centos7-test Downloads]$ touch -r hosts testhosts [xzt7566@centos7-test Downloads]$ stat * File: ‘hosts’ Size: 158 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 51758008 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ xzt7566) Gid: ( 1000/ xzt7566) Context: system_u:object_r:net_conf_t:s0 Access: 2019-05-04 18:56:34.948306623 +0800 Modify: 2013-06-07 22:31:32.000000000 +0800 Change: 2019-05-05 09:50:22.277298967 +0800 File: ‘testhosts’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd00h/64768d Inode: 51753441 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ xzt7566) Gid: ( 1000/ xzt7566) Context: unconfined_u:object_r:user_home_t:s0 Access: 2019-05-04 18:56:34.948306623 +0800 Modify: 2013-06-07 22:31:32.000000000 +0800 Change: 2019-05-05 09:57:37.337378898 +0800
-
更改mtime时间(ctime会一并修改)
[xzt7566@centos7-test Downloads]$ touch -m testhosts [xzt7566@centos7-test Downloads]$ stat testhosts File: ‘testhosts’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd00h/64768d Inode: 51753441 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ xzt7566) Gid: ( 1000/ xzt7566) Context: unconfined_u:object_r:user_home_t:s0 Access: 2019-05-04 18:56:34.948306623 +0800 Modify: 2019-05-05 10:10:23.578318358 +0800 Change: 2019-05-05 10:10:23.578318358 +0800
-
更改atime时间为18年11月11日01时01分
[xzt7566@centos7-test Downloads]$ touch -a -t 1811110101 testhosts [xzt7566@centos7-test Downloads]$ stat testhosts File: ‘testhosts’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd00h/64768d Inode: 51753441 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ xzt7566) Gid: ( 1000/ xzt7566) Context: unconfined_u:object_r:user_home_t:s0 Access: 2018-11-11 01:01:00.000000000 +0800 Modify: 2019-05-05 10:10:23.578318358 +0800 Change: 2019-05-05 10:20:15.651240345 +0800