微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

linux shell touch更新文件时间

目录

更改文件为当前时间

更改文件为指定时间

更改文件为别的文件相同的时间

更新指定目录下的所有文件时间

语法

总结


更改文件为当前时间

无此文件情况下 会创建一个文件

$touch new.txt
$ll new.txt
-rw-r--r-- 1 root root 0 7月  12 16:56 new.txt

如果此文件已经存在的情况下.更改文件时间为当前时间

$touch new.txt
-rw-r--r-- 1 root root 0 7月  12 16:57 new.txt

更改文件为指定时间

$date
2015年 07月 12日 星期日 16:59:10 CST
$touch -t 11111111 new.txt
$ll new.txt
-rw-r--r-- 1 root root 0 11月 11 2015 new.txt

分析:此处指定文件的时间格式为:yyyy(年)MM(月)DD(日)hh(时)mm(分),省略在表示使用当前系统的时间.

更改文件为别的文件相同的时间

$ll new.txt 
-rw-r--r-- 1 root root 0 7月  12 17:03 new.txt
$ll /etc/passwd
-rw-r--r-- 1 root root 1804 6月  10 23:27 /etc/passwd
$touch -r /etc/passwd new.txt
$ll new.txt 
-rw-r--r-- 1 root root 0 6月  10 23:27 new.txt

更新指定目录下的所有文件时间

$find /tmp -exec touch -t 11111111 {} \;
$ll /tmp
总用量 12
drwxr-xr-x 2 root root 4096 11月 11 2015 hidden
-rw-r--r-- 1 root root    0 11月 11 2015 new.txt
drwxr-xr-x 2 root root 4096 11月 11 2015 test
-rwxr-xr-x 1 root root  385 11月 11 2015 touch.sh

分析:可把/tmp下的所有文件和目录都改变修改时间。

语法

touch [-acdmt] 文件参数

主要选项和作用

参数

作用

-a

修改文件的最后访问时间

-c

修改时间,而不创建文件

-d

后面可以接日期,也可以使用-date=”如期或时间”

-m

修改文件修改时间

-t

后面可接时间,格式为[yyyyMMDDhhmm]

总结

linux中touch命令参数不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