一、定時刪除linux上定時的文件
顯示20分鍾前的文件 find /tmp/ -type f -mmin +20 -exec ls -l {} \; 刪除20分鍾前的文件 find /tmp/ -type f -mmin +20 -exec rm {} \; 顯示20天前的文件 find /tmp/ f -mtime +20 -exec ls -l {} \; 刪除20天前的文件 find /tmp/ -type f -mtime +20 -exec rm {} \;
二、定時刪除HDFS上過時的文件
思路:獲取文件或目錄的修改時間,與設定的過期時間進行比較,對過期文件執行刪除操作即可。
#!/bin/bash source ~/.bashrc #待檢測的HDFS目錄 data1_file=/hive/warehouse/db1/ data1_file=/hive/warehouse/db2/ #將待檢測的目錄(可以為多個)加載至數組中 array_check=($data1_file,$data2_file) # 當前時間戳 today_timestamp=$(date -d "$(date +"%Y-%m-%d %H:%M")" +%s) #Func: 刪除指定時間之前的過期,這里設置的是30天前 removeOutDate(){ hadoop fs -ls $1 > temp.txt cat temp.txt | while read quanxian temp user group size day hour filepath do current_file_time="$day $hour" current_file_timestamp=$(date -d "$current_file_time" +%s) if [ $(($today_timestamp-$current_file_timestamp)) -ge $((30*24*60*60)) ]; then echo "$(date +'%Y-%m-%d %H:%M:%S') $filepath" hadoop fs -rm -r $filepath > /dev/null 2>&1 fi done } #Func: 執行刪除 execute(){ echo -e "\n" echo "$(date +'%Y-%m-%d %H:%M:%S') start to remove outdate files in hdfs" echo "$(date +'%Y-%m-%d %H:%M:%S') today is: $(date +"%Y-%m-%d %H:%M:%S")" for i in ${array_check[@]} do echo "$(date +'%Y-%m-%d %H:%M:%S') processing filepath: $i" removeOutDate $i echo -e "\n" done echo "$(date +'%Y-%m-%d %H:%M:%S') remove outdate files in hdfs finished" echo -e "\n" } # 開始執行 execute
【參考資料】
[1]. 佚名, Linux 刪除指定時間前的文件.
[2]. mengrennwpu, 定時腳本: 刪除HDFS中的過期文件.