touch命令基础与语法介绍
touch 命令在 Linux 和 Unix 系统中主要用于两点:生成新的空文件或更新已有文件的时间戳信息。所谓更新时间戳,即刷新文件或目录的访问时间(atime)与修改时间(mtime)。
在使用该命令之前,了解其基本语法结构和可用参数选项是必要的。
基础语法结构如下:
# touch {选项} {文件}
以下是 touch 命令中常用的参数选项列表:

本文将深入解析 Linux 环境下 touch 命令的 9 个高频实用场景。
创建空文件与批量生成技巧
场景一:利用 touch 创建空文件
若需在 Linux 环境中新建一个空文件,只需在终端输入 touch 关键字后紧跟目标文件名即可。具体操作演示如下:
[root@linuxtechi ~]# touch devops.txt
[root@linuxtechi ~]# ls -l devops.txt
-rw-r--r--. 1 root root 0 Mar 29 22:39 devops.txt
场景二:批量生成多个空文件
在进行软件测试或环境初始化时,往往需要一次性创建大量空文件。借助 touch 命令的特性,可以高效完成这一任务:
[root@linuxtechi ~]# touch sysadm-{1..20}.txt
上述示例展示了如何快速生成从 sysadm-1.txt 到 sysadm-20.txt 共 20 个空文件。用户可根据实际需求自定义文件名前缀及数量范围。
独立修改访问时间与修改时间
场景三:单独修改文件或目录的访问时间
若仅需更新名为 devops.txt 文件的访问时间而不改变其他属性,可在命令中加入 -a 选项并指定文件名。操作方式如下:
[root@linuxtechi ~]# touch -a devops.txt
随后,调用 stat 命令检查文件详情,以确认访问时间是否已成功更新:
[root@linuxtechi ~]# stat devops.txt
File: 'devops.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 67324178 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2026-03-29 23:03:10.902000000 -0400
Modify: 2026-03-29 22:39:29.365000000 -0400
Change: 2026-03-29 23:03:10.902000000 -0400
Birth: -
对于目录而言,同样支持独立修改其访问时间:
例如,针对位于 /mnt 目录下的 nfsshare 文件夹,执行以下指令可更新其访问时间:
[root@linuxtechi ~]# touch -m /mnt/nfsshare/
[root@linuxtechi ~]# stat /mnt/nfsshare/
File: '/mnt/nfsshare/'
Size: 6 Blocks: 0 IO Block: 4096 directory
Device: fd00h/64768d Inode: 2258 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:mnt_t:s0
Access: 2026-03-29 23:34:38.095000000 -0400
Modify: 2026-03-03 10:42:45.194000000 -0500
Change: 2026-03-29 23:34:38.095000000 -0400
Birth: -
防止创建新文件的条件更新
场景四:仅更新时间戳而不创建新文件
在某些特定场景下,如果目标文件已存在,我们只想更新其访问时间;若文件不存在,则不希望自动创建它。此时应使用 -c 选项。该选项确保仅在文件存在时进行时间戳更新,若文件缺失则静默失败,不会新建文件。
[root@linuxtechi ~]# touch -c sysadm-20.txt
[root@linuxtechi ~]# touch -c winadm-20.txt
[root@linuxtechi ~]# ls -l winadm-20.txt
ls: cannot access winadm-20.txt: No such file or directory
场景五:修改文件和目录的修改时间
要更改文件或目录的修改时间(mtime),需配合 touch 命令的 -m 选项使用。
接下来演示如何修改 devops.txt 文件的修改时间:
[root@linuxtechi ~]# touch -m devops.txt
修改完成后,再次使用 stat 命令验证修改时间字段是否已变更:
[root@linuxtechi ~]# stat devops.txt
File: 'devops.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 67324178 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2026-03-29 23:03:10.902000000 -0400
Modify: 2026-03-29 23:59:49.106000000 -0400
Change: 2026-03-29 23:59:49.106000000 -0400
Birth: -
同理,该操作也适用于目录:
[root@linuxtechi ~]# touch -m /mnt/nfsshare/
通过交叉比对 stat 输出中的 Access 和 Modify 字段,可以清晰区分访问时间与其他时间的变化:
[root@linuxtechi ~]# stat devops.txt
File: 'devops.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 67324178 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2026-03-30 00:06:20.145000000 -0400
Modify: 2026-03-30 00:06:20.145000000 -0400
Change: 2026-03-30 00:06:20.145000000 -0400
Birth: -
设置特定日期与参考文件同步
场景七:将访问和修改时间设定为指定日期
默认情况下,直接使用 touch 命令会将当前系统时间写入文件的访问和修改时间字段。
若需将特定的历史或未来时间设置为文件的访问和修改时间,可以组合使用 -c 和 -t 选项来实现精准控制。
指定日期和时间时,需遵循以下格式规范:
{CCYY}MMDDhhmm.ss
各字段含义解析如下:
- CC – 年份的前两位数字
- YY – 年份的后两位数字
- MM – 月份 (01-12)
- DD – 天 (01-31)
- hh – 小时 (00-23)
- mm – 分钟 (00-59)
下面演示如何将 devops.txt 文件的访问和修改时间统一设置为未来的某个时间点,即 2026 年 10 月 19 日 18 时 20 分。
[root@linuxtechi ~]# touch -c -t 202610191820 devops.txt
执行完毕后,使用 stat 命令查看结果,确认访问和修改时间已按预期更新:

除了上述格式外,还可以使用 -d 选项配合人类可读的日期字符串来设置时间戳,命令格式为:touch -d "日期字符串" 文件名。具体示例如下:
[root@linuxtechi ~]# touch -c -d "2010-02-07 20:15:12.000000000 +0530" sysadm-29.txt
最后,通过 stat 命令验证文件的状态信息,确认时间戳应用成功:
[root@linuxtechi ~]# stat sysadm-20.txt
File: ‘sysadm-20.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 67324189 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2010-02-07 20:15:12.000000000 +0530
Modify: 2010-02-07 20:15:12.000000000 +0530
Change: 2026-03-30 10:23:31.584000000 +0530
Birth: -
注意:在上述命令中,如果省略了 -c 选项,且系统中不存在指定的文件,touch 命令将会创建一个新文件,并将其时间戳直接设置为命令中提供的值。
符号链接时间戳处理注意事项
场景八:基于参考文件设置时间戳 (-r)
touch 命令允许使用另一个文件作为参考源来复制时间戳。例如,若希望让 devops.txt 拥有与 sysadm-20.txt 完全相同的时间戳,可以使用 -r 选项轻松实现:
其语法结构为:
# touch -r {参考文件} 真正文件
[root@linuxtechi ~]# touch -r sysadm-20.txt devops.txt
场景九:在符号链接文件上更改时间戳
通常情况下,当尝试使用 touch 修改符号链接文件的时间戳时,系统默认会修改该链接指向的原始文件的时间戳。如果意图是直接修改符号链接本身的时间戳,则需要添加 -h 选项:
其语法结构为:
# touch -h {符号链接文件}
[root@linuxtechi opt]# ls -l /root/linuxgeeks.txt
lrwxrwxrwx. 1 root root 15 Mar 30 10:56 /root/linuxgeeks.txt -> linuxadmins.txt
[root@linuxtechi ~]# touch -t 203010191820 -h linuxgeeks.txt
[root@linuxtechi ~]# ls -l linuxgeeks.txt
lrwxrwxrwx. 1 root root 15 Oct 19 2030 linuxgeeks.txt -> linuxadmins.txt
以上即为 touch 命令的核心用法总结。希望这些实例能帮助您熟练掌握 Linux 文件时间戳的管理技巧。