> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man1/touch.1.md).

# touch.1

`touch` — 更改文件访问和修改时间

## 名称

`touch`

## 概要

`touch [-A [-][[hh]mm]SS] [-achm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] [-d YYYY-MM-DDThh:mm:SS[.frac][tz]] file`

## 描述

`touch` 实用程序设置文件的修改时间和访问时间。如果任何文件不存在，则以默认权限创建。

默认情况下，`touch` 同时更改修改时间和访问时间。可使用 `-a` 和 `-m` 标志单独选择访问时间或修改时间。同时选择两者等同于默认行为。默认情况下，时间戳设为当前时间。`-d` 和 `-t` 标志显式指定不同的时间，`-r` 标志指定将时间设为指定文件的时间。`-A` 标志按指定量调整值。

以下选项可用：

**`-A`** 按指定值调整文件的访问和修改时间戳。此标志用于修改时间戳设置错误的文件。参数格式为“\[-]\[\[hh]mm]SS”，其中每对字母表示如下：

**`-`** 使调整为负值：新时间戳设为旧时间戳之前。

**`hh`** 小时数，从 00 到 99。

**`mm`** 分钟数，从 00 到 59。

**`SS`** 秒数，从 00 到 59。

`-A` 标志隐含 `-c` 标志：如果指定的任何文件不存在，将静默忽略。

**`-a`** 更改文件的访问时间。除非同时指定了 `-m` 标志，否则不更改文件的修改时间。

**`-c`** 如果文件不存在则不创建。`touch` 实用程序不将此视为错误。不显示错误消息，退出值不受影响。

**`-d`** 将访问和修改时间更改为指定的日期时间，而非当前时间。参数格式为“YYYY-MM-DDThh:mm:SS\[.frac]\[tz]”，其中各字母表示如下：

**`YYYY`** 至少四位十进制数字表示年份。

**`MM`**, **`DD`**, **`hh`**, **`mm`**, **`SS`** 与 `-t` 时间相同。

**`T`** 字母 `T` 或空格为时间指示符。

**`.frac`** 可选小数部分，由句点或逗号后跟一位或多位数字组成。有效位数取决于内核配置和文件系统，可能为零。

**`tz`** 可选字母 `Z` 表示时间为 UTC。否则，假定时间为本地时间。本地时间受 `TZ` 环境变量的值影响。

**`-h`** 如果文件是符号链接，更改链接自身的时间，而非链接所指向文件的时间。注意，`-h` 隐含 `-c`，因此不会创建任何新文件。

**`-m`** 更改文件的修改时间。除非同时指定了 `-a` 标志，否则不更改文件的访问时间。

**`-r`** 使用指定文件的访问和修改时间，而非当前时间。

**`-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 到 60。

如果未指定“CC”和“YY”字母对，值默认为当前年份。如果未指定“SS”字母对，值默认为 0。

## 退出状态

`touch` 实用程序成功时退出 0，发生错误时退出 >0。

## 兼容性

支持 `touch` 的过时形式，即时间格式作为第一个参数指定。当未指定 `-r` 或 `-t` 选项、至少有两个参数且第一个参数是八位或十位数字字符串时，第一个参数被解释为“MMDDhhmm\[YY]”格式的时间规范。

“MM”、“DD”、“hh”和“mm”字母对的处理方式与指定给 `-t` 选项的对应项相同。如果“YY”字母对在 39 到 99 范围内，年份设为 1939 到 1999；否则，年份设为 21 世纪。

## 参见

utimensat(2)

## 标准

`touch` 实用程序预期为 IEEE Std 1003.2 ("POSIX.2") 规范的超集。

## 历史

`touch` 实用程序出现于 Version 7 AT\&T UNIX。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man1/touch.1.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
