Contents

Linux中Touch命令简介

1. 概述

在本教程中,我们将了解touch 命令。简而言之,这个命令允许我们更新文件或目录的最后修改时间和最后访问时间。

因此,我们将重点关注如何使用此命令及其各种选项。

请注意,我们使用 Bash 测试了此处显示的所有命令;但是,它们应该可以与任何符合 POSIX 标准的 shell 一起使用。

2. 默认行为

通过执行touch,文件系统上的一个或多个文件或目录被更新,使得它们的最后修改时间和最后访问时间被设置为当前系统时间。

所以,假设今天的日期是 2020 年 2 月 1 日,时间是早上 7:00。

如前所述,该命令将更新example-file.txt文件的两个时间戳:

# ls -l example-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Jan  1 20:00 example-file.txt
# touch example-file.txt
# ls -l example-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Feb  1 07:00 example-file.txt

2.1. 创建新文件

此外,当指定的文件不存在时,该命令将创建一个空文件并相应地设置时间:

# ls -l sample-file.txt
ls: sample-file.txt: No such file or directory
# touch sample-file.txt
# ls -l sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Feb  1 07:00 sample-file.txt

如果创建了一个新文件,新创建文件的权限将默认为给定文件系统的标准权限。

2.2. 具有多个文件或目录

如果指定了多个文件或目录,该命令将更新所有文件或目录的时间戳:

# ls -l example-file.txt sample-file.txt example-dir
drw-r--r--  1 blogdemo  blogdemo  0 Jan  1 22:00 example-dir
-rw-r--r--  1 blogdemo  blogdemo  0 Jan  1 20:00 example-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Jan  1 16:00 sample-file.txt
# touch example-file.txt sample-file.txt example-dir
# ls -l example-file.txt sample-file.txt example-dir
drw-r--r--  1 blogdemo  blogdemo  0 Feb  1 07:00 example-dir
-rw-r--r--  1 blogdemo  blogdemo  0 Feb  1 07:00 example-file.txt 
-rw-r--r--  1 blogdemo  blogdemo  0 Feb  1 07:00 sample-file.txt

3.定义特定时间

或者,我们可以使用*-t*选项定义要设置的时间戳。因此,这会更改使用系统时间的默认行为,而是使用选项设置指定的时间。使用此选项指定时间的格式是“ [[CC]YY]MMDDhhmm[.SS] ”:

  • CC:世纪
  • YY : 年份的后两位数;如果指定了YY但未指定CC ,则YY的值介于 69 和 99 之间会导致CC值为 19;否则,使用CC值 20
  • MM : 一年中的月份 (01-12)
  • DD : 一个月中的第几天 (01-31)
  • hh : 一天中的小时 (00-23)
  • mm : 小时 (00-59)
  • SS:分钟的第二个 (00-59)

因此,要使用我们定义的时间戳更新文件或目录,我们可以执行:

# ls -l sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Jan 20 19:00 sample-file.txt
 
# touch -t 202001262359.59 sample-file.txt
# ls -l sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Jan 26 23:59 sample-file.txt

当没有指定CCYY时,它们默认为当前年份:

# ls -l sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Jan 20 19:00 sample-file.txt
# touch -t 01282359.59 sample-file.txt
# ls -l sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Jan 28 23:59 sample-file.txt

如果未指定SS ,则该值默认为 0。

4.调整时间

默认行为的另一种替代方法是相对更改时间,而不是显式设置或使用系统时间。使用*-A*允许我们调整文件或目录相对于其当前时间戳的时间戳。用于指定时间调整的格式为“ [-][[hh]mm]SS ”:

  • -:使调整为负
  • hh : 小时数 (00-99)
  • mm : 分钟数 (00-59)
  • SS:秒数(00-59)

因此,要将访问时间调整 -25 小时,我们执行:

