您的位置:首页 > 运维架构 > Linux

[一文一命令]touch命令详解

2013-12-20 14:09 281 查看

touch

名称:touch - change file timestamps 更改文件时间标签
语法:touch [option] file
功能简述:linux中touch命令并不常用,一般在使用make的时间会用到,用来更改文件的时时间戳,或者新建一个不存在的文件
参数:
-a 或—time=atime 或 –tiem=access 只更改存取时间
-c 或–no-create 不建立任何文档
-d 使用指定的日期时间,而非现在的时间
-m --time=modify 只更改改动时间
-t 使用指定的日期和时间,而非现在的时间
-r 把指定文档或目录的日期和时间,设置成参考文档或目录日期的时间相同
实例:
1)创建一个不存在的文件[root@bogon ~]# touch 1.log 2.log 3.log
[root@bogon ~]# ls *.log
1.log 2.log 3.log install.log
[root@bogon ~]#

2)更新1.log和install.log的时间戳相同
[root@bogon ~]# ls -l 1.log install.log
-rw-r--r-- 1 root root 0 Apr 25 18:48 1.log
-rw-r--r-- 1 root root 25027 Apr 25 04:13install.log
[root@bogon ~]# touch 1.log install.log
[root@bogon ~]# ls -l 1.log install.log
-rw-r--r-- 1 root root 0 Apr 25 19:04 1.log
-rw-r--r-- 1 root root 25027 Apr 25 19:04install.log
Ps:我以为是把1.log 的时间改成install.log的时间,其实效果是两者的时间戳相同,但是现在的时间。那能使用-t选项来更新两者的时间戳成一样的?
[root@bogon ~]# touch -r -t 201304261005 1.log test.log
touch: failed to get attributes of `-t': No suchfile or directory
以上的方法试了下,行不通。不过既然要相同而且是手动指定,那加-r选项不是多余的吗? 呵呵…
[root@bogon ~]# touch -t201304261005 1.log test.log
[root@bogon ~]# ll 1.log test.log
-rw-r--r-- 1 root root 0 Apr 26 2013 1.log
-rw-r--r-- 1 root root 0 Apr 26 2013 test.log
时间戳格式说明:[[CC]YY]MMDDhhmm[.SS] CC为世纪数,YY为年数MM为月份DD为日期hh为小时mm为分钟。系统限制,遭遇1970年的时间是错误的。
在网上有发现有一些时间戳格式是反过来的MMDDhhmm[cc][yy][.ss]后man试验了下,是不错的。

将两个文件的时间戳修改成为现在的时间。-t选项是指定自己的一个时间戳

---------------------------后续自我小结------------------------
touch命令应该是不常用的,可以用来创建文件,更改时间戳。
----------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息