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


相关文章
|
3月前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
二、Linux文本处理与文件操作核心命令
|
3月前
|
Ubuntu Linux Anolis
Linux系统禁用swap
本文介绍了在新版本Linux系统(如Ubuntu 20.04+、CentOS Stream、openEuler等)中禁用swap的两种方法。传统通过注释/etc/fstab中swap行的方式已失效,现需使用systemd管理swap.target服务或在/etc/fstab中添加noauto参数实现禁用。方法1通过屏蔽swap.target适用于新版系统,方法2通过修改fstab挂载选项更通用,兼容所有系统。
236 3
Linux系统禁用swap
|
3月前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
303 137
|
3月前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
692 57
|
2月前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
445 2
|
3月前
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
301 0
Linux内存问题排查命令详解
|
3月前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
282 16
|
Linux 数据安全/隐私保护 Windows
初学[Linux]基础命令“touch, cat, more, cp, rm, vm“
初学[Linux]基础命令“touch, cat, more, cp, rm, vm“
266 1
|
Linux Shell
Linux命令(86)之touch
Linux命令(86)之touch
196 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--
138 0