2013-10-08 55 views
2

我有一个嵌套的目录,看起来像2013/10/08/access.log.xz,我可以找到所有的日志文件,我想find . -name \*access.log.xz。我想把它们全部放在一个以20131008_access.log.xz这个日期为前缀的单个目录中。我甚至不知道从哪里开始。有什么建议么?按日期更改嵌套的文件结构到文件名

回答

3

如果你有最近的bash或递归通配一个外壳,你可以做这样的事情:

shopt -s globstar 
for logfile in **/*access.log.xz; do 
    IFS=/ read year month day file <<< "$logfile" 
    mv "$logfile" "${year}${month}${day}_${file}" 
done 

如果你有一个旧的bash,你可以模拟的效果,但它难以阅读:

find . -name '*access.log.xz' -exec bash -c 'for logfile; do 
    IFS=/ read dot year month day file <<< "$logfile" 
    mv "$logfile" "${year}${month}${day}_${file}" 
done' _ {} + 
+0

太棒了!非常感谢。 –