我有一个cron作业,每30分钟运行一次,生成带有时间戳的日志文件,如下所示:
test20130215100531.log,test20130215102031.log
我想每天创建一个文件夹,其中包含日期时间戳,并在生成时将日志文件推送到相应的日期文件夹中.
我需要在AIX服务器上用bash实现这一点.
也许您正在寻找这样的脚本:
原文链接:https://www.f2er.com/bash/384156.html#!/bin/bash shopt -s nullglob # this line is so that it does not compain when no logfiles are found for filename in test*.log; do # Files considered are the ones startign with test and ending in .log foldername=$(echo "$filename" | awk '{print (substr($0,5,8));}'); # The foldername is characters 5 to 13 from the filename (if they exist) mkdir -p "$foldername" # -p so that we dont get "folder exists" warning mv "$filename" "$foldername" echo "$filename $foldername" ; done
我只测试了你的样本,所以在使用包含重要内容的目录之前进行适当的测试.
编辑以回应评论:
将原始脚本更改为:
foldername=$(date +%Y%m%d) mkdir -p /home/app/logs/"$foldername" sh sample.sh > /home/app/logs/"$foldername"/test$(date +%Y%m%d%H%M%S).log
或者它在其他地方创建目录,只需执行以下操作:
sh sample.sh > /home/app/logs/$(date +%Y%m%d)/test$(date +%Y%m%d%H%M%S).log