我对 shell 脚本知之甚少,但不幸的是我必须编写一个。我想问一下bash脚本迭代移动文件,我需要移动按月排序的日志文件,这些文件将由cronjob执行。计划是将 mtime +30(1 个月前)文件移动到几个文件夹中,cronjob 将每天执行,例如:
/home/Work/LogFiles/20131200012.log
/home/Work/LogFiles/thisLogIsDifferent.log
/home/Work/LogFiles/20120322222.log
/home/Work/LogFiles/20140100011.log
/home/Work/LogFiles/thisLogIsDifferent2.log
Run Code Online (Sandbox Code Playgroud)
/home/Work/LogFiles/thisLogIsDifferent.log
/home/Work/LogFiles/thisLogIsDifferent2.log
/home/Work/LogFiles/2013/DEC/20131200012.log
/home/Work/LogFiles/2012/MAR/20120322222.log
/home/Work/LogFiles/2014/JAN/20140100011.log
Run Code Online (Sandbox Code Playgroud)
我对我必须使用的方法一无所知。所以这是我糟糕的 shell 脚本:
BASE_DIR=/home/Work/LogFiles
REPORT_DIR_YEAR=$BASE_DIR/`date +%Y`
REPORT_DIR=$REPORT_DIR_YEAR/`date +%b`
NOW=$(date +"%Y%m")
if ! [ -d $REPORT_DIR_YEAR ]; then
mkdir $REPORT_DIR_YEAR
if ! [ -d $REPORT_DIR ]; then
mkdir $REPORT_DIR
fi
fi
#THIS PART NEED TO BE RE-ARRANGED
#What I expect is not date=NOW; BUT SOME KIND LIKE date %m-1? but I still don't have any ideas about modify date function.
for file …
Run Code Online (Sandbox Code Playgroud)