1. touch命令
| 功能类别 | 具体作用 | 使用场景 |
|---|---|---|
| 创建文件 | 快速创建一个或多个空文件 | 初始化日志、配置、占位文件 |
| 更新时间戳 | 将文件的访问/修改时间改为当前时间 | 触发依赖时间戳的构建工具(如 Make) |
| 自定义时间戳 | 将文件时间设为任意指定时间 | 文件归档、时间同步、测试 |
最常用的两个选项:
-a:仅更新访问时间-m:仅更新修改时间-c或--no-create:不创建新文件(仅更新时间戳时非常有用)-t:使用指定时间戳,格式为[[CC]YY]MMDDhhmm[.ss]-d:使用更灵活的时间字符串,如-d "2024-01-01 10:30:00"
1. 基础文件操作
# 创建单个空文件
touch README.md
# 批量创建多个文件(支持通配符模式)
touch file{1..5}.txt # 创建 file1.txt 到 file5.txt
touch chapter_{a..c}.md # 创建 chapter_a.md, chapter_b.md, chapter_c.md
# 创建带空格/特殊字符的文件(必须用引号)
touch "我的 报告.txt"
touch '测试(最终版).doc'
2. 更新时间戳(关键用途)
# 更新文件的"修改时间"为现在(常用于触发构建)
touch -m config.yaml
# 只更新"访问时间"
touch -a log.txt
# 同时更新访问和修改时间(默认行为)
touch data.json
# 防止创建新文件,仅更新时间戳(文件不存在也不报错)
touch -c some_file
3. 自定义时间戳(高级用法)
# 设置为特定日期时间(格式:MMDDhhmm[.ss])
touch -t 202407151830.00 report.pdf # 2024年7月15日18:30:00
# 使用自然语言日期
touch -d "next Friday" deadline.txt
touch -d "2 days ago" old_file.log
# 复制另一个文件的时间戳
touch -r source_file.txt target_file.txt # 让target拥有和source相同的时间戳
评论区