现在的位置: 首页 > 综合 > 正文

根据文件修改时间批量移动到指定目录

2014年01月13日 ⁄ 综合 ⁄ 共 540字 ⁄ 字号 评论关闭

要求:
1.找到所有以.log结尾的文件。
2.将找到的文件放到临时目录/tmp
3.根据文件的修改时间,把文件归类,批量移动到以文件修改时间为目录名的目录中。

mkdir /tmp/workdir
mv *.log /tmp/workdir
cd /tmp/workdir
for f in *.log
do
   datedir=$(stat -c %y $f | sed 's/ .*//;s/-//g')
   [ -d "$datedir"] || mkdir "$datedir" 
   mv "$f" "$datedir"
done

stat -c %y $f | sed 's/ .*//;s/-//g'   获取*.log last modification时间,然后sed将修改时间后端.和-部分去除

%x   Time of last access
%X   Time of last access as seconds since Epoch
%y   Time of last modification
%Y   Time of last modification as seconds since Epoch
%z   Time of last change
%Z   Time of last change as seconds since Epoch

Epoch就是传说中的从970年01月01日00时00分00秒到现在的秒数

抱歉!评论已关闭.