业务背景
/home/pms目录是工作目录,现在该目录占用硬盘空间过大,需要清理,现在需要列举该目录中所有大于200MB的子文件目录,以及该子文件目录的占用空间
脚本实现
<code class="language-shell hljs apache">du -h --max-depth=10 /home/pms/* | awk '{ if($1 ~ /M/){split($1, arr, "M")}; if(($1 ~ /G/) || ($1 ~ /M/ && arr[1]>200)) {printf "%-10s %s\n", $1, $2} }' | sort -n -r</code> 其中
<code class="language-shell hljs apache"><code class="hljs lasso">du -h --max-depth=10 /home/pms/*</code></code>
结果如下
<code class="language-shell hljs apache"><code class="hljs lasso"><code class="hljs r">$ du -h --max-depth=10 /home/pms/*0 /home/pms/addressCountMap12K /home/pms/bigDataEngine/conf1.7M /home/pms/bigDataEngine/analysis/warning33M /home/pms/bigDataEngine/analysis/log...</code></code></code>
下面这个awk语句,作用是判断第一个参数,进行字符串匹配,如果是M的话,按字符M进行截取
<code class="language-shell hljs apache"><code class="hljs lasso"><code class="hljs r"><code class="hljs ruby">if($1 ~ /M/){split($1, arr, "M")};</code></code></code></code> 下面这个awk语句,作用是判断第一个参数,进行字符串匹配:
M,判断容量是否大于200MB,是则直接输出参数1和参数2
G,直接输出参数1和参数2
<code class="language-shell hljs apache"><code class="hljs lasso"><code class="hljs r"><code class="hljs ruby"><code class="hljs ruby">if(($1 ~ /G/) || ($1 ~ /M/ && arr[1]>200)) {printf "%-10s %s\n", $1, $2}</code></code></code></code></code>输出结果
输出结果<code class="language-shell hljs apache"><code class="hljs lasso"><code class="hljs r"><code class="hljs ruby"><code class="hljs ruby"><code class="hljs r">$ du -h --max-depth=10 /home/pms/* | awk '{ if($1 ~ /M/){split($1, arr, "M")}; if(($1 ~ /G/) || ($1 ~ /M/ && arr[1]>200)) {printf "%-10s %s\n", $1, $2} }' | sort -n -r 1018M /home/pms/recsys/algorithm/schedule/project/mixproduct948M /home/pms/recsys/algorithm/schedule/project/contentbasedrelatedproduct940M /home/pms/recsys/algorithm/schedule/project/view_after_viewing/cf922M /home/pms/new_product_import913M /home/pms/db_engine903M /home/pms/recsys/algorithm/schedule/project/campus862M /home/pms/recsys/algorithm/schedule/project/company/user...</code></code></code></code></code></code>