本脚对指定目录进行遍历:
1、降序展示指定目录下文件、目录大小
2、对大于5GB的子目录进行递归查询,将子目录下文件和目录的资产占用情况一同打印。
# ./list_store.sh /
Size Path
310G /u01/
310G /u01/app/
30G /opt/
……# ./list_store.sh /u01/app/oracle/oradata/
Size Path
286G /u01/app/oracle/oradata/orcl/
33G /u01/app/oracle/oradata/orcl/test.DBF
32G /u01/app/oracle/oradata/orcl/temp01.dbf
25G /u01/app/oracle/oradata/orcl/system01.dbf
#!/bin/bash
function list_storage(){
TFILE=/tmp/storage.txt;
[ -f /tmp/storage.txt ] && rm -rf /tmp/storage.txt
TFILE2=/tmp/storage2.txt;
TMP_ARG=$1;[[ ! "$1" =~ .*\/$ ]] && TMP_ARG=$TMP_ARG"/"
for f1 in $(ls $TMP_ARG);
do
if [ ! $f1 == "proc" ]; then
if [ -f $TMP_ARG$f1 ];then
du -hs $TMP_ARG$f1 >> $TFILE
else
du -hs $TMP_ARG$f1"/" >> $TFILE
fi
fi
done
egrep -v '^0|[\d.]*K\s*\/' $TFILE |sort -hr > $TFILE2
}
NFILE=/tmp/large_stor.txt
function nested_list(){
cp -a /tmp/storage2.txt /tmp/large_stor2.txt
NFILE2=/tmp/large_stor2.txt
echo "Size Path" >$NFILE;
IFS_OLD=$IFS
IFS=$'\n'
for LINE in $(cat $NFILE2)
do
echo $LINE >> $NFILE
NUM=$(echo $LINE |egrep '^0|[\d.]*G'|awk -F 'G' '{print $1}')
#展示5G大小以上的文件
if [[ "$NUM" != "" && `expr $NUM \> 5` = 1 ]]; then
DIR="$(echo $LINE |awk '{print $2}')"
if [ ! -f $DIR ]; then
list_storage $DIR
cat /tmp/storage2.txt |grep G >> $NFILE
else
du -hs $DIR >> $NFILE
fi
fi
done
IFS=$IFS_OLD
}
function _report(){
cat $NFILE
rm -rf $TFILE $TFILE2 $NFILE $NFILE2
unset TFILE TFILE2 NFILE NFILE2
}
if [ -d $1 ];then
list_storage $1
nested_list
_report
else
du -hs $1
fi;unset list_storage nested_list _report