目录
一、touch命令
1.1、概述
- 创建文件与修改文件时间戳。
- touch命令可以用来创建文件(当被指定文件不存时,该命令会在当前位置建立一个空文件),此命令更重要的功能是修改文件的时间参数(但文件存在时,会修改文件的时间参数)。
- Linux系统中,每个文件主要有3个时间参数(可通过stat命令进行查看),分别是文件的访问时间、数据修改时间以及状态修改时间。
- 访问时间(Access time,简称 atime):只要文件的内容被读取,访问时间就会更新,使用cat命令查看文件内容,此时文件访问时间会发声改变
- 数据修改时间(Modify time,简称 mtime):当文件的内容数据发生改变,此文件的数据修改时间会跟着发生改变。
- 状态修改时间(change Time,简称ctime):当文件状态发生变化,就会相应的改变这个时间,如文件的权限或者属性发生改变,此时间就会发生变化。
touch命令语法
- touch[选项]文件名
-
touch [-acfm][-d<日期时间>][-r<参考文件或目录>] [-t<日期时间>][--help][--version][文件或目录…]
- -a:只修改文件访问时间。
- -c:仅修改文件的时间参数,如果文件不存在,则不建立新文件。
- -d:后面可以跟预修订的日期,而不用当前的时间,即把文件的atime和atime时间该位指定时间。
- -m:只修改文件的时间记录。
- -t:命令后面可以跟修订的时间,而不用目前的时间,时间书写格式为 YYMMDDhhmm。
1.2、案例
1.2.1、touch创建空白文件
[user@localhost ~]$ su root
密码:
[root@localhost user]# touch test
[root@localhost user]# ls
cangls Documents Music Public Templates Videos
Desktop Downloads Pictures runoob test
1.2.2、修改文件的访问时间
[root@localhost user]# ll --time=atime test 查看文件访问时间
-rw-r--r--. 1 root root 0 11月 25 23:32 test 文件上次访问时间11月 25 23:32
[root@localhost user]# touch test
[root@localhost user]# ll --time=atime test
-rw-r--r--. 1 root root 0 11月 25 23:34 test 再次查看文件访问时间,如果文件存在则不会报错
[root@localhost user]#
1.2.3、仅修改test文件的atime和mtime
[root@localhost user]# ll test; ll --time=atime test;ll --time=ctime test
-rw-r--r--. 1 root root 0 11月 25 00:00 test
-rw-r--r--. 1 root root 0 11月 25 23:39 test
-rw-r--r--. 1 root root 0 11月 25 23:39 test
[root@localhost user]#
1.3、touch --help
帮助使用touch命令。
[root@localhost user]# touch --help
用法:touch [选项]... 文件...
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.
不存在的文件参数被创建为空,除非-c或-h提供
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 只更改访问时间
-c, --no-create 不创建任何文件
-d, --date=字符串 使用指定字符串表示时间而非当前时间
-f (忽略)
-h, --no-dereference 会影响符号链接本身,而非符号链接所指示的目的地
(当系统支持更改符号链接的所有者时,此选项才有用)
-m 只更改修改时间
-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 显示此帮助信息并退出
--version 显示版本信息并退出
请注意,-d 和-t 选项可接受不同的时间/日期格式。
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告touch 的翻译错误
要获取完整文档,请运行:info coreutils 'touch invocation'