Linux touch命令详述

2020-09-21 0 434 百度已收录

touch命令

touch命令两个功能:一是用于把已存在文件时间标签更新为系统当前的时间(默认方式),它们的数据将原封不动地保留下来;二是用来创建新的空文件。

 语法

touch(选项)(参数)

最常用用法:touch fileA
– 如果fileA存在,使用touch指令可更改这个文件或目录日期时间,包括存取时间和更改时间;
– 如果fileA不存在,touch指令会在当前目录下新建一个空白文件fileA。

 参数
参数 解释
-a  只更改存取时间。
-c  不建立任何文档
-d 使用指定的日期时间,而非现在的时间。 [[CC]YY]MMDD text
-f 此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题。
-m  只更改变动时间。
-r 把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。
-t 使用指定的日期时间,而非现在的时间[CC[YY]MMDDhhmm[.SS] 
–help 在线帮助
–version 显示版本信息
 使用范例:
 实例一:创建不存在的文件

touch test1.txt test2.txt

[root@Linuxidc tmp]# touch test1.txt test2.txt  [root@Linuxidc tmp]# ll  total 0  -rw-r--r-- 1 root root 0 Nov 30 14:54 test1.txt  -rw-r--r-- 1 root root 0 Nov 30 14:54 test2.txt
 实例二:如果test3不存在,则不创建文件

touch -c test3.txt

[root@Linuxidc tmp]# touch -c test3.txt  [root@Linuxidc tmp]# ll  total 0  -rw-r--r-- 1 root root 0 Nov 30 14:54 test1.txt  -rw-r--r-- 1 root root 0 Nov 30 14:54 test2.txt  
 实例三:更新test1.txt的时间和test2.txt时间戳相同

touch touch test1.txt test3.txt

[root@Linuxidc tmp]# ll test1.txt test3.txt  -rw-r--r-- 1 root root 0 Nov 30 14:54 test1.txt  -rw-r--r-- 1 root root 0 Nov 30 14:58 test3.txt  [root@Linuxidc tmp]# touch test1.txt test3.txt  [root@Linuxidc tmp]# ll test1.txt test3.txt  -rw-r--r-- 1 root root 0 Nov 30 14:58 test1.txt  -rw-r--r-- 1 root root 0 Nov 30 14:58 test3.txt  [root@Linuxidc tmp]#
 实例四:设定文件的时间戳

touch -t 201808080808 test1.txt

[root@Linuxidc tmp]# ll test1.txt  -rw-r--r-- 1 root root 0 Nov 30 14:58 test1.txt  [root@Linuxidc tmp]# touch -t 201808080808 test1.txt    [root@Linuxidc tmp]# stat test1.txt    File: 'test1.txt'    Size: 0             Blocks: 0          IO Block: 4096   regular empty file  Device: 803h/2051d    Inode: 11041       Links: 1  Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)  Access: 2018-08-08 08:08:00.000000000 +0800  Modify: 2018-08-08 08:08:00.000000000 +0800  Change: 2019-05-30 15:01:03.959402702 +0800   Birth: -
 实例五:将后面的时间改成前面的时间,将test2.txt的时间改为test1.txt的时间。

touch -r test1.txt test2.txt 

[root@Linuxidc tmp]# ll  总用量 0  -rw-r--r-- 1 root root 0 8月   8 2018 test1.txt  -rw-r--r-- 1 root root 0 11月 30 14:54 test2.txt  -rw-r--r-- 1 root root 0 11月 30 14:58 test3.txt  [root@Linuxidc tmp]# touch -r test1.txt test2.txt  [root@Linuxidc tmp]# ll  总用量 0  -rw-r--r-- 1 root root 0 8月   8 2018 test1.txt  -rw-r--r-- 1 root root 0 8月   8 2018 test2.txt  -rw-r--r-- 1 root root 0 11月 30 14:58 test3.txt
 实例六:把所以的.txt文件修改到2013年10月13日的时间。操作命令:

touch -d "10/13/2013" *.txt

[root@Linuxidc tmp]# touch -d 20161013 *.txt  [root@Linuxidc tmp]# ll  总用量 0  -rw-r--r-- 1 root root 0 10月 13 00:00 test1.txt  -rw-r--r-- 1 root root 0 10月 13 00:00 test2.txt  -rw-r--r-- 1 root root 0 10月 13 00:00 test3.txt

另外也可以单独修改时间或者月份,如下:
以使用 am, pm 或是 24 小时格式,日期可以使用其他格式如 6 May 2000 。
– touch -d “6:03pm” file
– touch -d “05/06/2000” file
– touch -d “6:03pm 05/06/2000” file


 实例七: 不带任何选项下运行 touch

最简单的使用 touch 命令。只需键入:$ touch file_name
请观察下面的一张截图

[root@Linuxidc tmp]# ll  总用量 0  -rw-r--r-- 1 root root 0 10月 13 00:00 test1.txt  [root@Linuxidc tmp]# touch test1.txt  [root@Linuxidc tmp]# ll  总用量 0  -rw-r--r-- 1 root root 0 11月 30 15:23 test1.txt

test1.txt原来的时间戳是 00:00在我们使用 touch 命令后,它改变为了 15:23。由此看出,默认情况下,touch 会将文件的时间戳改为当前时间。

 实例八:只改变访问时间

正如我们之前提到的,每个文件都附有访问时间和修改时间。上面的时间戳是 15:23。我们可以更多细节

[root@Linuxidc tmp]# stat test1.txt    文件:"test1.txt"    大小:0             块:0          IO 块:4096   普通空文件  设备:803h/2051d    Inode:11041       硬链接:1  权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)  最近访问:2019-05-30 15:23:27.216913900 +0800  最近更改:2019-05-30 15:23:27.216913900 +0800  最近改动:2019-05-30 15:23:27.216913900 +0800  创建时间:-

我们发现访问时间和修改时间的值是相同的都是 15:23:27 ,还有它们属于同一时区 GMT +8。
如果现在我们要只改变访问时间,我们需要使用-a选项。
touch -a test1.txt

[root@Linuxidc tmp]# touch -a test1.txt  [root@Linuxidc tmp]# stat test1.txt    文件:"test1.txt"    大小:0             块:0          IO 块:4096   普通空文件  设备:803h/2051d    Inode:11041       硬链接:1  权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)  最近访问:2019-05-30 15:28:35.938248963 +0800  最近更改:2019-05-30 15:26:16.850686494 +0800  最近改动:2019-05-30 15:28:35.938248963 +0800  创建时间:-

如你所见,访问时间变为了 15:28:35 ,但是修改时间仍为15:26:16

 实例九:只改变修改时间

使用-m选项来实现
touch -m test1.txt

[root@Linuxidc tmp]# touch -m test1.txt  [root@Linuxidc tmp]# stat test1.txt    文件:"test1.txt"    大小:0             块:0          IO 块:4096   普通空文件  设备:803h/2051d    Inode:11041       硬链接:1  权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)  最近访问:2019-05-30 15:28:35.938248963 +0800  最近更改:2019-05-30 15:31:41.033247158 +0800  最近改动:2019-05-30 15:31:41.033247158 +0800  创建时间:-

现在修改时间改为了 15:31:41。请注意,当文件被访问或修改时,状态改变时间域的值总会对其记录。

 实例十:更改为自定义时间戳

-a-m选项都会将文件的时间戳改为现在当前时间,也可以更改为自定义时间戳。使用-t选项实现。
从上面示例示例中的 test1.txt,我们看出它的时间戳是:
– 最近访问:2019-05-30 15:28:35
– 最近更改:2019-05-30 15:31:41
– 最近改动:2019-05-30 15:31:41

假如我们想要将访问时间和修改时间改为2017年1月12日 09:58:27。我们可以用下列命令来完成:
 touch -t 201701120958.27 test1.txt

[root@Linuxidc tmp]# touch -t 201701120958 test1.txt  [root@Linuxidc tmp]# stat test1.txt    文件:"test1.txt"    大小:0             块:0          IO 块:4096   普通空文件  设备:803h/2051d    Inode:11041       硬链接:1  权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)  最近访问:2017-01-12 09:58:00.000000000 +0800  最近更改:2017-01-12 09:58:00.000000000 +0800  最近改动:2019-05-30 15:36:43.837822257 +0800  创建时间:-

-t 选项语法组成 :

[[CC]YY]MMDDhhmm [.SS]

CC – 年份的前两位  YY – 年份的后两位
MM – 月份 [01-12] DD – 日期 [01-31]
hh – 时 [00-23] mm – 分 [00-59]
SS – 秒 [00-61]  

这里,CC为年数中的前两位,即”世纪数”;YY为年数的后两位,即某世纪中的年数.如果不给出CC的值,则touch 将把年数CCYY限定在1969–2068之内.MM为月数,DD为天将把年数CCYY限定在1969–2068之内.MM为月数,DD为天数,hh为小时数(几点),mm为分钟数,SS为秒数.此处秒的设定范围是0–61,这样可以处理闰秒.这些数字组成的时间是环境变量TZ指定的时区中的一个时间.由于系统的限制,早于1970年1月1日的时间是错误的.

 实例十一:改变日期和时间的另一种方式

如果你觉得[[CC]YY]MMDDhhmm [.SS]格式不适合你,我们也可以使用 -d 选项。下面是-d选项使用的一个示例。
举例来说,我们有个名为 test3.txt 的文件,它的相关属性在下面的截图中展示了。

[root@Linuxidc tmp]# stat test3.txt     文件:"test3.txt"    大小:0             块:0          IO 块:4096   普通空文件  设备:803h/2051d    Inode:11044       硬链接:1  权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)  最近访问:2016-10-13 00:00:00.000000000 +0800  最近更改:2016-10-13 00:00:00.000000000 +0800  最近改动:2019-05-30 15:11:35.285472753 +0800  创建时间:-

现在我们要将日期2017年1月14日改为2013年12月20日。可以使用下列命令:
$ touch -d '10-December-2013' test3.txt

[root@Linuxidc tmp]#  touch -d '10-December-2013' test3.txt  [root@Linuxidc tmp]# stat test3.txt     文件:"test3.txt"    大小:0             块:0          IO 块:4096   普通空文件  设备:803h/2051d    Inode:11044       硬链接:1  权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)  最近访问:2013-12-10 00:00:00.000000000 +0800  最近更改:2013-12-10 00:00:00.000000000 +0800  最近改动:2019-05-30 16:22:21.647100822 +0800  创建时间:-  

不出意料,访问时间和修改时间的日期已经改为了2013年12月10日。

 实例十二:改变时区

如果我们想改变指定GMT的时间,我们也可以使用-d选项。
touch file_3.txt

[root@Linuxidc tmp]# touch test3.txt   [root@Linuxidc tmp]# stat test3.txt     文件:"test3.txt"    大小:0             块:0          IO 块:4096   普通空文件  设备:803h/2051d    Inode:11044       硬链接:1  权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)  最近访问:2019-05-30 16:23:50.453099824 +0800  最近更改:2019-05-30 16:23:50.453099824 +0800  最近改动:2019-05-30 16:23:50.453099824 +0800  创建时间:-  

不难发现 test3.txt 的时区为 GMT +0800。要改为 GMT3 时区,我们只需要键入如下命令:
touch -d GMT3 test3.txt

[root@Linuxidc tmp]# touch -d GMT3 test3.txt  [root@Linuxidc tmp]# stat test3.txt     文件:"test3.txt"    大小:0             块:0          IO 块:4096   普通空文件  设备:803h/2051d    Inode:11044       硬链接:1  权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)  最近访问:2019-05-30 11:00:00.000000000 +0800  最近更改:2019-05-30 11:00:00.000000000 +0800  最近改动:2019-05-30 16:25:25.496878027 +0800  创建时间:-  

好的,现在时间已经改为了 11:00:00 AM。

 实例十三:结合参数下使用-d选项

还可以用很酷的方式使用-d选项。请观察下面的图片

[root@Linuxidc tmp]# touch -d 'next tuesday GMT3' test3.txt   [root@Linuxidc tmp]# stat test3.txt     文件:"test3.txt"    大小:0             块:0          IO 块:4096   普通空文件  设备:803h/2051d    Inode:11044       硬链接:1  权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)  最近访问:2019-04-06 11:00:00.000000000 +0800  最近更改:2019-04-06 11:00:00.000000000 +0800  最近改动:2019-05-30 16:28:55.210247929 +0800  创建时间:-

我们可以将单词 next Sunday 和 GMT 3合成一个值,而 touch 命令仍然能识别它。

还有另一个-d选项的示例。 首先,我们要将file3_3.txt重置到当前日期和时间。
touch file_3.txt

[root@Linuxidc tmp]# touch test3.txt   [root@Linuxidc tmp]# stat test3.txt     文件:"test3.txt"    大小:0             块:0          IO 块:4096   普通空文件  设备:803h/2051d    Inode:11044       硬链接:1  权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)  最近访问:2019-05-30 16:30:36.173246795 +0800  最近更改:2019-05-30 16:30:36.173246795 +0800  最近改动:2019-05-30 16:30:36.173246795 +0800  创建时间:-

然后我们尝试这个命令:
touch -d '1 year ago 13:43:07' test3.txt

[root@Linuxidc tmp]# touch -d '1 year ago 13:43:07' test3.txt  [root@Linuxidc tmp]# stat test3.txt     文件:"test3.txt"    大小:0             块:0          IO 块:4096   普通空文件  设备:803h/2051d    Inode:11044       硬链接:1  权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)  最近访问:2018-11-30 13:43:07.000000000 +0800  最近更改:2018-11-30 13:43:07.000000000 +0800  最近改动:2019-05-30 16:31:30.369246185 +0800  创建时间:-

太震撼了,Touch 甚至能识别单词‘1 year ago’。

 实例十四:创建一个空文件

当你运行 touch 命令目标文件不存在时,touch 会创建一个同名的空文件。
touch test4.txt

[root@Linuxidc tmp]# touch test4.txt  [root@Linuxidc tmp]# ll test4.txt  -rw-r--r-- 1 root root 0 11月 30 16:34 test4.txt
 实例十五: 同时创建多个文件

你可以用空格将目标文件分割开来,以此来创建多个文件。
touch doc_10.txt doc_20.txt doc_30.txt

[root@Linuxidc tmp]# touch doc_10.txt doc_20.txt doc_30.txt  [root@Linuxidc tmp]# ll doc*  -rw-r--r-- 1 root root 0 11月 30 16:35 doc_10.txt  -rw-r--r-- 1 root root 0 11月 30 16:35 doc_20.txt  -rw-r--r-- 1 root root 0 11月 30 16:35 doc_30.txt

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

栗子博客 软件 Linux touch命令详述 https://www.lizi.tw/soft/14708.html

常见问题
  • 1、杰齐1.7仅适用于PHP5.2 2、需Zend支持 3、尽量使用宝塔面板 4、尽量使用Windows 系统,关关对Linux支持不太友好。
查看详情

相关文章

评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Linux touch命令详述-海报

分享本文封面