今天给各位介绍Linux touch命令详述

今天给各位介绍Linux touch命令详述,第1张

概述  触摸命令   touch命令有两个功能:一个是将现有文件的时间戳更新为系统的当前时间(默认模式),它们的数据将保持不变;第二个是创建一个新的空文件。   语法   触摸(可选)   最常见的用法:touch fileA(http://iask.sina.com.cn/b/1IexC3QGhGeh.html)   - 如果fileA存在,请使用touch命令更改此文件或目录的日期和时间,包括访问

  触摸命令

  touch命令有两个功能:一个是将现有文件的时间戳更新为系统的当前时间(默认模式),它们的数据将保持不变;第二个是创建一个新的空文件。

  语法

  触摸(可选)

  最常见的用法:touch fileA(http://iask.sina.com.cn/b/1IexC3QGhGeh.html)

  - 如果fileA存在,请使用touch命令更改此文件或目录的日期和时间,包括访问时间和更改时间;

  - 如果fileA不存在,touch命令将在当前目录中创建一个新的空白文件fileA。

  参数

  参数说明

  -a仅更改访问时间。

  -c不创建任何文档。

  -d使用指定的日期和时间,而不是当前时间。 [[CC] YY] MMDD文字

  -f此参数将被忽略,不会被处理。它仅负责解决BSD版本touch命令的兼容性问题。

  -m仅更改更改时间。

  -r将指定文档或目录的日期和时间设置为与参考文档或目录相同的日期和时间。

  -t使用指定的日期和时间而不是当前时间[CC [YY] MMDDhhmm [.SS]

  --help在线帮助

  --version显示版本信息。

  使用范例:

  实例一:创建不存在的文件

  touch test1.txt test2.txt

  [[email protected] tmp]# touch test1.txt test2.txt

  [[email protected] 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

  [[email protected] tmp]# touch -c test3.txt

  [[email protected]uxIDc 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

  [[email protected] 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

  [[email protected] tmp]# touch test1.txt test3.txt

  [[email protected] 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

  [[email protected] tmp]#

  实例四:设定文件的时间戳

  touch -t 201808080808 test1.txt

  [[email protected] tmp]# ll test1.txt

  -rw-r--r-- 1 root root 0 Nov 30 14:58 test1.txt

  [[email protected] tmp]# touch -t 201808080808 test1.txt

  [[email protected] 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

  [[email protected] 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

  [[email protected] tmp]# touch -r test1.txt test2.txt

  [[email protected] 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

  [[email protected] tmp]# touch -d 20161013 *.txt

  [[email protected] 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

  请观察下面的一张截图。

  [[email protected] tmp]# ll

  总用量 0

  -rw-r--r-- 1 root root 0 10月 13 00:00 test1.txt

  [[email protected] tmp]# touch test1.txt

  [[email protected] 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。我们可以看更多的细节。

  [[email protected] 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

  [[email protected] tmp]# touch -a test1.txt

  [[email protected] 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

  [[email protected] tmp]# touch -m test1.txt

  [[email protected] 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

  [[email protected] tmp]# touch -t 201701120958 test1.txt

  [[email protected] 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 的文件,它的相关属性在下面的截图中展示了。

  [[email protected] 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

  [[email protected] tmp]# touch -d ‘10-December-2013‘ test3.txt

  [[email protected] 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

  [[email protected] tmp]# touch test3.txt

  [[email protected] 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

  [[email protected] tmp]# touch -d GMT3 test3.txt

  [[email protected] 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选项。请观察下面的图片。

  [[email protected] tmp]# touch -d ‘next tuesday GMT3‘ test3.txt

  [[email protected] 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

  [[email protected] tmp]# touch test3.txt

  [[email protected] 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

  [[email protected] tmp]# touch -d ‘1 year ago 13:43:07‘ test3.txt

  [[email protected] 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

  [[email protected] tmp]# touch test4.txt

  [[email protected] 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

  [[email protected] tmp]# touch doc_10.txt doc_20.txt doc_30.txt

  [[email protected] 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

总结

以上是内存溢出为你收集整理的今天给各位介绍Linux touch命令详述全部内容,希望文章能够帮你解决今天给各位介绍Linux touch命令详述所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1024922.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存