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

ops/2024/10/15 22:29:46/

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/ops/29897.html

相关文章

细说SVPWM原理及软件实现原理,关联PWM实现

细说SVPWM原理及软件实现原理&#xff0c;关联PWM实现 文章目录 细说SVPWM原理及软件实现原理&#xff0c;关联PWM实现1. 前言2. 基础控制原理回顾2.1 FOC 原理回顾2.2 细说 SVPWM2.2.1 矢量扇区计算2.2.2 矢量作用时间计算 2.2.3 如何理解 U4 U6 2/3Udc?2.2.4 如何理解 U4m…

【C++】命名冲突了怎么办?命名空间来解决你的烦恼!!!C++不同于C的命名方式——带你认识C++的命名空间

命名空间 导读一、什么是C?二、C的发展三、命名空间3.1 C语言中的重名冲突3.2 什么是命名空间&#xff1f;3.3 命名空间的定义3.4 命名空间的使用环境3.5 ::——作用域限定符3.6 命名空间的使用方法3.6.1 通过作用域限定符来指定作用域3.6.2 通过关键字using和关键字namespace…

Ubuntu下安装并配置SAMBA服务器

今天我要给大家带来一个关于在Ubuntu下安装并配置SAMBA服务器的详细技术博客。不过&#xff0c;在我们开始之前&#xff0c;我得先夸一夸阿贝云免费服务器&#xff0c;这个免费云服务器真是不错的东西啊&#xff01;配置有1核CPU、1G内存、10G硬盘和5M带宽。现在我们开始吧&…

Django响应‘表单请求’过程

&#xff08;1&#xff09;用户通过自己的浏览器&#xff08;客户端&#xff09;第一次向服务器发出含有表单页面的请求&#xff0c;Django会创建一个未绑定数据的表单实例&#xff08;例如form LoginForm(), form实例就是未绑定实例&#xff09;&#xff0c;即空表单&#xf…

【Docker】如何注册Hub账号并上传镜像到Hub仓库

一、创建Hub账户 浏览器访问&#xff1a;hub.docker.com 点击【Sign up】注册账号 输入【邮箱】【用户名】【密码】 ps&#xff1a;用户名要有字母数字&#xff1b;订阅不用勾选 点击【Sign up】注册即可 点击【Sign in】登录账号 输入【邮箱】【密码】 点击【Continue】登录 二…

SAP PP学习笔记08 - 作业区(工作中心Work Center),作业区Customize

上一章讲了作业手顺&#xff08;工艺路线Routing&#xff09;。 SAP PP学习笔记07 - 作业手顺&#xff08;工艺路线Routing&#xff09;-CSDN博客 这一章来讲讲作业区&#xff08;工作中心 Work Center&#xff09;。 1&#xff0c;作业区&#xff08;工作中心&#xff09;中…

《十二》Qt各种对话框之FileDialog文件对话框及QMessageBox 消息对话框

QFileDialog 对话框 选择打开一个文件 若要打开一个文件&#xff0c;可调用静态函数 QFileDialog::getOpenFileName()&#xff0c;“打开一个文件”按钮的响应代码如下&#xff1a; void Dialog::on_btnOpen_clicked() { //选择单个文件QString curPathQDir::currentPath()…

241 基于matlab的Dijkstra算法进行路径规划

基于matlab的Dijkstra算法进行路径规划。可根据实际情况输入障碍物和起止点坐标信息&#xff1b; 输出避碰最短路径&#xff1b; 能够利用切线图算法对障碍物区域进行环境建模&#xff0c;设置障碍物的位置和区域。利用Dijkstra算法进行路径规划。程序已调通&#xff0c;可直接…