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

embedded/2024/10/16 2:31:06/

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/embedded/30023.html

相关文章

【Flask 系统教程 1】入门及配置

当你开始学习 Flask 时&#xff0c;了解如何进行基本的配置是非常重要的。Flask 是一个简单而灵活的 Python Web 框架&#xff0c;它允许你快速构建 Web 应用程序&#xff0c;并且易于学习。在这篇博客中&#xff0c;我将介绍如何从零开始进行 Flask 的基础配置&#xff0c;适合…

在idea中连接mysql

IDE&#xff08;集成开发环境&#xff09;是一种软件应用程序&#xff0c;它为开发者提供编程语言的开发环境&#xff0c;通常集成了编码、编译、调试和运行程序的多种功能。一个好的IDE可以大幅提高开发效率&#xff0c;尤其是在进行大型项目开发时。IDE通常包括以下几个核心组…

TCP/IP网络模型各层简介

第一张图是OSI参考模型&#xff0c;由国际标准组织ISO定义的理论模型。 第二张则是TCP/IP协议的网络模型&#xff0c;实际互联网所采用的网络协议族的基础架构&#xff0c;工作中也一般都是接触这个。 第三章是学习时一般使用的网络模型&#xff0c;尤其是《计算机网络-自顶向…

等级保护测评一般多长时间能做完?

一个二级或三级的系统&#xff0c;整体持续周期一到两个月 具体时间还要根据信息系统数量&#xff0c;及信息系统的规模&#xff0c;以及测评方与被测方的配合情况等&#xff0c;有所增减。 现场测评周期一般一周左右 小规模安全整改&#xff0c;包括管理制度策略配置技术&a…

WPF之自定义绘图

1&#xff0c;创建自定义控件类 class CustomDrawnElement:FrameworkElement{public static readonly DependencyProperty BackgroundColorProperty;static CustomDrawnElement(){FrameworkPropertyMetadata meta new FrameworkPropertyMetadata(Colors.SkyBlue);meta.Affects…

C语言——小知识和小细节17

一、未能给指针成功赋值 #include <stdio.h> #include <stdlib.h> #include <string.h>void GetMemory(char* p) {p (char*)malloc(20 * sizeof(char)); }void Test() {char* str NULL;GetMemory(str);strcpy(str, "Hello World!");printf(&quo…

windows驱动开发-电源状态(二)

Modern Standby这个特性其实很难准确的讲清楚&#xff0c;因为它是一个系统行为不是驱动功能行为&#xff0c;应用层、功能驱动、系统总线、设备本身都有不同程度的参与&#xff0c;并且它属于否决性的&#xff0c;一个系统中&#xff0c;只要有一个设备不支持Modern Standby&a…

【算法基础实验】图论-最小生成树Prim的延迟实现

最小生成树-Prim的延迟实现 理论基础 树的基本性质 用一条边连接树中的任意两个顶点都会产生一个新的环&#xff1b; 从树中删去一条边将会得到两棵独立的树。 切分定理的定义 定义。图的一种切分是将图的所有顶点分为两个非空且不重叠的两个集合。横切边 是一条连接两个属…