列出占用磁盘空间较多的目录

来源:互联网 发布:地理信息大数据 编辑:程序博客网 时间:2024/04/30 16:58

列出某一目录下,空间用量超过指定大小的子目录。

chk_dir_size.sh:

 1 #! /bin/bash  2  3 shopt -s -o nounset  4 DIR=${1:?'Please enter the path you want to check,for example:/var'}  5 if [[ ! $DIR == /* ]]; then  6                   DIR=/$DIR  7 fi  8  9 declare -i size SIZE 10 11 #Only list the directory when the using space bigger than X Mb 12 SIZE=50 13 14 while read size dir 15 do 16         if [ $size -gt $SIZE ];then 17                 echo -e "$size\t\t$dir" 18         fi 19 done < <(find $DIR -mindepth 1 -type d -exec du -sm {} \;)


行5-7,如果DIR存储的路径不是以/开头,就帮它加上/,使之成为绝对路径。

行12 ,凡是超过50M以上的子目录,均列出。

行16-20,使用一个while循环来处理find指令交给read读取的每一行数据,其格式为: 用量    路径名称     

 如下图所示:

 

行16,read读入数据行后,子目录的空间用量存入size变量中,子目录的绝对路径存入dir变量中。

行19,使用进程替换技巧。find由DIR的“下一层”目录开始找起。

 -mindepth 1,是指不处理最上层的目录,如指定从var开始找,-mindepth 1会忽略/var本身,而是从/var的下一层开始找起。

-type d是指想要寻找的文件形态为目录。

du -sm,-s指文件大小的总和,-m是采用MB为计量单位。

 

测试结果: