Linux 文件管理:Linux touch 命令(创建文件) 0 个改进

touch 命令是创建空白文件的一种方法(当然还有其他方法)。 touch 命令用于刷新文件的访问时间或修改时间属性的。

语法:

touch <filename>  

举例:

$ touch myfile1  

TOUCH Command

参见上面例子,我们通过 touch 命令创建了2个文件 myfile1myfile2 。一条 touch 命令跟多个文件名可创建多个文件,如上例,我们可以使用如下一条命令完成同样任务:

$ touch myfile1 myfile2  

touch 命令选项

选项 功能
touch -a 改变文件访问时间属性和改变时间属性。
touch -m 改变文件修改时间属性。
touch -r 按照某指定文件的时间戳属性刷新文件的时间戳属性。
touch -t 按指定时间创建新文件。
touch -c 不创建空白文件。

touch 命令 -a 选项

touch -a 命令加 -a 选项是改变文件的访问时间和改变时间。默认是当前系统时间。

语法:

touch -a <filename>  

举例:

$ touch -a usr  

查看文件访问时间(Access)和 改变时间(Change)属性,使用 stat 命令。

TOUCH -a Command

如上截图中,先使用 stat 命令只是查看目录 Destop/usr 的状态(包含时间属性)。然后使用 $ touch -a usr 命令使 usr 的访问时间和改变时间都变为当前系统默认时间。

touch 命令 -m 选项

touch -m 命令的 -m 选项改变一个文件的修改时间(Modify)。

语法:

touch -m <filename>  

举例:

$ touch -m usr  

TOUCH Command

仔细观察上面的截图,只有修改时间(Modify)时间被改变。

touch 命令 -r 选项

这个命令将参考其它目标进行时间戳属性改变。有两种形式,作用相同。

下面的例子,按照 demo.txt 文件的时间戳属性,改变 Demo.txt 文件的时间戳属性。可以写成如下形式:

$ touch -r demo.txt Demo.txt  

也可写成:

$ touch Demo.txt -r demo.txt  

刷新时间戳前,先看看这两个文件的时间戳属性。

TOUCH Command

现在,使用 $ touch -r demo.txt Demo.txt 命令后,再次查看两个文件的时间戳知道 Demo.txt 的时间戳已按 demo.txt 文件的时间戳进行了改变。

TOUCH Command

touch 命令 -t 选项

使用这个命令,改变文件访问时间(access)和修改时间(modify)为指定的时间。(译注:读者要自己实测,文中所说未必正确)

这个命令以指定时间替换系统默认时间修改文件相关时间戳属性。

时间格式如下:

#        年年年年月月日日时时分分.秒秒
touch -t YYYYMMDDhhmm.ss  

下面图示的是文件 2.png 执行修改时间戳命令前的状态,

TOUCH -t Command

下面图示的是文件 2.png 执行用指定时间修改文件时间戳属性后的状态。

TOUCH Command

touch 命令 -c 选项

使用 -c 选项,在指定文件不存在的情况下,不创建新文件。

语法:

touch -c <filename>  

举例:

touch -c movie  

TOUCH Command

如上图示,欲创建文 movie,但使用了 -c 选项,结果不会创建 movie 文件。

本文为 Wiki 文章,邀您参与纠错、纰漏和优化
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!