如何使用touch - 命令示例

    1. 使用touch命令
      以最简单的形式,touch 命令可用于指定的文件以更新访问、修改和更改时间戳,我们将在下面看到。我们可以看到我们的 test.txt 示例文件自 9 月 2 日以来尚未被访问、修改或者更改。运行 touch 后,我们可以看到时间戳更新为当前时间,即 9 月 7 日。
[jack@onitroad ~]# stat test.txt
  File: ‘test.txt’
  Size: 69              Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 67191495    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2014-09-02 09:55:35.298208314 +1000
Modify: 2014-09-02 09:55:33.348208164 +1000
Change: 2014-09-02 09:55:33.348208164 +1000
 Birth: 
[jack@onitroad ~]# touch test.txt
[jack@onitroad ~]# stat test.txt
  File: ‘test.txt’
  Size: 69              Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 67191495    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2014-09-07 11:01:36.117150795 +1000
Modify: 2014-09-07 11:01:36.117150795 +1000
Change: 2014-09-07 11:01:36.117150795 +1000
 Birth: 
    1. 更改访问时间
      我们可以指定 -a 选项来仅修改访问时间,而不是更改所有时间戳。请注意,这也会更新更改时间,因为文件已更改。
[jack@onitroad ~]# stat test2.tar
  File: ‘test2.tar’
  Size: 10240           Blocks: 24         IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 68496231    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2014-09-02 15:47:33.940838420 +1000
Modify: 2014-09-02 15:47:33.940838420 +1000
Change: 2014-09-02 15:47:33.940838420 +1000
 Birth: 
[jack@onitroad ~]# touch -a test2.tar
[jack@onitroad ~]# stat test2.tar
  File: ‘test2.tar’
  Size: 10240           Blocks: 24         IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 68496231    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2014-09-07 11:04:55.267166167 +1000
Modify: 2014-09-02 15:47:33.940838420 +1000
Change: 2014-09-07 11:04:55.267166167 +1000
 Birth: 
  • 3.更改修改时间
    或者,我们可以指定 -m 来更新修改时间戳而不是访问时间。再次注意,这也会更新更改时间,因为文件已更改。
[jack@onitroad ~]# touch -m test2.tar
[jack@onitroad ~]# stat test2.tar
  File: ‘test2.tar’
  Size: 10240           Blocks: 24         IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 68496231    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2014-09-07 11:04:55.267166167 +1000
Modify: 2014-09-07 11:06:18.771172613 +1000
Change: 2014-09-07 11:06:18.771172613 +1000
 Birth: 
    1. 创建新文件
      如果我们在 touch 命令后指定的文件不存在,默认 touch 将创建一个新的 0 字节大小的空文件。
[jack@onitroad ~]# touch new-file-123
[jack@onitroad ~]# stat new-file-123
  File: ‘new-file-123’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 68496243    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2014-09-07 11:07:07.419176368 +1000
Modify: 2014-09-07 11:07:07.419176368 +1000
Change: 2014-09-07 11:07:07.419176368 +1000
 Birth: 
    1. 创建多个文件
      我们可以使用 touch 命令创建多个新文件,只需一个接一个地指定文件名。
[jack@onitroad files]# touch 1 2 3
[jack@onitroad files]# ls -l
total 0
-rw-r--r--. 1 root root 0 Sep  7 11:54 1
-rw-r--r--. 1 root root 0 Sep  7 11:54 2
-rw-r--r--. 1 root root 0 Sep  7 11:54 3

在 bash 的帮助下,我们可以非常快速地创建大量新文件。

[jack@onitroad files]# touch {a..e}
[jack@onitroad files]# ls -l
total 0
-rw-r--r--. 1 root root 0 Sep  7 11:57 a
-rw-r--r--. 1 root root 0 Sep  7 11:57 b
-rw-r--r--. 1 root root 0 Sep  7 11:57 c
-rw-r--r--. 1 root root 0 Sep  7 11:57 d
-rw-r--r--. 1 root root 0 Sep  7 11:57 e
    1. 不要创建新文件
      我们还可以选择使用 -c 选项阻止使用 touch 命令创建新文件。
[jack@onitroad ~]# touch -c this-does-not-exist
[jack@onitroad ~]# stat this-does-not-exist
stat: cannot stat ‘this-does-not-exist’: No such file or directory
    1. 使用时间字符串
      我们可以使用 -d 选项设置自定义时间戳,该选项采用给定的字符串,而不是使用当前时间。这可能很有用,因为它允许我们指定人类可读的格式。
[jack@onitroad ~]# touch test123 -d '20 Jun 2016'
[jack@onitroad ~]# stat test123
  File: ‘test123’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 68496245    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2016-06-20 00:00:00.000000000 +1000
Modify: 2016-06-20 00:00:00.000000000 +1000
Change: 2014-09-07 11:23:08.186250528 +1000
 Birth: 
    1. 设置具体时间
      与上面的 -d 选项类似,我们可以使用 -t 选项以 [[CC]YY]MMDDhhmm[.ss] 格式编辑文件上的时间戳
[jack@onitroad ~]# touch file -t 205001010000
[jack@onitroad ~]# stat file
  File: ‘file’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 68496244    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2050-01-01 00:00:00.000000000 +1100
Modify: 2050-01-01 00:00:00.000000000 +1100
Change: 2014-09-07 11:19:13.919232445 +1000
 Birth: 

在此示例中,我们创建了一个名为“file”的新文件,该文件的最后访问和修改时间设置为 2050 年的遥远未来。

    1. 使用参考文件
      我们可以使用 -r 选项读取另一个文件中存在的时间戳,而不是指定要应用于文件的自定义时间戳,然后是要复制时间戳的文件。
[jack@onitroad ~]# stat z.mp3
  File: ‘z.mp3’
  Size: 3407872         Blocks: 6656       IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 67183574    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2014-09-02 14:35:14.767503488 +1000
Modify: 2014-09-01 11:29:15.743511080 +1000
Change: 2014-09-01 11:29:15.743511080 +1000
 Birth: 
[jack@onitroad ~]# touch new -r z.mp3
[jack@onitroad ~]# stat new
  File: ‘new’
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: fd00h/64768d    Inode: 67191499    Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2014-09-02 14:35:14.767503488 +1000
Modify: 2014-09-01 11:29:15.743511080 +1000
Change: 2014-09-07 11:10:37.477192582 +1000
 Birth: 

在这个例子中,我们看到 z.mp3 文件最后一次访问是在 9 月 2 日,并在 9 月 1 日被修改。我们使用 -r 选项创建一个名为“new”的新文件,并确认创建后它具有与 z.mp3 文件相同的访问和修改时间。请注意,更改时间戳会更新为创建文件的时间。

    1. 显示版本
      我们可以使用 --version 选项打印出当前安装在我们系统上的 touch 版本。
[jack@onitroad ~]# touch --version
touch (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Paul Rubin, Arnold Robbins, Jim Kingdon,
David MacKenzie, and Randy Smith.
    1. 查看帮助手册
      最后,如果我们需要任何进一步的帮助,我们可以随时使用 --help 选项检查文档。请注意,为简洁起见,我没有包含此命令的完整输出。
[jack@onitroad ~]# touch --help
Usage: touch [OPTION]... FILE...
Linux touch命令示例

'touch' 命令用于修改现有文件的时间戳,或者创建新的空文件。

Touch 来自 GNU Coreutils 包 。

日期:2020-07-07 20:57:00 来源:oir作者:oir