touch命令使用说明
1、touch命令概述:
创建文件和修改文件或者目录的时间戳
touch命令会根据当前的系统时间更新指定文件的访问时间和修改时间。如果文件不存在,将会创建新的空文件,除非指定了”-c”或”-h”选项。
注意:在修改文件的时间属性的时候,用户必须是文件的属主,或拥有写文件的访问权限。
2、命令语法:
touch 【选项】 【文件名或者目录名】
3、命令选项:
-a 只修改文件的access(访问)时间.
-c 或--no-create 更改文件时间戳,若文件不存在,不会创建不存在的文件。
-d 使用指定的日期时间(任何格式),而非现在的时间
-t 将时间修改为参数指定的日期,如:201807081556代表2018年7月8号15点56分
-m 只修改Modify(修改)时间,而不修改access(访问)时间
-r 使用参考文件的时间戳(access,modify),更改目标文件的时间戳(access,modify)与参考文件的时间戳相同
示例:touch -r 参考文件 目标文件
注:access 表示最后一次访问(仅仅是访问,没有改动)文件的时间
modify 表示最后一次修改文件的时间
change 表示最后一次对文件属性改变的时间,包括权限,大小,属性等等
-h 在符号链接文件上更改访问和修改时间
4、命令示例:
4.1创建新的文件
1 [root@localhost ~]# ls 2 anaconda-ks.cfg 3 [root@localhost ~]# touch a.txt 4 [root@localhost ~]# ls 5 anaconda-ks.cfg a.txt
4.2创建多个文件
1 [root@localhost ~]# ls 2 anaconda-ks.cfg a.txt 3 [root@localhost ~]# touch b.txt d.txt f.txt 4 [root@localhost ~]# ls 5 anaconda-ks.cfg a.txt b.txt d.txt f.txt
4.3创建多个有序的文件
1 [root@localhost ~]# ls 2 anaconda-ks.cfg 3 [root@localhost ~]# touch a{1..3}.txt 4 [root@localhost ~]# ls 5 a1.txt a2.txt a3.txt anaconda-ks.cfg 6 [root@localhost ~]# touch 9{a..c}.txt 7 [root@localhost ~]# ls 8 9a.txt 9b.txt 9c.txt a1.txt a2.txt a3.txt anaconda-ks.cfg
4.4 -a 修改文件的访问时间(access)
1 [root@localhost ~]# stat a.txt 2 File: ‘a.txt’ 3 Size: 45 Blocks: 8 IO Block: 4096 regular file 4 Device: 805h/2053d Inode: 201522683 Links: 1 5 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 6 Context: unconfined_u:object_r:admin_home_t:s0 7 Access: 2019-10-10 09:47:18.731012023 +0800 (当前时间) 8 Modify: 2019-10-10 09:47:42.346074011 +0800 9 Change: 2019-10-10 09:47:42.346074011 +0800 10 Birth: - 11 [root@localhost ~]# touch -a a.txt 12 [root@localhost ~]# stat a.txt 13 File: ‘a.txt’ 14 Size: 45 Blocks: 8 IO Block: 4096 regular file 15 Device: 805h/2053d Inode: 201522683 Links: 1 16 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 17 Context: unconfined_u:object_r:admin_home_t:s0 18 Access: 2019-10-10 09:52:58.744913520 +0800 (修改后时间) 19 Modify: 2019-10-10 09:47:42.346074011 +0800 20 Change: 2019-10-10 09:52:58.744913520 +0800 21 Birth: -
4.5 -c 如果文件存在,更改文件的时间戳,如果文件不存在,也不会创建它
1 [root@localhost ~]# stat a.txt 2 File: ‘a.txt’ 3 Size: 25 Blocks: 8 IO Block: 4096 regular file 4 Device: 805h/2053d Inode: 201522681 Links: 1 5 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 6 Context: unconfined_u:object_r:admin_home_t:s0 7 Access: 2019-10-10 10:05:45.047948145 +0800 8 Modify: 2019-10-10 10:02:26.431420414 +0800 9 Change: 2019-10-10 10:02:26.432420417 +0800 10 Birth: - 11 [root@localhost ~]# touch -c a.txt 12 [root@localhost ~]# stat a.txt 13 File: ‘a.txt’ 14 Size: 25 Blocks: 8 IO Block: 4096 regular file 15 Device: 805h/2053d Inode: 201522681 Links: 1 16 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 17 Context: unconfined_u:object_r:admin_home_t:s0 18 Access: 2019-10-10 10:06:01.399991766 +0800 19 Modify: 2019-10-10 10:06:01.399991766 +0800 20 Change: 2019-10-10 10:06:01.399991766 +0800 21 Birth: -
4.6 -d 使用指定的日期时间(任何格式),而非现在的时间
1 [root@localhost ~]# stat a.txt 2 File: ‘a.txt’ 3 Size: 25 Blocks: 8 IO Block: 4096 regular file 4 Device: 805h/2053d Inode: 201522681 Links: 1 5 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 6 Context: unconfined_u:object_r:admin_home_t:s0 7 Access: 2019-10-10 10:13:13.226143681 +0800 8 Modify: 2019-10-10 10:13:13.226143681 +0800 9 Change: 2019-10-10 10:13:13.226143681 +0800 10 Birth: - 11 [root@localhost ~]# touch -d '20180506 12:25:06' a.txt 12 [root@localhost ~]# stat a.txt 13 File: ‘a.txt’ 14 Size: 25 Blocks: 8 IO Block: 4096 regular file 15 Device: 805h/2053d Inode: 201522681 Links: 1 16 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 17 Context: unconfined_u:object_r:admin_home_t:s0 18 Access: 2018-05-06 12:25:06.000000000 +0800 19 Modify: 2018-05-06 12:25:06.000000000 +0800 20 Change: 2019-10-10 10:13:56.304258604 +0800 21 Birth: -
4.7 -m 只修改Modify(修改)时间,而不修改access(访问)时间
1 [root@localhost ~]# stat a.txt 2 File: ‘a.txt’ 3 Size: 25 Blocks: 8 IO Block: 4096 regular file 4 Device: 805h/2053d Inode: 201522681 Links: 1 5 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 6 Context: unconfined_u:object_r:admin_home_t:s0 7 Access: 2019-10-10 10:16:47.054714080 +0800 8 Modify: 2019-10-10 10:16:40.294696044 +0800 9 Change: 2019-10-10 10:16:40.294696044 +0800 10 Birth: - 11 [root@localhost ~]# touch -m a.txt 12 [root@localhost ~]# stat a.txt 13 File: ‘a.txt’ 14 Size: 25 Blocks: 8 IO Block: 4096 regular file 15 Device: 805h/2053d Inode: 201522681 Links: 1 16 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 17 Context: unconfined_u:object_r:admin_home_t:s0 18 Access: 2019-10-10 10:16:47.054714080 +0800 19 Modify: 2019-10-10 10:17:48.701878531 +0800 20 Change: 2019-10-10 10:17:48.701878531 +0800 21 Birth: -
4.8 -r 使用参考文件的时间戳(access,modify),更改目标文件的时间戳(access,modify)与参考文件的时间戳相同
1 [root@localhost ~]# stat b.txt 2 File: ‘b.txt’ 3 Size: 0 Blocks: 0 IO Block: 4096 regular empty file 4 Device: 805h/2053d Inode: 201522683 Links: 1 5 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 6 Context: unconfined_u:object_r:admin_home_t:s0 7 Access: 2019-10-10 10:21:29.259466874 +0800 8 Modify: 2019-10-10 10:21:29.259466874 +0800 9 Change: 2019-10-10 10:21:29.259466874 +0800 10 Birth: - 11 [root@localhost ~]# touch -r a.txt b.txt 12 [root@localhost ~]# stat b.txt 13 File: ‘b.txt’ 14 Size: 0 Blocks: 0 IO Block: 4096 regular empty file 15 Device: 805h/2053d Inode: 201522683 Links: 1 16 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 17 Context: unconfined_u:object_r:admin_home_t:s0 18 Access: 2019-10-10 10:16:47.054714080 +0800 19 Modify: 2019-10-10 10:17:48.701878531 +0800 20 Change: 2019-10-10 10:26:26.223266379 +0800 21 Birth: -
4.9 -t 将时间修改为参数指定的日期,如:201807081556代表2018年7月8号15点56分
1 [root@localhost ~]# stat a.txt 2 File: ‘a.txt’ 3 Size: 25 Blocks: 8 IO Block: 4096 regular file 4 Device: 805h/2053d Inode: 201522681 Links: 1 5 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 6 Context: unconfined_u:object_r:admin_home_t:s0 7 Access: 2019-10-10 10:33:19.601381756 +0800 8 Modify: 2019-10-10 10:33:19.601381756 +0800 9 Change: 2019-10-10 10:33:19.601381756 +0800 10 Birth: - 11 [root@localhost ~]# touch -t 201805231225 a.txt 12 [root@localhost ~]# stat a.txt 13 File: ‘a.txt’ 14 Size: 25 Blocks: 8 IO Block: 4096 regular file 15 Device: 805h/2053d Inode: 201522681 Links: 1 16 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) 17 Context: unconfined_u:object_r:admin_home_t:s0 18 Access: 2018-05-23 12:25:00.000000000 +0800 19 Modify: 2018-05-23 12:25:00.000000000 +0800 20 Change: 2019-10-10 10:33:46.865455315 +0800 21 Birth: -
4.10 -h 在符号链接文件上更改访问和修改时间
默认情况下,每当我们尝试使用 touch 命令更改符号链接文件的时间戳时,它只会更改原始文件的时间戳。如果你想更改符号链接文件的时间戳,则可以使用 touch 命令中的 -h 选项来实现
1 [root@lzg ~]# ls 2 1.txt 3 [root@lzg ~]# touch log.`date "+%Y-%m-%d_%H:%M:%S"`.txt #``反引号,调用命令 4 [root@lzg ~]# ls 5 1.txt log.2019-11-05_13:18:51.txt 6 [root@lzg ~]# touch log1.$(date "+%Y-%m-%d_%H:%M:%S").txt #或者$(),调用命令 7 [root@lzg ~]# ls 8 1.txt log1.2019-11-05_13:19:30.txt log.2019-11-05_13:18:51.txt
4.11 以上对文件更改时间戳的操作适用于目录