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

news/2024/10/16 2:24:57/

0 前言

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

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 referenced
                         file (useful only on systems that can change the
                         timestamps 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 -a
                           WORD is modify or mtime: equivalent to -m
      --help     display this help and exit
      --version  output version information and exit

Note 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'

1.1 touch命令的功能

 touch命令用于修改文件或者目录的时间属性,包括存取时间和更改时间。

如果指定的文件或目录不存在,则可以创建同名的空文件(取决于命令指定的选项)。

注意:这里的文件或目录,可以是一个文件或目录,也可以是一个文件集或目录集。

文件集的格式: 文件1 文件2 ……

目录集的格式: 目录1 目录2……

1.2 touch命令的格式

touch [选项]... 文件或目录...

 1.3 touch命令的选项说明

选项说明
-a改变文件或目录的读取时间记录

-c

--no-create

如果指定的文件或目录不存在,不创建同名的空文件

-d

--date=STRING

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

-h

--no-dereference

影响每个符号链接,而不是任何引用的文件

(仅在可以更改符号链接时间戳的系统上有用)

-m改变文件或目录的修改时间记录

-r

--reference=FILE

使用指定文件的时间记录
-t STAMP设定文件或目录的时间记录,格式是[[CC]YY]MMDDhhmm[.ss]
--time=WORD

更改指定时间:

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

WORD 是 modify 或 mtime:相当于 -m

--help显示帮助信息
--version显示版本信息

2 touch命令使用实例

2.1 touch 命令 创建空文件

purpleEndurer @ bash ~/test $ ls -l
total 0
purpleEndurer @ bash ~/test $ touch 1.txt
purpleEndurer @ bash ~/test $ ls -l
total 0
-rw-rw-r-- 1 csdn csdn 0 5月   2 22:26 1.txt
purpleEndurer @ bash ~/test $ 

我们先使用ls -l 命令 查看~/test 下没有文件

然后我们使用 touch 1.txt 修改文件1.txt的时间属性,由于文件1.txt不存在,于是创建了名为1.txt的文件。

我们再用使用ls -l 命令 查看 可以看到文件 1.txt。

2.2 touch 命令更新指定文件时间属性,如果指定文件不存在时不创建同名的空文件。

purpleEndurer @ bash ~/test $ ls -l
total 0
-rw-rw-r-- 1 csdn csdn 0 5月   2 22:26 1.txt
purpleEndurer @ bash ~/test $ touch -c 1.txt 2.txt
purpleEndurer @ bash ~/test $ ls -l
total 0
-rw-rw-r-- 1 csdn csdn 0 5月   2 22:41 1.txt
purpleEndurer @ bash ~/test $ 

先用 ls -l 命令查看,当前目录下只有文件1.txt

执行命令 touch -c 1.txt 2.txt 后 我们再用 ls -l 命令查看

可以看到 文件1.txt的时间属性已更新,而名为2.txt的文件原选并不存在,也没有被touch命令创建,这是因为我信指点定了 -c 选项。

2.3 用指定的时间更新指定文件的时间属性,指定文件不存在时则创建

purpleEndurer @ bash ~/test $ touch -d "2020-02-20 12:20" 2.txt
purpleEndurer @ bash ~/test $ ls -l
total 0
-rw-rw-r-- 1 csdn csdn 0 5月   2 22:59 1.txt
-rw-rw-r-- 1 csdn csdn 0 2月  20  2020 2.txt
purpleEndurer @ bash ~/test $ stat 2.txt
  File: ‘2.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 4ch/76d Inode: 1714885     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/    csdn)   Gid: ( 1000/    csdn)
Access: 2020-02-20 12:20:00.000000000 +0800
Modify: 2020-02-20 12:20:00.000000000 +0800
Change: 2024-05-02 23:15:34.397217850 +0800
 Birth: -

我们先使用命令touch -d "2020-02-20 12:20" 2.txt 来更新文件2.txt的时间属性,指定的时间为 2020-02-20 12:20

然后我们使用 ls -l命令查看当前目录下的文件信息,可以看到 文件2.txt 的时间属性是我们指定的2月20日。

接着我们使用stat 2.txt来查看2.txt的时间属性。

2.4 使用其它文件的时间属性来更新指定文件的时间属性。

