通过结合Linux的find
和touch
命令,可批量修改符合特定条件的文件时间戳。核心步骤如下:
-
基本语法:
find [路径] [条件] -exec touch -t [时间戳] {} \;
-
常用场景:
-
按时间筛选:
# 修改7天内修改过的文件为当前时间 find /path -type f -mtime -7 -exec touch {} \; # 修改30天前的日志文件为指定时间 find /var/log -name "*.log" -mtime +30 -exec touch -t 202310101200 {} \;
-
按名称/类型筛选:
# 修改所有.txt文件的时间戳 find ~/docs -name "*.txt" -exec touch -d "2023-01-01" {} \;
-
-
关键参数:
touch -t YYYYMMDDHHMM.SS
:精确到秒的时间戳touch -d "STRING"
:自然语言时间(如"2 days ago")find -mtime +n/-n
:筛选n天前/内的文件
-
验证操作: 先运行
find
命令不加-exec
,确认文件列表后再执行实际修改。
注意:需确保对目标路径有写权限,谨慎使用-delete
等破坏性操作。