# ls -l sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Jan 27 22:59 sample-file.txt
# touch -A -250000 sample-file.txt
# ls -l sample-file.txt 
-rw-r--r--  1 blogdemo  blogdemo  0 Jan 26 21:59 sample-file.txt

5. 其他选项

5.1. 使用参考文件中的时间戳

-r选项使用来自指定参考文件的时间戳作为更新时要使用的时间戳。在这里,使用-r运行命令从example-file.txt获取时间戳并将它们应用于sample-file.txt

# ls -l example-file.txt sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Feb  1 17:00 example-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Jan 23 22:45 sample-file.txt
# touch -r example-file.txt sample-file.txt
# ls -l example-file.txt sample-file.txt
-rw-r--r-- 1 blogdemo blogdemo 0 Feb 1 17:00 example-file.txt 
-rw-r--r-- 1 blogdemo blogdemo 0 Feb 1 17:00 sample-file.txt

5.2. 使用符号链接

如果我们在指向文件或目录的符号链接上执行命令,链接目标的时间戳将更新:

# ls -l example-file.txt link-to-example-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Jan 23 22:00 example-file.txt
lrw-r--r--  1 blogdemo  blogdemo  0 Jan 23 22:30 link-to-example-file.txt -> example-file.txt
# touch example-file-link.txt
# ls -l example-file.txt link-to-example-file.txt
-rw-r--r--  1 blogdemo  blogdemo 0  Feb  1 07:00 example-file.txt
lrw-r--r--  1 blogdemo  blogdemo 0  Jan 23 22:30 link-to-example-file.txt -> example-file.txt

使用*-h*,我们可以更新指向目标文件或目录的符号链接的时间戳,而不是目标本身。

5.3. 防止文件创建

我们可以使用*-c*禁用文件创建。使用此选项执行时,如果文件不存在,则该命令不执行任何操作:

# ls -l sample-file.txt
ls: sample-file.txt: No such file or directory
# touch -c sample-file.txt
# ls -l sample-file.txt
ls: sample-file.txt: No such file or directory

5.4. 仅更新上次访问时间

如果我们只想更新文件或目录的最后访问时间,我们可以使用*-a*选项:

# ls -l sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Jan 20 19:00 sample-file.txt
# ls -l -u sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Jan 20 19:00 sample-file.txt  
# touch -a sample-file.txt
# ls -l sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Jan 20 19:00 sample-file.txt
# ls -l -u sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Feb  1 07:00 sample-file.txt

如前所述,默认行为是同时更新上次访问时间和上次修改时间。使用此选项会覆盖设置两个时间戳的默认行为。一起使用 -a 和 -m 会导致更新两个时间戳的默认行为。

5.5. 仅更新上次修改时间

同样,在我们只想更新文件或目录的最后修改时间的情况下,我们可以使用*-m* 选项:

# ls -l sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Jan 20 19:00 sample-file.txt
# ls -l -u sample-file.txt 
-rw-r--r--  1 blogdemo  blogdemo  0 Jan 20 19:00 sample-file.txt
# touch -m sample-file.txt
# ls -l sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Feb  1 07:00 sample-file.txt
# ls -l -u sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Jan 20 19:00 sample-file.txt

6.兼容性

为了在旧 BSD 系统上保持与touch 的向后兼容性,仍然支持一些过时的功能和选项。

该命令仍然支持旧的、过时的touch 版本。它允许我们首先以“ MMDDhhmm[YY] ”格式指定时间格式,后跟要在文件系统上更新的文件或目录的名称。

MMDDhhmm字母对被视为它们由*-t选项指定的对应物。如果YY*介于 39 和 99 之间,则年份为 20 世纪。否则,年份是 21 世纪:

# ls -l sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Feb 10 12:00 sample-file.txt
# touch 0123000020 sample-file.txt
# ls -l sample-file.txt
-rw-r--r--  1 blogdemo  blogdemo  0 Jan 23 00:00 sample-file.txt

该命令曾经能够使用*-f*选项强制更新文件。然而,这个选项现在被忽略了。