触摸命令

  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

  [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

今天给各位介绍Linux touch命令详述的更多相关文章

  1. Linux Touch命令的8种使用技巧

    Linux touch命令不仅可以用于在Linux上创建空文件. 您可以使用它来更改现有文件的时间戳,包括其访问权限和修改时间. 本文介绍了8种可以通过Linux终端使用touch命令的方案. 我们在 ...

  2. Linux Touch命令的8种常见使用方法

    Linux touch命令不仅可以用于在Linux上创建空文件. 您可以使用它来更改现有文件的时间戳,包括其访问权限和修改时间. 本文介绍了8种可以通过Linux终端使用touch命令的方案. 我们在 ...

  3. Linux touch命令详解

    Linux touch命令 Linux touch命令用于修改文件或者目录的时间属性,包括存取时间和更改时间.若文件不存在,系统会建立一个新的文件. 用法: touch [-acfm][-d<日 ...

  4. 详细介绍Linux finger命令的使用

    Linux 允许多个用户使用不同的终端同时登陆,Linux finger命令为系统管理员提供知道某个时候到底有多少用户在使用这台Linux主机的方法,对于这个简单的命令我们还是先介绍一下再举例吧. L ...

  5. 详细介绍Linux telnet命令的使用

    对Linux系统进行远程登录,Linux telnet命令是必须得掌握的一个知识,虽然telnet并不是唯一的远程登录的方案,但是不可否认它是最常用的,所以很有必要详细了解Linux telnet命令 ...

  6. Linux -- touch 命令

    在Linux中,每个文件都关联一个时间戳,并且每个文件搜会存储最近一次访问的时间.最近一次修改的时间和最近一次变更的时间等信息.所以,无论何时我们创建一个新文件,访问或者修改一个已经存在的文件,文件的 ...

  7. Linux touch命令

    touch命令不常用,一般用于更改文件时间戳,或创建一个空文件 命令选项 -a:只更改访问时间 -c:--no-create 不创建任何文件 -d:--date=字符串 使用指定字符串表示时间而非当前 ...

  8. linux touch命令 创建文件

    touch 创建文件,用法,touch test.txt,如果文件存在,则表示修改当前文件时间 [root@MongoDB ~]# touch /data/text.txt [root@MongoDB ...

  9. C实现Linux之touch命令

    Linux 的 touch 命令用来修改文件时间戳,或者新建一个不存在的文件,使用方式如下: touch [-acm][-r ref_file(参照文件)|-t time(时间值)] file(文件名 ...

随机推荐

  1. SPSS数据记录的选择(Select Cases)

    SPSS数据记录的选择(Select Cases) 在数据分析时,有时可能只对某些记录感兴趣.例如,在判别分析时,可能用其中90%的记录数据建立判别函数,用其余10%的记录来考核判别函数.此时,可以通 ...

  2. 去掉Word 标题编号变成黑框

    问题: 在使用Word编写文档时,提前拟好的标题编号会突然变成黑框(黑色的方框,黑色的矩形),如下图 解决方案: 1.将光标定位到标题中,紧邻黑框的右侧 2.按键盘左方向键使方框变成黑色 3.按键盘的 ...

  3. 1.1两个char类型数据相加后,转化为int类型

    #include<stdio.h> main() { char a = 127; char i=0; char ai=0; ai= a+i; printf("size short ...

  4. Python3 From Zero——{最初的意识:008~初级实例演练}

    一.构显国际橡棋8x8棋盘 #!/usr/bin/env python3 #-*- coding:utf-8 -*- color_0="\033[41m \033[00m" col ...

  5. 剑指offer——26树的子结构

    题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构)   题解: 注意,所谓的子结构,是树的形状和值相同,并非判断B是不是A的一部分[如果是这样,那就是 ...

  6. 02->交互式图形学--用glut库实现Sierpinski镂垫

    Sierpinski图形是一类有趣的图形,本文结合交互式图形学教材,讲解Sierpinski镂垫图形生成,作为入门图形学编程的第一课.为了简便,本文就2D来进行讨论.实验环境见01 算法 主要是根据随 ...

  7. Maven + Springboot + redis 配置

    前言 刚进入到Java 开发的世界,对于小白Java的我来说,使用Maven + SpringBoot 的项目下启动redis: 第一步 本地安装Redis 服务 关于redis的教程链接 点击这里: ...

  8. (转)Google Protocol Buffer 的使用和原理

    转自:https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html   简介 什么是 Google Protocol Buffer? ...

  9. code+第四次网络赛div2

    T1 组合数问题: 用k个不完全相同的组合数表示一个数n. 用k-1个1和一个n-k+1表示即可. #include<cstdio> using namespace std; int x, ...

  10. mysql 数据库 内容的增删改查

    /*所有字段插入值*//*注意插入值数目要与字段值一致*/INSERT INTO student VALUES(1,'熊大','123','2019-10-18',1200);INSERT INTO ...