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