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

Linux shell编程学习笔记60:touch命令

0 前言

在csdn技能树Linux入门的练习题中,touch是最常见的一条命令。这次我们就来研究它的用法。

1 touch命令的功能、格式和选项说明

我们可以使用touch --help命令查看touch命令的帮助信息。

[purpleendurer @ bash ~ ]touch --help
Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.A FILE argument that does not exist is created empty, unless -c or -h
is supplied.A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.Mandatory arguments to long options are mandatory for short options too.-a                     change only the access time-c, --no-create        do not create any files-d, --date=STRING      parse STRING and use it instead of current time-f                     (ignored)-h, --no-dereference   affect each symbolic link instead of any referencedfile (useful only on systems that can change thetimestamps of a symlink)-m                     change only the modification time-r, --reference=FILE   use this file's times instead of current time-t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time--time=WORD        change the specified time:WORD is access, atime, or use: equivalent to -aWORD is modify or mtime: equivalent to -m--help     display this help and exit--version  output version information and exitNote that the -d and -t options accept different time-date formats.GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report touch translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'touch invocation'
[purpleendurer @ bash ~ ]

1.1 touch命令的功能

touch命令可以将指定文件的访问时间或修改时间更新为当前时间,

如果指定的文件不存在,touch命令也可以创建出新文件。

1.2 touch命令的格式

touch [选项] 文件1 [[文件2] ……]

1.3  touch命令的选项

选项功能
-a只更改访问时间

-c

--no-create

不创建文件

-d=STRING

--date=STRING

解析 STRING 并使用它来代替当前时间
-f可以忽略不使用,是为了与其他 unix 系统的相容性而保留

-h

--no-dereference

影响每个符号链接,而不是任何引用的文件(仅在可以更改符号链接时间戳的系统上有用)
-m仅更改修改时间

-r=FILE

--reference=FILE

使用指定文件的时间而不是当前时间
-t STAMP

使用 [[CC]YY]MMDDhhmm[.ss] 代替当前时间

其中:

CC 为年份前两位数字

YY 为年份后两位数字

MM 为月份

DD 为日

hh 为小时

mm 为分钟

ss 为秒数

--time=WORD

更改指定时间:

WORD 是 access、atime 或 use:等价于 -a

WORD 是 modify 或 mtime:相当于 -m

--help显示帮助信息并退出
--version输出版本信息并退出

 2 touch命令实例

2.1 touch test1.txt : 创建文件test1.txt

