Linux使用touch批量修改文件/文件夹时间戳
Linux下touch是一个非常有用的命令。
touch语法结构如下:
touch [-acfm][-d <日期时间>][-r <参考文件或目录>][-t <日期时间>][--help] [--version][文件或目录...] 或 touch [-acfm][--help][--version][日期时间][文件或目录...]
1 2 3 4 5 6 7 8 9 10 11 | Usage: touch [OPTION]... FILE... Mandatory arguments to long options are mandatory for short options too. -a change only the access time -d, -- date =STRING parse STRING and use it instead of current time -m change only the modification time -r, --reference=FILE use this file 's times instead of current time -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time -- time =WORD change the specified time : WORD is access, atime, or use: equivalent to -a WORD is modify or mtime: equivalent to -m Note that the -d and -t options accept different time - date formats. |
使用举例:
--------------------------------------------
1 2 3 4 5 6 7 | touch -m -d "2016-05-20 14:25:50" file touch -d "2016-05-20 14:25:50" file touch -d "2016-05-20" file touch -d "14:25:50" file 或者 touch -t 201605201315.50 file touch -t 05201315 file |
命令参数:
1 2 3 | -d使用指定的日期时间。 -a只更改存取时间access -m只更改变动时间modify |
设置日期(设置当前时间,只有root权限才能设置,其他只能查看):
1 2 3 4 | #date //显示当前日期 #date -s 20061010 //设置成20061010,这样会把具体时间设置成空00:00:00 #date -s 12:23:23 //设置具体时间,不会对日期做更改 #date -s "2006-10-10 12:12:23" //这样可以设置全部时间 |
-------------------------------------------
具体步骤:
1、设置系统时间(能影响change time)
1 | date -s "2010-10-10 10:10:10" |
2、修改文件时间
#当前目录下文件/文件夹(不能递归):
1 | touch -m -d "2010-10-10 10:10:10" * |
#递归修改当前目录下所有文件/文件夹3个时间戳(Access、Modify、Change time):
1 | find ./ * - exec touch {} \; |
#递归修改当前目录下所有文件/文件夹指定时间戳(Modify、Change time):
1 | find ./ * - exec touch -m -d "2010-10-10 10:10:10" {} \; |
3、还原系统时间
1 | clock --hctosys |
请注意:
1、文件的Access time会随着每次访问而更新时间,所有这个参数意义不大,浏览器每打开一次这个文件,Access time均会更新。
2、Change time不能随便修改,必须先修改系统时间才能改变这个值。
查看系统硬件时钟
clock --show
硬件时钟与系统时钟同步:
clock --hctosys hc代表硬件时间,sys代表系统时间
PS:补充下查看文件时间:
1 | stat file |
时间显示示例:
1 2 3 | Access: 2010-05-02 01:22:11.000000000 +0800 Modify: 2010-05-02 01:22:11.000000000 +0800 Change: 2010-10-10 00:00:14.000000000 +0800 |
勤苦修行得自在,道力有边边亦无边!
分类:
linux
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 开发的设计和重构,为开发效率服务
· 从零开始开发一个 MCP Server!
· Ai满嘴顺口溜,想考研?浪费我几个小时
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密