描述:建立一个新的文件,当被建立的文件存在时,则改变文件的时间戳。node
用法:touch [-acmdtr] 文件名vim
经常使用选项:code
-a:修改文件的访问时间;文档
-c:仅用来修改文件的时间,若是目标文件不存在,不会建立新的文件。与--no-create效果同样;cmd
-m:修改文件的修改时间;object
-d:后面接日期,也能够使用--date="日期或时间”;date
-t:后面接日期,格式为[YYMMDDhhmm]。file
-r:把指定文档或目录的日期时间,通通设成和参考文档或目录的日期时间相同。im
示例:时间戳
1.建立三个空文件 a b c
[root@share21 ~]# touch a b c
2.建立三个空文件 file001 file002 file003
[root@share21 ~]# touch file{001,002,003}
3.若是file004文件不存在,则不建立文件,仅用来修改时间
[root@share21 ~]# touch -c file004 [root@share21 ~]# ll total 0 -rw-r--r--. 1 root root 0 May 18 11:05 file001 -rw-r--r--. 1 root root 0 May 18 11:05 file002 -rw-r--r--. 1 root root 0 May 18 11:05 file00
4.建立file004,且将file004的时间戳设置的和file001同样
[root@share21 ~]# touch file004 [root@share21 ~]# ll total 0 -rw-r--r--. 1 root root 0 May 18 11:05 file001 -rw-r--r--. 1 root root 0 May 18 11:05 file002 -rw-r--r--. 1 root root 0 May 18 11:05 file003 -rw-r--r--. 1 root root 0 May 18 11:12 file004 [root@share21 ~]# touch -r file001 file004 [root@share21 ~]# ll total 0 -rw-r--r--. 1 root root 0 May 18 11:05 file001 -rw-r--r--. 1 root root 0 May 18 11:05 file002 -rw-r--r--. 1 root root 0 May 18 11:05 file003 -rw-r--r--. 1 root root 0 May 18 11:05 file004 [root@share21 ~]#
5.同时修改文件的修改时间和访问时间
[root@share21 ~]# touch -t 201211251230 file004 [root@share21 ~]# ll total 0 -rw-r--r--. 1 root root 0 May 18 11:05 file001 -rw-r--r--. 1 root root 0 May 18 11:05 file002 -rw-r--r--. 1 root root 0 May 18 11:05 file003 -rw-r--r--. 1 root root 0 Nov 25 2012 file004
[root@share21 ~]# touch -d "2012-11-25 12:01" file004 [root@share21 ~]# stat file004 File: ‘file004’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd00h/64768d Inode: 67655814 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2012-11-25 12:01:00.000000000 +0800 Modify: 2012-11-25 12:01:00.000000000 +0800 Change: 2016-05-18 11:28:01.466228047 +0800 Birth: -
6.只修改文件的访问时间
[root@share21 ~]# touch -d -a "2012-11-25 12:01" file004
7.只修改文件的修改时间
[root@share21 ~]# touch -d -m "2012-11-25 12:01" file004
最后总结下经常使用的文件操做与时间的关系:
一、访问时间,读一次这个文件的内容,这个时间就会更新。好比对这个文件使用more命令。ls、stat命令都不会修改文件的访问时间。
二、修改时间,对文件内容修改一次,这个时间就会更新。好比:vim后保存文件。ls -l列出的时间就是这个时间。
三、状态改变时间。经过chmod命令更改一次文件属性,这个时间就会更新。查看文件的详细的状态、准确的修改时间等,能够经过stat命令文件名。