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

devtools/2024/10/16 2:31:22/

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/devtools/30082.html

相关文章

WPF基础应用

WPF参考原文 MVVM介绍 1.常用布局控件 1.1 布局控件 WPF&#xff08;Windows Presentation Foundation&#xff09;提供了多种布局容器来帮助开发者设计用户界面&#xff0c;以下是一些常用的布局&#xff1a; Grid: Grid是最常用的布局容器之一&#xff0c;它允许你通过定…

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

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

直播电商的核心业务管理架构:打造数字化营销新模式

直播电商作为电子商务的新兴形态&#xff0c;正在以惊人的速度改变着传统零售行业的格局。而要想在激烈的竞争中脱颖而出&#xff0c;一个稳固的核心业务管理架构是至关重要的。本文将深入探讨直播电商的核心业务管理架构&#xff0c;揭示其背后的运营秘密&#xff0c;助您了解…

Redis八股文

1、安装redis # 参考网址 https://www.cnblogs.com/hunanzp/p/12304622.html# 下载redis安装包 wget http://download.redis.io/releases/redis-5.0.7.tar.gz# 解压并安装redis tar -zxvf redis-5.0.7.tar.gz# 移动redis目录 mv /root/redis-5.0.7 /usr/local/redis# 编译 cd …

泛型通配符

泛型&通配符 文章目录 泛型&通配符一、泛型介绍&理解1.1 泛型概述&使用(集合/比较器)1.2 自定义范型结构(类/接口/方法) 二、通配符&读写特点三、企业真题 一、泛型介绍&理解 1.1 泛型概述&使用(集合/比较器) 泛型&#xff1a;类似于场景中的标签…

Mysql

目录 背景步骤宏观微观数据库设计表字段设计索引设计引擎设计运行问题 解决问题的办法日志解决锁事务 总结 背景 步骤 宏观 我们首先找到一条线将所有关于mysql的知识串起来 客户端&#xff1a;最上层的服务并不是 MySQL 所独有的&#xff0c;大多数基于网络的客户端/服务…

【论文阅读笔记】TS2Vec: Towards Universal Representation of Time Series

【论文阅读笔记】TS2Vec: Towards Universal Representation of Time Series 摘要 这段文字介绍了一个名为TS2Vec的通用框架&#xff0c;用于学习时间序列数据的表示&#xff0c;可以在任意语义层次上进行。与现有方法不同&#xff0c;TS2Vec通过对增强的上下文视图进行层次化…

ZooKeeper知识点总结及分布式锁实现

最初接触ZooKeeper是之前的一个公司的微服务项目中&#xff0c;涉及到Dubbo和ZooKeeper&#xff0c;ZooKeeper作为微服务的注册和配置中心。好了&#xff0c;开始介绍ZooKeeper了。 目录 1.ZooKeeper的基本概念 2.ZooKeeper的节点&#xff08;ZNode&#xff09; 3. ZooKeep…