Linux系统之touch命令的基本使用

在这里插入图片描述

一、touch命令介绍

1. touch命令简介

touch命令用于创建空文件或修改现有文件的时间戳。

2. touch命令作用

touch命令 有两个功能:

  • 创建新文件:如果你指定一个不存在的文件名作为touch命令的参数,它将在当前目录下创建一个新的空文件。这在需要快速创建一些空的文本文件或者配置文件时非常有用。

  • 更新文件的时间戳:对于已经存在的文件,touch命令会更新该文件的访问时间和修改时间标记为当前系统时间。这并不改变文件的内容,只是修改了文件的元数据,即文件的“最近访问时间”(atime)和“最近修改时间”(mtime)。这对于某些需要基于文件更新时间进行逻辑处理的情况非常有用,比如让cron任务认为某个脚本是“新”的,以便重新执行。

二、touch命令帮助

1. touch命令的帮助信息

在centos7.6中,touch命令是系统默认已安装的,可通过touch --help查询帮助信息。

[root@jeven ~]# 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/>
For complete documentation, run: info coreutils 'touch invocation'

2. touch命令的选项解释

touch命令的选项解释

-a:仅修改文件的访问时间(atime)。
-c:如果文件不存在,不要创建新文件。
-d:设置文件的时间戳为指定时间,可以使用各种格式的日期时间值,如“2021-12-31 23:59:59”、“next Friday”、“+3 hours”等。
-m:仅修改文件的修改时间(mtime)。
-r:将目标文件的时间戳与指定文件的时间戳相同。
-t:设置文件的访问时间和修改时间为指定时间,格式与-d选项相同。
--help:在线帮助;
--version:显示版本信息。

三、touch命令的基本使用

1. 查看touch工具版本

查看touch工具版本,可以看到在centos7.6中默认安装的版本为 8.22

[root@jeven ~]# touch --version
touch (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Rubin, Arnold Robbins, Jim Kingdon,
David MacKenzie, and Randy Smith.

2. 创建空文件

使用touch命令创建空文件

[root@jeven tmp]# touch file01.txt
[root@jeven tmp]# ls
file01.txt

3.查看空文件属性

查看touch创建的空文件属性

[root@jeven tmp]# file file01.txt 
file01.txt: empty
  • 写入文本内容后,再次查看文件属性。
[root@jeven tmp]# echo aa > file01.txt 
[root@jeven tmp]# file file01.txt 
file01.txt: ASCII text

4. 修改文件时间戳

  • 查看当前文件的时间戳
[root@jeven tmp]# stat file01.txt 
  File: ‘file01.txt’
  Size: 3         	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 18451793    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2023-07-10 19:55:25.464098671 +0800
Modify: 2023-07-10 19:55:21.487098520 +0800
Change: 2023-07-10 19:55:21.487098520 +0800
 Birth: -

  • 要修改文件的访问时间和修改时间为当前时间
touch -t $(date +"%Y%m%d%H%M.%S") file01.txt 

在这里插入图片描述

5. 文件不存在时不创建

使用-c选项,当文件不存在时,则不创建该文件。

[root@jeven tmp]# touch -c aa
[root@jeven tmp]# ls
file01.txt

四、总结

使用touch命令时,有几点注意事项需要留意:

  1. 权限问题:你需要有足够权限才能使用touch命令创建或修改文件的时间戳。如果没有相应的权限,比如尝试在没有写权限的目录下创建文件,命令将会失败并返回错误信息。

  2. 仅修改时间戳touch命令主要作用是修改文件的访问和修改时间戳,并不会改变文件的实际内容。如果你需要编辑文件内容,应该使用如vinanoecho等其他命令或工具。

  3. 创建新文件:当指定的文件名不存在时,touch会创建一个新的空文件。如果文件已存在,它不会被清空,只是时间戳会被更新。

  4. 更新时间选项:使用-a只更新访问时间(atime),使用-m只更新修改时间(mtime)。如果不指定,两者都会被更新为当前时间。

  5. 时间戳设定:使用-t-d选项可以设定特定的时间戳,而不是使用当前时间。确保按照正确的格式输入日期和时间,否则命令可能无法正确执行。

  6. 引用文件时间戳:使用-r选项可以将一个文件的时间戳复制到另一个文件上,这在批量调整文件时间时特别有用。

  7. 防止创建文件:加上-c选项后,如果文件已存在,touch将只更新其时间戳而不创建新文件。如果文件不存在,命令不做任何操作且不返回错误。

  8. 环境变量与别名:在某些系统或用户的配置中,touch命令可能已被别名重定义或受环境变量影响,因此了解当前环境的具体行为是很重要的,特别是在执行自动化脚本时。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江湖有缘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值