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

相关推荐
ICscholar3 小时前
ExaDigiT/RAPS
linux·服务器·ubuntu·系统架构·运维开发
sim20204 小时前
systemctl isolate graphical.target命令不能随便敲
linux·mysql
米高梅狮子4 小时前
4. Linux 进程调度管理
linux·运维·服务器
再创世纪5 小时前
让USB打印机变网络打印机,秀才USB打印服务器
linux·运维·网络
fengyehongWorld6 小时前
Linux ssh端口转发
linux·ssh
知识分享小能手7 小时前
Ubuntu入门学习教程,从入门到精通, Ubuntu 22.04中的Shell编程详细知识点(含案例代码)(17)
linux·学习·ubuntu
Xの哲學8 小时前
深入解析 Linux systemd: 现代初始化系统的设计与实现
linux·服务器·网络·算法·边缘计算
龙月9 小时前
journalctl命令以及参数详解
linux·运维
EndingCoder9 小时前
TypeScript 的基本类型:数字、字符串和布尔
linux·ubuntu·typescript
YJlio9 小时前
Kali Linux 外置无线网卡接入与识别排障(VMware 环境|合规学习版)
linux·网络·学习