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

相关文章

在Linux系统中,设置ARP防火墙可以通过多种方法实现,包括使...
在Linux环境下,使用Jack2进行编译时,可以采取以下策略来提...
`getid`命令在Linux系统中用于获取当前进程的有效用户ID(EU...
在Linux环境下,codesign工具用于对代码进行签名,以确保其完...
Linux中的`tr`命令,其英文全称是“transform”,即转换的意...
Linux中的ARP防火墙是一种用于防止ARP欺骗攻击的安全措施,它...