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

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

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

一、touch命令介绍

1. touch命令简介

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

2. touch命令作用

touch命令 有两个功能:

  • 用于把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的数据将原封不动地保留下来;
  • 用来创建新的空文件。

二、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工具版本

[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


相关文章
|
1天前
|
Linux
【Linux】深入理解ls命令
【Linux】深入理解ls命令
|
1天前
|
SQL 监控 架构师
linux系统性能分析的目的
【4月更文挑战第19天】在Linux系统中,找到性能瓶颈是关键,涉及应用程序、操作系统、硬件和网络的全面排查。优化方案通常针对应用程序和操作系统,而硬件和网络问题较易定位。目标是平衡资源使用,确保系统响应和稳定性。系统管理员、架构设计人员和开发人员共同参与,通过监控硬件、网络、配置和代码来优化性能。流程包括管理员初步判断,架构师处理结构问题,开发人员优化代码,实现系统资源的均衡利用。
6 1
|
3天前
|
Ubuntu Linux
Linux(Ubuntu)系统临时IP以及静态IP配置(关闭、启动网卡等操作)
请注意,以上步骤是在临时基础上进行配置的。如果要永久保存静态IP地址,通常还需要修改 `/etc/network/interfaces`文件,以便在系统重启后保持配置。同时,确保备份相关配置文件以防止出现问题。
15 1
|
3天前
|
监控 Linux Windows
50个必知的Linux命令技巧,你都掌握了吗?(下)
50个必知的Linux命令技巧,你都掌握了吗?(下)
|
4天前
|
Linux 数据安全/隐私保护
Linux系统忘记密码的三种解决办法
这篇博客介绍了三种在Linux忘记密码时重置登录密码的方法:1) 使用恢复模式,通过控制台界面以管理员权限更改密码;2) 利用Linux Live CD/USB启动,挂载硬盘分区并使用终端更改密码;3) 进入单用户模式,自动以管理员身份登录后重置密码。每个方法都提供了详细步骤,提醒用户在操作前备份重要数据。
|
4天前
|
Linux Shell Windows
Linux 常用基本命令
Linux 常用基本命令
|
4天前
|
JSON Unix Linux
Linux系统之jq工具的基本使用
Linux系统之jq工具的基本使用
32 2
|
4天前
|
数据采集 监控 安全
linux系统被×××后处理经历
linux系统被×××后处理经历
|
4天前
|
监控 安全 Linux
Linux系统之安装ServerBee服务器监控工具
【4月更文挑战第22天】Linux系统之安装ServerBee服务器监控工具
43 2
|
18天前
|
缓存 Linux 测试技术
安装【银河麒麟V10】linux系统--并挂载镜像
安装【银河麒麟V10】linux系统--并挂载镜像
100 0