#!/bin/bash

echo "请您输入你想删除的文件个数(时间越早的文件靠前):"
read file_num

total_file_num=`ls -l | grep "^d" | wc -l` # 统计目录文件的个数
if (( $file_num > $total_file_num ))
    then echo "您要删除的文件数目太多!"
else
    ls -ltr | grep -v 'total' | grep "^d" | awk '{print $9}' | head -n $file_num | xargs rm -rf
    echo "删除成功!"
fi

# grep -v 'total' :去除ls中显示“total”的行
# grep "^d" :把目录文件输出

Logo

更多推荐