我對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
後
/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
我對我必須使用的方法一無所知。這是我糟糕的 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 in find $BASE_DIR -maxdepth 1 -type f -mtime +30 -name '*$NOW*'
do
month=$(ls -l $file | awk '{ print $6 }')
case "$month" in
"Jan") mv $file $REPORT_DIR_YEAR/$month/$file echo "$file moved to $REPORT_DIR/$file";;
"Feb") mv $file $REPORT_DIR_YEAR/$month/$file echo "$file moved to $REPORT_DIR/$file";;
"Mar") mv $file $REPORT_DIR_YEAR/$month/$file echo "$file moved to $REPORT_DIR/$file";;
"Apr") mv $file $REPORT_DIR_YEAR/$month/$file echo "$file moved to $REPORT_DIR/$file";;
"May") mv $file $REPORT_DIR_YEAR/$month/$file echo "$file moved to $REPORT_DIR/$file";;
"Jun") mv $file $REPORT_DIR_YEAR/$month/$file echo "$file moved to $REPORT_DIR/$file";;
"Jul") mv $file $REPORT_DIR_YEAR/$month/$file echo "$file moved to $REPORT_DIR/$file";;
"Aug") mv $file $REPORT_DIR_YEAR/$month/$file echo "$file moved to $REPORT_DIR/$file";;
"Sep") mv $file $REPORT_DIR_YEAR/$month/$file echo "$file moved to $REPORT_DIR/$file";;
"Oct") mv $file $REPORT_DIR_YEAR/$month/$file echo "$file moved to $REPORT_DIR/$file";;
"Nov") mv $file $REPORT_DIR_YEAR/$month/$file echo "$file moved to $REPORT_DIR/$file";;
"Dec") mv $file $REPORT_DIR_YEAR/$month/$file echo "$file moved to $REPORT_DIR/$file";;
*) echo " Do nothing " ;;
esac
done
是的,這個範例$month
不適用於先前的 for 迴圈$file
。為什麼?我不知道。我只是從各種來源、論壇、for 循環中複製範例,但它不起作用。
答案1
首先,它是解析輸出不是一個好主意ls
因為它會導致各種各樣的問題。取得文件年齡的更好方法是stat
.例如:
$ ls -l 20120322222.log
-rw-r--r-- 1 terdon terdon 0 Jan 1 2012 20120322222.log
$ stat -c %y 20120322222.log
2012-01-01 00:00:00.000000000 +0100
所以,現在我們知道如何獲取文件的年齡,問題是如何將其轉換為三個字母的月份名稱。最簡單的是使用date
:
$ date -d "2012-01-01" +"%b"
Jan
結合這兩個命令給出:
$ date -d "$(stat -c %y 20120322222.log)" +"%b"
Jan
因此,考慮到這一點,您可以將腳本編寫為:
#!/usr/bin/env bash
BASE_DIR=/home/Work/LogFiles
## Find those files that are older than a month
find "$BASE_DIR" -maxdepth 1 -mtime +30 -type f -name "20*" |
while IFS= read -r file; do
## Get the file's modification year
year="$(date -d "$(stat -c %y "$file")" +%Y)"
## Get the file's modification month
month="$(date -d "$(stat -c %y "$file")" +%b)"
## Create the directories if they don't exist. The -p flag
## makes 'mkdir' create the parent directories as needed so
## you don't need to create $year explicitly.
[[ ! -d "$BASE_DIR/$year/$month" ]] && mkdir -p "$BASE_DIR/$year/$month";
## Move the file
mv "$file" "$BASE_DIR/$year/$month"
done
上面的腳本假設您想要取得檔案的真實修改日期,而不是解析名稱。如果您想解析名稱,請告訴我,我將進行相應修改。
答案2
感謝 terdon:我設法獲取他/她的腳本並對其進行修改,以便它可以在 OS X 中運行。
#!/usr/bin/env bash
BASE_DIR=/Users/user/
## Find those files that are older than a month
find "$BASE_DIR" -maxdepth 1 -type f |
while IFS= read -r file; do
## Get the file's modification month
month="$(stat -f '%Sm' -t '%m' "$file")"
## Get the file's modification day
day="$(stat -f '%Sm' -t '%d' "$file")"
## Get the file's modification hour
hour="$(stat -f '%Sm' -t '%H' "$file")"
## Create the directories if they don't exist. The -p flag
## makes 'mkdir' create the parent directories as needed so
## you don't need to create $year explicitly.
[[ ! -d "$BASE_DIR/$month/$day/$hour" ]] && mkdir -p "$BASE_DIR/$month/$day/$hour";
## Move the file
mv "$file" "$BASE_DIR/$month/$day/$hour"
done
答案3
這是我的解決方案:
#!/bin/bash
BASE_DIR="${1}"
if [ -z "${BASE_DIR}" ]; then
BASE_DIR="/home/Work/LogFiles"
fi
if [ ! -d "${BASE_DIR}" ]; then
echo "Error: '${BASE_DIR}' does not exists." >2
exit 1
fi
declare -a MONTH_NAMES
MONTH_NAMES[1]='JAN'
MONTH_NAMES[2]='FEB'
MONTH_NAMES[3]='MAR'
MONTH_NAMES[4]='APR'
MONTH_NAMES[5]='MAY'
MONTH_NAMES[6]='JUN'
MONTH_NAMES[7]='JUL'
MONTH_NAMES[8]='AUG'
MONTH_NAMES[9]='SEP'
MONTH_NAMES[10]='OCT'
MONTH_NAMES[11]='NOV'
MONTH_NAMES[12]='DEC'
find "${BASE_DIR}" -maxdepth 1 -mtime +30 -type f -name '*.log' \
| grep -e '/[0-9]*.log$' \
| while read FILE; do
FILENAME="$(basename "${FILE}")"
FILE_YEAR="$(echo "${FILENAME}" | cut --bytes=1-4)"
FILE_MONTH="$(echo "${FILENAME}" | cut --bytes=5-6)"
FILE_MONTH_NAME="${MONTH_NAMES[${FILE_MONTH}]}"
REPORT_DIR="${BASE_DIR}/${FILE_YEAR}/${FILE_MONTH_NAME}"
test -d "${REPORT_DIR}" || mkdir -p "${REPORT_DIR}"
mv "${FILE}" "${REPORT_DIR}"
echo "'${FILENAME}' moved to '${REPORT_DIR}'"
done