touch 是 Linux 中用于创建空文件或更新文件时间戳的常用命令。以下是详细用法和示例:
bash
touch filename.txt| 选项 | 作用 | 示例 |
|---|---|---|
-a |
仅更新访问时间(atime) | touch -a file.txt |
-m |
仅更新修改时间(mtime) | touch -m file.txt |
-c |
不创建新文件(仅更新时间戳) | touch -c file.txt |
-d |
指定时间(而非当前时间) | touch -d "2023-10-01 12:00" file.txt |
-r |
复制其他文件的时间戳 | touch -r ref_file.txt target.txt |
-t |
自定义时间格式([[CC]YY]MMDDhhmm[.ss]) |
touch -t 202310011200.30 file.txt |
bash
touch file1.txt file2.txt file3.txtlog{1..5}.txt):
bash
touch log{1..5}.txtbash
touch -c non_existent_file.txt # 文件不存在时不会创建bash
touch -d "2024-01-01 08:30" holiday.txtbash
touch -r source_file.txt target_file.txt # 将target时间同步为source的时间-t 时,时间格式为:[[CC]YY]MMDDhhmm[.ss]202310011200.30 表示 2023年10月1日 12:00:30。用 stat 命令查看文件时间戳:
stat filename.txt
输出示例:
Access: 2023-10-01 12:00:00.000000000 +0800
Modify: 2023-10-01 12:00:00.000000000 +0800
. 开头可创建隐藏文件(如 .config)。-h 选项(但多数系统忽略此选项)。通过灵活组合选项,touch 可以高效管理文件时间戳或快速生成空文件。