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

相关推荐
哈哈幸运15 分钟前
Linux Sed 深度解析:从日志清洗到 K8s 等12个高频场景
linux·运维·编辑器·sed
心随_风动18 分钟前
主流操作系统对比分析(macOS、Linux、Windows、Unix)
linux·windows·macos
啊吧怪不啊吧1 小时前
Linux常见指令介绍下(入门级)
linux·开发语言·centos
陈大大陈2 小时前
基于 C++ 的用户认证系统开发:从注册登录到Redis 缓存优化
java·linux·开发语言·数据结构·c++·算法·缓存
maotou5262 小时前
Ubuntu22学习记录
linux·ubuntu
杨凯凡2 小时前
Linux安全防护:全方位服务安全配置指南
linux·运维·服务器·安全
李菠菜2 小时前
CentOS系统中重置Root用户密码的完整步骤详解
linux·运维
Nightwish52 小时前
Linux随记(十七)
linux·运维·服务器
ghostmen2 小时前
Centos 实现 MySql 8.0.40 主从配置
linux·mysql·mysql主从
付出不多2 小时前
Linux——系统安全及应用
linux·运维·系统安全