purpleEndurer @ bash ~/test $ ls -l
total 0
-rw-rw-r-- 1 csdn csdn 0 5月   2 22:59 1.txt
-rw-rw-r-- 1 csdn csdn 0 2月  20  2020 2.txt
purpleEndurer @ bash ~/test $ touch -r 2.txt 1.txt
purpleEndurer @ bash ~/test $ ls -l
total 0
-rw-rw-r-- 1 csdn csdn 0 2月  20  2020 1.txt
-rw-rw-r-- 1 csdn csdn 0 2月  20  2020 2.txt

我们使用 命令 ls -l 查看当前目录下的文件 1.txt 和 2.txt的信息,两者的时间属性不同。

然后我们使用命令 touch -r 2.txt 1.txt,用文件2.txt的时间属性来更新文件1.txt的时间属性。

接着我们再次使用 命令 ls -l 查看当前目录下的文件 1.txt 和 2.txt的信息,可以看到文件1.txt的时间属性跟文件2.txt相同。


http://www.ppmy.cn/news/1450325.html

相关文章

golang垃圾回收

4.29 Golang中GC回收机制三色标记与混合写屏障 第14讲-总结哔哩哔哩bilibili 三个阶段 gc垃圾回收 早期是 GO1.3 标记清除  之前的标记清除&#xff1a;开始标记找到可达对象&#xff0c;并标记&#xff0c;标记完后清楚未标记的 较长时间的STW&#xff0c;使程序暂停…

ChatGPT的AI“记忆”可以记住付费客户的偏好

通过记住有关 ChatGPT Plus 订阅者的详细信息&#xff0c;OpenAI 的聊天机器人添加了更多个人助理风格的功能 OpenAI 在今年二月宣布了 “记忆 ”功能&#xff0c;该功能允许 ChatGPT 更永久地存储查询、提示和其他自定义功能。当时&#xff0c;只有 “一小部分 ”用户可以使用…

基于Unity+Vue通信交互的WebGL项目实践

unity-webgl 是无法直接向vue项目进行通信的&#xff0c;需要一个中间者 jslib 文件 jslib当作中间者&#xff0c;unity与它通信&#xff0c;前端也与它通信&#xff0c;在此基础上三者之间进行了通信对接 看过很多例子&#xff1a;介绍的都不是很详细&#xff0c;不如自己写&…

OpenCV在计算机视觉中的应用

OpenCV&#xff08;Open Source Computer Vision Library&#xff09;是一个广泛使用的开源计算机视觉库&#xff0c;旨在提供丰富的图像和视频处理功能。它最初由Intel于1999年开发&#xff0c;并演变成为一个全球性的开源项目&#xff0c;得到了众多开发者的贡献和支持。Open…

快讯! MySQL 8.4.0 LTS 发布(MySQL 第一个长期支持版本)

MySQL 第一个长期支持版本 8.4.0 LTS 发布&#xff0c;社区版下载地址&#xff1a; https://dev.mysql.com/downloads/mysql/ 功能变更 添加或更改的功能 组复制&#xff1a;与组复制相关的两个服务器系统变量的默认值已更改&#xff1a; 系统变量的默认值为 group_replication…

RustGUI学习(iced)之小部件(三):如何使用下拉列表pick_list?

前言 本专栏是学习Rust的GUI库iced的合集,将介绍iced涉及的各个小部件分别介绍,最后会汇总为一个总的程序。 iced是RustGUI中比较强大的一个,目前处于发展中(即版本可能会改变),本专栏基于版本0.12.1. 概述 这是本专栏的第三篇,主要讲述下拉列表pick_list部件的使用,会…

写文献综述常用的几种深度神经网络模型!

写文献综述常用的几种深度神经网络模型 卷积神经网络&#xff08;CNN&#xff09; 解释说明&#xff1a;专门用于处理图像和图像数据的深度学习模型。它通过卷积层、池化层等操作提取图像特征。应用&#xff1a;图像分类、目标检测、人脸识别等。未来改进&#xff1a;进一步提…

【学习vue 3.x】(二)组件应用及单文件组件

文章目录 章节介绍本章学习目标学习前的准备工作Vue.js文件下载地址 组件的概念及组件的基本使用方式组件的概念组件的命名方式与规范根组件局部组件与全局组件 组件之间是如何进行互相通信的父子通信父子通信需要注意的点 组件的属性与事件是如何进行处理的组件的属性与事件 组…