这是我写的一个Shell脚本,可以得到文件夹内七天前的文件,但包含当前文件夹的目录。
#!/bin/bash
#文件夹目录 多个以空格分隔
dir_array=(/root/logs /data/PVsor)
#天数
creation_time=7
for((i=0; i<${#dir_array[*]}; i++))
do
find .${dir_array[i]} -type f -mtime +"$creation_time" -name "*.*"
#date=`ls -l ${dir_array[i]} --time-style=full-iso|awk '{print $6,$7;}'|sed -e 's/\..*//' -e 's/[- :]//g'`
echo ${dir_array[i]}
#echo ${date}
echo the dir name is ${dir_array[i]}
done
本人shell是个初学的新手,求各位赐教,谢谢!
这是我根据你的需求自己写的脚本,没有经过测试,但是思路应该是没问题的,可以参考参考。。
#!/bin/bash
###获取当前月份###
month_now=`date |awk '{print $2}'`
###创建函数,可筛选指定目录下的文件,超过七天并且不是本月的做处理###
function screen_file () {
###先筛选出目录下超过七天的文件###
find $1 -type f -mtime +7|while read line
do
###进入目录中,方便相对路径的操作###
cd $1
###获取被筛选出文件的修改月份,并做相应处理###
month_file=`ll ${line} |awk '{print $6}'`
change_time=`stat ${line} |grep Change |awk '{print $2}'`
if [[ ${month_file} !== ${change_time} ]];then
if [ -d ${change_time} ];then
mv $line ${change_time}
else
mkdir ${change_time}
mv $line ${change_time}
fi
fi
done
}
###调用该函数去处理文件###
dir_array=(/root/logs /data/PVsor)
for((i=0; i<${#dir_array[*]}; i++))
do
screen_file ${dir_array[i]}
done
#!/bin/bash
###获取当前月份###
month_now=`date |awk '{print $2}'`
###创建函数,可筛选指定目录下的文件,超过指定天数天并且不是本月的做处理###
function screen_file () {
###先筛选出目录下超过指定天数天的文件###
###$1表示传入函数的第一个参数 $2表示第二个###
find $1 -type f -mtime +$2 |while read line
do
###进入目录中,方便相对路径的操作###
cd $1
###获取被筛选出文件的修改月份,并做相应处理###
month_file=`ll $line |awk '{print $6}'`
###stat $line 获取文件信息 'NR==7{print $2}' 获取第七行第二列的日期###
change=`stat $line |awk 'NR==7{print $2}'`
###截取字符串只得到年月 其中的 0 表示左边第一个字符开始 7表示字符的总个数###
change_time=${change:0:7}
if [[ ${month_file} != ${change_time} ]];then
if [ -d ${change_time} ];then
mv $line ${change_time}
else
mkdir ${change_time}
mv $line ${change_time}
fi
fi
done
}
###调用该函数去处理文件###
###要删除几天前的文件———天数的数组
creation_time=(10 15)
###目标文件夹数组 多个以空格分隔###
dir_array=(/root/tools /root/logs)
for((i=0; i<${#dir_array[*]}; i++))
do
screen_file ${dir_array[i]} ${creation_time[i]}
done