国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > touch 以获得更好的体验。

# Bash 的 touch 命令

touch [OPTION]... FILE...

!subtitle:功能

将文件的时间戳修改为当前时间,如果文件不存在则会创建一个空文件。

查看文件时间戳:

$ stat file.txt
Access: 2025-10-27 20:35:00
Modify: 2025-10-27 20:35:00
Change: 2025-10-27 20:35:00

!subtitle:类型

可执行文件(/usr/bin/rm),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • -a - 只修改文件的访问时间(access time)

    • -c, --no-create - 不创建文件

    • -d, --date=STRING - 指定时间(格式为 YYYY-MM-DD HH:MM:SS),而不是当前时间

    • -f -

    • -h, --no-dereference - 如果文件是符号链接,修改符号链接本身,而不是源文件

    • -m - 只修改文件的修改时间(modification time)

    • -r, --reference=FILE - 使用 FILE 文件的时间

    • -t STAMP - 指定时间(格式为 [[CC]YY]MMDDhhmm[.ss]),而不是当前时间

    • --time=WORD - 修改指定的时间类型;有效值:atime, mtime

    • --help - 显示帮助

    • --version - 显示版本

  • FILE - 文件列表

# 示例

!subtitle:更新已有文件的时间戳

$ touch existing.txt
  • 默认更新 访问时间 + 修改时间 为当前时间。

!subtitle:创建新文件(如果不存在)

$ touch file.txt
  • 如果 file.txt 不存在,则创建空文件。

!subtitle:不创建新文件(仅修改已有文件)

$ touch -c file.txt
  • 文件不存在时不创建

!subtitle:只更新访问时间(atime)

$ touch -a file.txt

!subtitle:只更新修改时间(mtime)

$ touch -m file.txt

!subtitle:设置指定时间

$ touch -t 202510271200 file.txt
  • 修改时间为 2025-10-27 12:00

$ touch --date="2025-10-27 12:00" file.txt
  • 使用可读时间字符串(GNU 扩展)

!subtitle:参考另一个文件的时间

$ touch -r file1 file2
  • file2 的时间和 file1 一样

$ touch -r file1 --time=atime file2   # 只修改访问时间
$ touch -r file1 --time=mtime file2   # 只修改修改时间
  • 可以指定修改哪个时间戳

!subtitle:修改多个文件的时间

$ touch file1.txt file2.txt file3.txt

# 推荐阅读

# 手册

TOUCH(1)                         User Commands                        TOUCH(1)

NAME
       touch - change file timestamps

SYNOPSIS
       touch [OPTION]... FILE...

DESCRIPTION
       Update  the  access  and modification times of each FILE to the current
       time.

       A FILE argument that does not exist is created empty, unless -c  or  -h
       is supplied.

       A  FILE  argument  string of - is handled specially and causes touch to
       change the times of the file associated with standard output.

       Mandatory arguments to long options are  mandatory  for  short  options
       too.

       -a     change only the access time

       -c, --no-create
              do not create any files

       -d, --date=STRING
              parse STRING and use it instead of current time

       -f     (ignored)

       -h, --no-dereference
              affect each symbolic link instead of any referenced file (useful
              only on systems that can change the timestamps of a symlink)

       -m     change only the modification time

       -r, --reference=FILE
              use this file's times instead of current time

       -t STAMP
              use [[CC]YY]MMDDhhmm[.ss] instead of current time

       --time=WORD
              change the specified time: WORD is access, atime, or use: equiv‐
              alent to -a WORD is modify or mtime: equivalent to -m

       --help display this help and exit

       --version
              output version information and exit

       Note that the -d and -t options accept different time-date formats.

DATE STRING
       The  --date=STRING  is  a mostly free format human readable date string
       such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29  16:21:42"  or
       even  "next Thursday".  A date string may contain items indicating cal‐
       endar date, time of day, time zone, day of week, relative  time,  rela‐
       tive date, and numbers.  An empty string indicates the beginning of the
       day.   The date string format is more complex than is easily documented
       here but is fully described in the info documentation.

AUTHOR
       Written by Paul Rubin, Arnold Robbins, Jim  Kingdon,  David  MacKenzie,
       and Randy Smith.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright  ©  2023  Free Software Foundation, Inc.  License GPLv3+: GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This is free software: you are free  to  change  and  redistribute  it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       Full documentation <https://www.gnu.org/software/coreutils/touch>
       or available locally via: info '(coreutils) touch invocation'

GNU coreutils 9.4                 April 2024                          TOUCH(1)
本文 更新于: 2025-11-27 09:38:16 创建于: 2025-11-27 09:38:16