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

相关推荐
每日亿学18 分钟前
Docker在windows上使用vscode远程连接容器
linux·vscode·docker·ssh
猛踹DY好腿18 分钟前
Web基础
linux·web
花桑有淇19 分钟前
Linux要解压 .rar 文件,你应该使用 unrar 命令
大数据·linux·ubuntu
Camellia__Wang1 小时前
Linux中的库
linux
pin️‍灼灼灼灼1 小时前
Linux——线程练习
linux·服务器·算法
worthsen1 小时前
Linux kernel 与 设备树
linux·kernel
薛定谔式炼丹1 小时前
linux问题总结2
linux·运维·服务器
懒人w1 小时前
百万数据量修改数据思路及方法
java·linux·windows
云计算_Later2 小时前
【mysql】の 用户 | 权限 | 存储引擎 | 变量 | 索引~
linux·mysql·云计算
ztenv2 小时前
C++23特性一览
linux·c++·c++23