touch -r:把一个文件的时间戳复制给另一个

Linux 上用 touch -r 将参考文件的修改时间同步到目标文件。

Linux文件时间戳touch 创建文件低风险
执行 touch -r 参考文件 目标文件,即可让目标文件的访问和修改时间与参考文件一致。

01命令

命令
$ touch -r reference.dat build/artifact.stamp

02作用说明

容易被遗忘的 -r 选项让 touch 以现有文件作为时间来源,而不是当前时间。它常用于让生成的文件看起来与源文件同期(可复现构建、make 的 stamp 文件),配合 -c 还能在目标不存在时不默默创建。也可以用 -d/-t 直接指定时间,但 -r 完全不用解析日期格式,最省心。

03风险

风险低风险
!注意: 目标文件不存在时 touch 会创建它;不希望这样请加上 -c。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • touch(1P) — POSIX manual page
    man7.org · technical · 2026-09-17
    依据: POSIX documents touch creating files when absent and updating access/modification times
  • touch(1) — Arch Linux manual
    Arch Linux · technical · 2026-09-17
    依据: -r, --reference=FILE: use this file's times instead of current time; -c do not create missing files

最后验证: 2026-09-17 · 分类: 文件与文件夹

06相关方案