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


相关文章
|
18天前
|
Linux
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
54 23
Linux系统之whereis命令的基本使用
|
5天前
|
缓存 Ubuntu Linux
Linux中yum、rpm、apt-get、wget的区别,yum、rpm、apt-get常用命令,CentOS、Ubuntu中安装wget
通过本文,我们详细了解了 `yum`、`rpm`、`apt-get`和 `wget`的区别、常用命令以及在CentOS和Ubuntu中安装 `wget`的方法。`yum`和 `apt-get`是高层次的包管理器,分别用于RPM系和Debian系发行版,能够自动解决依赖问题;而 `rpm`是低层次的包管理工具,适合处理单个包;`wget`则是一个功能强大的下载工具,适用于各种下载任务。在实际使用中,根据系统类型和任务需求选择合适的工具,可以大大提高工作效率和系统管理的便利性。
54 25
|
3天前
|
缓存 Linux
Linux查看内存命令
1. free free命令是最常用的查看内存使用情况的命令。它显示系统的总内存、已使用内存、空闲内存和交换内存的总量。 free -h • -h 选项:以易读的格式(如GB、MB)显示内存大小。 输出示例: total used free shared buff/cache available Mem: 15Gi 4.7Gi 4.1Gi 288Mi 6.6Gi 9.9Gi Swap: 2.0Gi 0B 2.0Gi • to
14 2
|
1月前
|
缓存 安全 Linux
Linux系统查看操作系统版本信息、CPU信息、模块信息
在Linux系统中,常用命令可帮助用户查看操作系统版本、CPU信息和模块信息
131 23
|
1月前
|
网络协议 Unix Linux
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
42 11
|
2月前
|
Linux
linux查看目录下的文件夹命令,find查找某个目录,但是不包括这个目录本身?
通过本文的介绍,您应该对如何在 Linux 系统中查看目录下的文件夹以及使用 `find` 命令查找特定目录内容并排除该目录本身有了清晰的理解。掌握这些命令和技巧,可以大大提高日常文件管理和查找操作的效率。 在实际应用中,灵活使用这些命令和参数,可以帮助您快速定位和管理文件和目录,满足各种复杂的文件系统操作需求。
160 8
|
2月前
|
存储 监控 Linux
嵌入式Linux系统编程 — 5.3 times、clock函数获取进程时间
在嵌入式Linux系统编程中,`times`和 `clock`函数是获取进程时间的两个重要工具。`times`函数提供了更详细的进程和子进程时间信息,而 `clock`函数则提供了更简单的处理器时间获取方法。根据具体需求选择合适的函数,可以更有效地进行性能分析和资源管理。通过本文的介绍,希望能帮助您更好地理解和使用这两个函数,提高嵌入式系统编程的效率和效果。
126 13
|
Linux 数据安全/隐私保护 Windows
初学[Linux]基础命令“touch, cat, more, cp, rm, vm“
初学[Linux]基础命令“touch, cat, more, cp, rm, vm“
147 1
|
9月前
|
Linux Shell
Linux命令(86)之touch
Linux命令(86)之touch
93 0
|
SQL Linux
linux(十一)文件和目录相关命令touch和mkdir(2)
3:创建目录的时候,同时指定权限 语法:mkdir -m 755 xxx sql 复制代码 [root@iZuf60ynur81p6k0ysvtneZ opt]# mkdir -m 700 auth [root@iZuf60ynur81p6k0ysvtneZ opt]# ll -a total 24 drwxr-xr-x. 6 root root 4096 Aug 27 10:49 . dr-xr-xr-x. 18 root root 4096 Aug 10 19:00 .. drwx------ 2 root root 4096 Aug 27 10:49 auth -rw-r--r--
66 0