当前位置: 首页 > article >正文

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 不会创建新文件,这对于仅希望更新已存在文件的时间戳的场景非常有用。


http://www.kler.cn/a/472054.html

相关文章:

  • 【C语言】可移植性陷阱与缺陷(八): 随机数的大小
  • 如何安装和配置PHP开发环境?
  • apex安装
  • crawl4ai 大模型友好格式输入爬虫框架
  • LLM架构从基础到精通之NLP基础1
  • Java-基于Redisson的Redis工具类RedissonUtils
  • WebSocket 性能优化:从理论到实践
  • 51单片机——中断(重点)
  • 《空舞的巨兽》官方学习版
  • 批量写入数据到数据库,卡顿怎么解决
  • 根据状态修改圆锥扩散材质并实现扩散效果【Mars3d】
  • 百度Android面试题及参考答案 (下)
  • unity学习14:unity里的C#脚本的几个基本生命周期方法, 脚本次序order等
  • 使用 Conda创建新的环境遇到的问题
  • Vue3 + Vite + Electron + Ts 项目快速创建
  • 基于python的网络爬虫爬取天气数据及可视化分析(Matplotlib、sk-learn等,包括ppt,视频)
  • [项目实战2]贪吃蛇游戏
  • linux下绑host
  • jenkins入门13--pipeline
  • 单片机串口控制