2015-06-25创建
1、概要:
Linux touch 命令
改变文件的时间戳。将每个文件的访问时间和修改时间变为当前时间。如果访问的文件不存在,那么该文件会被创建,除非使用了选项 -c 或者 -h。
所以该命令也常用于创建文件。
2、命令语法:
touch [OPTION]... FILE... 中文:touch [选项]... 文件...
3、常用示例:
touch test 如果没有文件test 那么就创建,有则改变该文件的时间戳。
touch -c test 如果存在该文件那么改变其时间戳,否则无任何效果,也不会创建新文件。
4、常用命令及参数:
[root@moban data]# touch -a ttt ##此时文件不存在,所以-a 无效 [root@moban data]# stat ttt ##可以查看文件的时间属性 File: `ttt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 803h/2051d Inode: 915783 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-06-26 06:08:42.497865566 +0800 ##访问时间 Modify: 2015-06-26 06:08:42.496865502 +0800 ##修改时间 Change: 2015-06-26 06:08:42.497865566 +0800 ##改变时间,当文件属性改变时,该时间会改变 [root@moban data]# touch -a ttt [root@moban data]# stat ttt File: `ttt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 803h/2051d Inode: 915783 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-06-26 06:09:01.632877204 +0800 Modify: 2015-06-26 06:08:42.496865502 +0800 Change: 2015-06-26 06:09:01.632877204 +0800
[root@moban data]# stat aa ##当前文件时间戳信息 File: `aa' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 803h/2051d Inode: 915757 Links: 4 Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-06-26 05:39:43.382866279 +0800 Modify: 2015-06-26 05:39:43.382866279 +0800 Change: 2015-06-26 05:39:43.382866279 +0800 [root@moban data]# touch -d "2012-12-11 15:23:50" aa ##替换当前时间戳信息 [root@moban data]# stat aa File: `aa' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 803h/2051d Inode: 915757 Links: 4 Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2012-12-11 15:23:50.000000000 +0800 Modify: 2012-12-11 15:23:50.000000000 +0800 Change: 2015-06-26 06:31:38.958867074 +0800 [root@moban data]# touch --date="2009-09-01" aa [root@moban data]# stat aa File: `aa' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 803h/2051d Inode: 915757 Links: 4 Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2009-09-01 00:00:00.000000000 +0800 Modify: 2009-09-01 00:00:00.000000000 +0800 Change: 2015-06-26 06:32:17.329870867 +0800
[root@moban data]# stat aa File: `aa' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 803h/2051d Inode: 915757 Links: 4 Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2009-09-01 00:00:00.000000000 +0800 Modify: 2009-09-01 00:00:00.000000000 +0800 Change: 2015-06-26 06:34:01.560872647 +0800 [root@moban data]# touch -m aa ##只改变 修改时间戳 信息 [root@moban data]# stat aa File: `aa' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 803h/2051d Inode: 915757 Links: 4 Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2009-09-01 00:00:00.000000000 +0800 Modify: 2015-06-26 06:35:59.893881443 +0800 Change: 2015-06-26 06:35:59.893881443 +0800 [root@moban data]# touch -m -d "2020-05-05" aa ## -m 联合 -d 使用 [root@moban data]# stat aa File: `aa' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 803h/2051d Inode: 915757 Links: 4 Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2009-09-01 00:00:00.000000000 +0800 Modify: 2020-05-05 00:00:00.000000000 +0800 Change: 2015-06-26 06:36:22.770864951 +0800
使用该文件的时间替代当前时间
[root@mode-6 day04]# touch -r ../install.log 3 # ../install.log 文件, 3 是另外一个文件
[root@mode-6 day04]# stat 3 File: `3' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 803h/2051d Inode: 155749 Links: 2 Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-10-02 03:59:47.578999375 +0800 # 注意该时间 Modify: 2015-10-02 04:06:38.990999209 +0800 # 注意该时间 Change: 2015-10-03 18:25:13.943263595 +0800 [root@mode-6 day04]# stat ../install.log File: `../install.log' Size: 39693 Blocks: 88 IO Block: 4096 regular file Device: 803h/2051d Inode: 15 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-10-02 03:59:47.578999375 +0800 # 注意该时间 Modify: 2015-10-02 04:06:38.990999209 +0800 # 注意该时间 Change: 2015-10-02 04:06:46.126999205 +0800
[root@moban data]# which touch /bin/touch [root@moban data]# type touch touch is hashed (/bin/touch)