[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月   3  2021 Code
[purpleendurer @ bash ~ ]touch test1.txt
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月   3  2021 Code
-rw-rw-r-- 1 csdn csdn    0 6月  29 18:25 test1.txt
[purpleendurer @ bash ~ ]

我们先使用命令 ls -l 查看当前目录的内容,当前目录中没有文件test1.txt。

所以我们执行命令touch test1.txt时,会自动创建这个文件。

2.2 touch -t 198001020304.05 test1.txt :将文件test1.txt的访问或修改时间改为 1980年1月2日3点4分05秒

[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月   3  2021 Code
-rw-rw-r-- 1 csdn csdn    0 6月  29 18:34 test1.txt
[purpleendurer @ bash ~ ]touch -t 198001020304.05 test1.txt
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月   3  2021 Code
-rw-rw-r-- 1 csdn csdn    0 1月   2  1980 test1.txt
[purpleendurer @ bash ~ ]

我们先使用命令 ls -l 查看当前目录的内容,其中test1.txt的日期是6月29日 18:34。

接着我们执行命令touch -t 198001020304.05 test1.txt

再使用命令 ls -l 查看当前目录的内容,其中test1.txt的日期是已变成1980年1月2日 。

2.3 touch --r=test1.txt test2.txt :将文件test2.txt的访问或修改时间改为 test1.txt的时间

[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月   3  2021 Code
-rw-rw-r-- 1 csdn csdn    0 1月   2  1980 test1.txt
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月   3  2021 Code
-rw-rw-r-- 1 csdn csdn    0 1月   2  1980 test1.txt
[purpleendurer @ bash ~ ]touch test2.txt
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月   3  2021 Code
-rw-rw-r-- 1 csdn csdn    0 1月   2  1980 test1.txt
-rw-rw-r-- 1 csdn csdn    0 6月  29 18:38 test2.txt
[purpleendurer @ bash ~ ]touch --r=test1.txt test2.txt
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月   3  2021 Code
-rw-rw-r-- 1 csdn csdn    0 1月   2  1980 test1.txt
-rw-rw-r-- 1 csdn csdn    0 1月   2  1980 test2.txt
[purpleendurer @ bash ~ ]

我们先使用命令 ls -l 查看当前目录的内容,其中文件test1.txt的日期是1980年1月2日,文件test2.txt的日期是6月29日 18:38。

接着我们执行命令touch --r=test1.txt test2.txt

再使用命令 ls -l 查看当前目录的内容,其中test2.txt的日期已变成1980年1月2日,跟test1.txt一样。

2.4 touch --date="2022-01-01 12:00:00" test2.txt :将文件test2.txt的时间改为 2022年1月1日

[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月   3  2021 Code
-rw-rw-r-- 1 csdn csdn    0 6月  29 18:55 test1.txt
-rw-rw-r-- 1 csdn csdn    0 6月  29 18:55 test2.txt
[purpleendurer @ bash ~ ]touch -d="2022-01-01 12:00:00" test2.txt
touch: invalid date format ‘=2022-01-01 12:00:00’
[purpleendurer @ bash ~ ]touch --date="2022-01-01 12:00:00" test2.txt
[purpleendurer @ bash ~ ]ls -l
total 4
drwxr-xr-x 2 csdn csdn 4096 8月   3  2021 Code
-rw-rw-r-- 1 csdn csdn    0 6月  29 18:55 test1.txt
-rw-rw-r-- 1 csdn csdn    0 1月   1  2022 test2.txt
[purpleendurer @ bash ~ ]

我们先使用命令 ls -l 查看当前目录的内容,其中文件test2.txt的日期是6月29日 18:55。

接着我们执行命令touch --date="2022-01-01 12:00:00"

再使用命令 ls -l 查看当前目录的内容,其中test2.txt的日期已变成2022年1月1日。

相关文章:

  • MySQL高级-SQL优化- count 优化 - 尽量使用count(*)
  • 日常生活中应用广泛的长度单位(知道了解即可)
  • 安卓手机软件自动运行插件的开发流程及代码科普!
  • 【高考志愿】冶金工程
  • 深入解析Android DEX文件及其优化策略
  • uniapp横屏移动端卡片缩进轮播图
  • Qt | 2D 时钟设计
  • 基于uniapp(vue3)H5附件上传组件,可限制文件大小
  • Django QuerySet对象,all()方法
  • vue3+vite+nodejs,通过接口的形式请求后端打包(可打包全部或指定打包组件)
  • web的学习和开发
  • 面试专区|【40道移动端测试高频题整理(附答案背诵版)】
  • Android 生成 AAR 包
  • 论文学习_UVSCAN: Detecting Third-Party Component Usage Violations in IoT Firmware
  • 硬件工程师干了一年,公司无效卷,怎么破?
  • @angular/forms 源码解析之双向绑定
  • 【108天】Java——《Head First Java》笔记(第1-4章)
  • Apache Pulsar 2.1 重磅发布
  • IDEA 插件开发入门教程
  • javascript数组去重/查找/插入/删除
  • Perseus-BERT——业内性能极致优化的BERT训练方案
  • python 学习笔记 - Queue Pipes,进程间通讯
  • springMvc学习笔记(2)
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 基于 Ueditor 的现代化编辑器 Neditor 1.5.4 发布
  • 计算机常识 - 收藏集 - 掘金
  • 坑!为什么View.startAnimation不起作用?
  • 七牛云 DV OV EV SSL 证书上线,限时折扣低至 6.75 折!
  • 前端技术周刊 2019-01-14:客户端存储
  • 我与Jetbrains的这些年
  • 学习使用ExpressJS 4.0中的新Router
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • #LLM入门|Prompt#3.3_存储_Memory
  • #每天一道面试题# 什么是MySQL的回表查询
  • (附源码)ssm高校实验室 毕业设计 800008
  • (三)mysql_MYSQL(三)
  • (四)Android布局类型(线性布局LinearLayout)
  • (心得)获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列。
  • (循环依赖问题)学习spring的第九天
  • (转)大型网站架构演变和知识体系
  • .Net 8.0 新的变化
  • .NET 材料检测系统崩溃分析
  • .NET 中小心嵌套等待的 Task,它可能会耗尽你线程池的现有资源,出现类似死锁的情况
  • .net解析传过来的xml_DOM4J解析XML文件
  • .net生成的类,跨工程调用显示注释
  • @RequestMapping用法详解
  • [20170728]oracle保留字.txt
  • [BZOJ] 2427: [HAOI2010]软件安装
  • [C++数据结构](22)哈希表与unordered_set,unordered_map实现
  • [CF226E]Noble Knight's Path
  • [HTML]Web前端开发技术12(HTML5、CSS3、JavaScript )——喵喵画网页
  • [Interview]Java 面试宝典系列之 Java 多线程
  • [LeetCode] 596:超过5名学生的课
  • [LeetCode][面试算法]逻辑闭环的二分查找代码思路