Linux 常用命令 - touch 【创建空文件与修改时间戳】

简介

touch 命令源自英文单词 “touch”,本意为 “触摸、接触”。在 Linux 系统中,该命令主要用于创建空文件或更新文件的访问和修改时间。touch 命令不打开文件,仅更改文件的时间戳,如果指定的文件不存在,它会创建一个新的空文件。

使用方式

touch [OPTION]... FILE...

常用参数

  • -a:只更新文件的访问时间。

  • -c:不创建新的文件。

  • -d,--date=STRING:将文件时间更新为指定的日期时间,而非当前的时间。

  • -h, --no-dereference:在处理符号链接(symlink)时,不要解引用符号链接,而是直接操作符号链接本身。该选项仅在能够修改符号链接时间戳的系统下有效。

  • -m:只更新文件的修改时间。

  • -r, --reference=FILE:将指定文件或目录的日期时间设置成给定参考文件或目录的日期时间。

  • -t STAMP:STAMP为特定格式的时间:[[CC]YY]MMDDhhmm[.ss],将文件时间更新为指定的日期时间,而非当前的时间。

  • --time=WORD:更新指定的时间,WORD 可以为:accessatimeuse ,这些等同于 -a 选项。或 modifymtime,这些等同于 -m 选项。

  • --help:显示帮助信息。

  • --version:显示版本信息。

日期字符串

touch 命令的 -d 或者 --date 选项后可以跟一个字符串来表示一个特定时间,而这个字符串是比较灵活且易懂的,比如下面几种:

“Sun, 29 Feb 2004 16:21:42 -0800”

“2004-02-29 16:21:42”

“next Thursday”

一般来说,日期字符串中可以包含以下几种元素:

  • 日历时间:具体的年、月、日信息。

  • 一天中的时间:具体的时、分、秒信息。

  • 时区:表示时间相对于 UTC 的偏移量,比如 -0800 表示比 UTC 时间早8个小时。

  • 星期几:具体一周中的某一天。

  • 相对时间:如几个小时前,几分钟后等。

  • 相对日期:如 “next Thursday” 表示下一个周四。

  • 数字:可以是表示日期或时间的任何数字。

如果日期字符串为空,它表示的是那一天开始的时刻。完整的日期字符串说明可以在如下网址查看:
https://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html

参考示例

1. 创建一个指定名称的空文件

touch test.txt

2. 结合通配符,创建多个文件

jay@jaylinuxlenovo:~/test/touch$ touch File{1..5}.txt
jay@jaylinuxlenovo:~/test/touch$ ls
File1.txt  File2.txt  File3.txt  File4.txt  File5.txt

3. 更新指定文件的访问及修改时间

jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt 
-rw-rw-r-- 1 jay jay 0 Dec 31 14:38 File1.txt
jay@jaylinuxlenovo:~/test/touch$ touch File1.txt 
jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt 
-rw-rw-r-- 1 jay jay 0 Dec 31 14:44 File1.txt

更新前时间为 14:38 ,更新后为 14:44 。

4. 使用特定的时间戳格式更新文件的时间

jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt 
-rw-rw-r-- 1 jay jay 0 Dec 31 14:44 File1.txt
jay@jaylinuxlenovo:~/test/touch$ touch -t 202201010130 File1.txt 
jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt 
-rw-rw-r-- 1 jay jay 0 Jan  1  2022 File1.txt

5. 使用更易懂的时间字符串更新文件时间

jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt 
-rw-rw-r-- 1 jay jay 0 Dec 31 14:48 File1.txt
jay@jaylinuxlenovo:~/test/touch$ touch -d '2023-12-31 12:12' File1.txt 
jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt 
-rw-rw-r-- 1 jay jay 0 Dec 31 12:12 File1.txt

6. 将指定文件的时间更新为参考文件的时间

jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt  File2.txt 
-rw-rw-r-- 1 jay jay 0 Dec 31 12:12 File1.txt
-rw-rw-r-- 1 jay jay 0 Dec 31 14:38 File2.txt
jay@jaylinuxlenovo:~/test/touch$ touch -r File2.txt File1.txt 
jay@jaylinuxlenovo:~/test/touch$ ls -l File1.txt  File2.txt 
-rw-rw-r-- 1 jay jay 0 Dec 31 14:38 File1.txt
-rw-rw-r-- 1 jay jay 0 Dec 31 14:38 File2.txt

注意事项

  • 使用 touch 时,如果指定的文件不存在,系统会创建一个新的空文件。这可能导致在不期望的位置创建文件。

  • 运行 touch 命令需要有足够的权限来在指定目录下创建或修改文件。如果没有相应权限,命令会失败。

  • 当使用 -d 参数设置特定日期时,日期格式需要符合系统的日期格式要求。

  • 使用 -c 参数时,如果文件不存在,touch 不会创建新文件,这对于仅希望更新已存在文件的时间戳的场景非常有用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WKJay_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值