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的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值