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 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'
[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日。

相关推荐
Sapphire~6 小时前
Linux-07 ubuntu 的 chrome 启动不了
linux·chrome·ubuntu
伤不起bb6 小时前
NoSQL 之 Redis 配置与优化
linux·运维·数据库·redis·nosql
广东数字化转型6 小时前
nginx怎么使用nginx-rtmp-module模块实现直播间功能
linux·运维·nginx
啵啵学习6 小时前
Linux 里 su 和 sudo 命令这两个有什么不一样?
linux·运维·服务器·单片机·ubuntu·centos·嵌入式
半桔7 小时前
【Linux手册】冯诺依曼体系结构
linux·缓存·职场和发展·系统架构
网硕互联的小客服7 小时前
如何利用Elastic Stack(ELK)进行安全日志分析
linux·服务器·网络·安全
冰橙子id8 小时前
linux——磁盘和文件系统管理
linux·运维·服务器
无聊的小坏坏9 小时前
环境变量深度解析:从配置到内核的全链路指南
linux·bash
才鲸嵌入式9 小时前
01 Ubuntu20.04下编译QEMU8.2.4,交叉编译32位ARM程序,运行ARM程序的方法
linux·c语言·单片机·嵌入式·arm·qemu·虚拟机
伤不起bb11 小时前
Kafka 消息队列
linux·运维·分布式·kafka