linux下遍历目录和文件,删除过期天数目录或文件shell脚本

来源:互联网 发布:软件交易平台哪个最好 编辑:程序博客网 时间:2024/05/19 23:17
#!/bin/bashfunction delete_file(){INTERVAL=$(($1*3600*24))pathdir=$2/    #echo $INTERVAL#echo $pathdirnow_timestamp=$(date -d "$(date +"%Y-%m-%d %T")" +%s) #echo $now_timestamp    files=$(ls $pathdir)for file in $files;    do#file_date=$(stat $pathdir$file | grep Modify | awk '{print $2,$3}');file_date=$( stat $pathdir$file | tail -2|head -1 | awk '{print $1,$2}'| cut -c16- )#echo $file_datefile_timestamp=$(date -d "$file_date" +%s)#echo $file#echo $file_timestampif [ $? -ne 0 ];                then                        file_path=$file                        echo "delete file 0 $pathdir$file_path"rm -rf $pathdir$file_path                        continue                fiif [ $(($now_timestamp - $file_timestamp)) -gt $INTERVAL ]thenfile_path=$fileecho "delete file 1 $pathdir$file_path"rm -rf $pathdir$file_path        fidonereturn 0;}function getdir(){    for element in `ls $2`    do          dir_or_file=$2"/"$element        if [ -d $dir_or_file ]        then            echo $dir_or_file            delete_file $1 $dir_or_file            getdir $1 $dir_or_file        else    #delete_file $2 $dir_or_file            echo "clear finished"        fi      done    return 0;}root_dir="."function main(){    getdir $1 $root_dir    ret=$?    echo "getdir run ret:$ret"}main $@

使用时shell输入参数删除当前目录7天前的目录或文件

./delest_images 7


阅读全文
0 0
原创粉丝点击