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


相关文章
|
21小时前
|
Linux Shell
Linux系统
是对Linux系统进行管理的命令。对于Linux系统来说,无论是中央处理器、内存、磁盘驱动器、键盘、鼠标,还是用户等都是文件,Linux系统管理的命令是它正常运行的核心,与之前的DOS命令类似。linux命令在系统中有两种类型:内置Shell命令和Linux命令。
|
3天前
|
Linux Shell
Linux系统
是对Linux系统进行管理的命令。对于Linux系统来说,无论是中央处理器、内存、磁盘驱动器、键盘、鼠标,还是用户等都是文件,Linux系统管理的命令是它正常运行的核心,与之前的DOS命令类似。linux命令在系统中有两种类型:内置Shell命令和Linux命令。
|
1天前
|
Linux Shell
Linux系统
是对Linux系统进行管理的命令。对于Linux系统来说,无论是中央处理器、内存、磁盘驱动器、键盘、鼠标,还是用户等都是文件,Linux系统管理的命令是它正常运行的核心,与之前的DOS命令类似。linux命令在系统中有两种类型:内置Shell命令和Linux命令。
|
2天前
|
Ubuntu Linux
Linux系统基本操作
Linux系统基本操作
12 7
|
2天前
|
Linux
Linux常用命令包括
Linux常用命令包括
10 5
|
2天前
|
Linux
Linux命令
Linux命令
13 5
|
3天前
|
监控 安全 Linux
使用NRPE和Nagios监控Linux系统资源的方法
通过遵循以上步骤,可以有效地使用NRPE和Nagios监控Linux系统资源,确保系统运行稳定,并及时响应任何潜在的问题。这种方法提供了高度的可定制性和灵活性,适用于从小型环境到大型分布式系统的各种监控需求。
12 2
|
3天前
|
Linux Shell
Linux系统
是对Linux系统进行管理的命令。对于Linux系统来说,无论是中央处理器、内存、磁盘驱动器、键盘、鼠标,还是用户等都是文件,Linux系统管理的命令是它正常运行的核心,与之前的DOS命令类似。linux命令在系统中有两种类型:内置Shell命令和Linux命令。
|
4天前
|
Ubuntu Linux Shell
Linux系统密码忘记
【10月更文挑战第2天】在Linux系统中,若忘记密码,可以通过单用户模式或使用Live CD/USB来重置。对于Ubuntu系统,可通过GRUB引导菜单进入单用户模式,利用命令行重置密码;或使用Live CD/USB启动并挂载硬盘分区后修改密码文件。CentOS系统同样支持单用户模式重置密码,也可借助安装介质进入救援模式,挂载文件系统后进行密码重置。这些方法均能在忘记密码的情况下帮助恢复系统访问。
|
3月前
|
Linux
Linux部署04-ls命令的参数和选项,主体,参数,选项,ls / 查看根目录下的文件夹,-a的意思是列出全部选项 ls -a home全部文件,.代表着隐藏的文件夹,-l 选项,以列表竖向的形式展
Linux部署04-ls命令的参数和选项,主体,参数,选项,ls / 查看根目录下的文件夹,-a的意思是列出全部选项 ls -a home全部文件,.代表着隐藏的文件夹,-l 选项,以列表竖向的形式展
下一篇
无影云桌面