touch ¶
touch 命令常用案例 ¶
命令简介 ¶
touch 用于更新文件的访问时间和修改时间为当前时间。如果文件不存在,则创建一个空文件。
使用语法 ¶
touch [选项]... 文件...
特殊说明 ¶
- 如果文件不存在,默认会创建空文件(除非使用
-c或-h选项) - 参数
-会特殊处理,改变与标准输出关联的文件的时间戳 -d和-t选项接受不同的时间日期格式
选项说明 ¶
| 选项 | 说明 |
|---|---|
-a |
仅更改访问时间(atime) |
-c, --no-create |
不创建任何文件(文件不存在时不创建) |
-d, --date=字符串 |
使用指定时间字符串代替当前时间 |
-f |
忽略(兼容性保留) |
-h, --no-dereference |
影响符号链接本身,而非其指向的文件(仅支持修改符号链接时间戳的系统) |
-m |
仅更改修改时间(mtime) |
-r, --reference=文件 |
使用参考文件的时间,而非当前时间 |
-t 时间戳 |
使用指定时间戳,格式为 [[CC]YY]MMDDhhmm[.ss] |
--time=WORD |
更改指定时间: access、atime、use 等同于 -a modify、mtime 等同于 -m |
--help |
显示帮助信息 |
--version |
显示版本信息 |
常用案例 ¶
1. 创建空文件 ¶
touch file.txt
# 如果 file.txt 不存在,创建一个空文件
# 如果已存在,更新其时间戳为当前时间
2. 创建多个空文件 ¶
touch file1.txt file2.txt file3.txt
3. 不创建文件(仅更新已存在文件的时间戳) ¶
touch -c file.txt
# 如果 file.txt 不存在,不创建任何文件
4. 仅更改访问时间 ¶
touch -a file.txt
5. 仅更改修改时间 ¶
touch -m file.txt
6. 使用指定时间(-d 选项) ¶
# 使用特定日期
touch -d "2024-01-15 10:30:00" file.txt
# 使用相对时间
touch -d "yesterday" file.txt
touch -d "2 days ago" file.txt
touch -d "next week" file.txt
7. 使用时间戳(-t 选项) ¶
# 格式:[[CC]YY]MMDDhhmm[.ss]
touch -t 202401151030.30 file.txt
# 解释:2024年01月15日 10:30:30
8. 参考其他文件的时间 ¶
touch -r reference.txt file.txt
# 将 file.txt 的时间设置为与 reference.txt 相同
9. 同时更改访问时间和修改时间 ¶
touch -a -m file.txt
# 或简写
touch -am file.txt
10. 修改符号链接的时间戳(而非指向的文件) ¶
touch -h symlink.txt
11. 查看文件时间戳变化 ¶
# 创建文件前
ls -l file.txt # 显示修改时间
stat file.txt # 显示详细时间信息
# 创建或更新
touch file.txt
# 再次查看
ls -l file.txt
stat file.txt
12. 使用 --time 选项 ¶
# 只改访问时间
touch --time=access file.txt
touch --time=atime file.txt
# 只改修改时间
touch --time=modify file.txt
touch --time=mtime file.txt
实际工作场景案例 ¶
1. 批量创建空日志文件 ¶
touch log_{1..10}.log
# 创建 log_1.log 到 log_10.log 共10个文件
2. 在脚本中创建锁文件 ¶
touch /var/run/myapp.lock
3. 强制更新文件时间戳,触发某些程序重新处理 ¶
touch config.conf
# 某些应用(如 Make)会检查文件时间戳,更新时间戳可触发重新编译
4. 创建项目必需的占位文件 ¶
touch .gitignore README.md LICENSE
5. 确保目录存在结构 ¶
# 创建目录并确保某个文件存在
mkdir -p /var/log/myapp
touch /var/log/myapp/app.log
6. 批量更新所有文件时间戳 ¶
# 当前目录所有文件
touch *
# 特定类型文件
touch *.log *.txt
7. 检查文件是否存在(静默创建) ¶
touch -c file.txt && echo "文件已存在或已更新"
8. 使用当前时间以外的固定时间 ¶
# 设置为2024年1月1日
touch -t 202401010000.00 file.txt
# 使用相对时间
touch -d "2024-01-01" file.txt
时间戳说明 ¶
| 时间戳类型 | 说明 | 查看命令 |
|---|---|---|
| 访问时间 (atime) | 文件最后被读取的时间 | stat file.txt |
| 修改时间 (mtime) | 文件内容最后被修改的时间 | ls -l file.txt |
| 更改时间 (ctime) | 文件元数据最后被修改的时间 | stat file.txt |
注意:touch 默认会更新 atime 和 mtime,但 ctime 会自动更新(无法手动设置)。
常用组合 ¶
| 需求 | 命令 |
|---|---|
| 创建文件,不更新已存在文件的任何时间 | touch -c file.txt |
| 只改访问时间 | touch -a file.txt |
| 只改修改时间 | touch -m file.txt |
| 使用特定时间 | touch -d "2024-01-01 12:00" file.txt |
| 复制时间戳 | touch -r source.txt dest.txt |
| 创建多个文件 | touch file{1..100}.txt |
注意事项 ¶
1. 时间戳需要 root 权限 ¶
修改其他用户的文件时间戳可能需要 sudo:
sudo touch /root/file.txt
2. 文件系统限制 ¶
某些文件系统(如挂载时使用 noatime 参数)可能不会更新访问时间。
3. 空文件创建 ¶
touch 创建的是空文件(0 字节),不是目录。
4. 通配符使用 ¶
touch *.txt # 更新所有 .txt 文件的时间戳,如果没有任何 .txt 文件会报错
touch *.txt 2>/dev/null # 忽略错误提示
5. 与重定向创建文件的区别 ¶
# 创建空文件
touch file.txt
# 通过重定向创建(也会创建空文件)
> file.txt
# 区别:重定向会清空已有文件,touch 保留内容只更新时间戳