linux - Define array for walking directories with du -


i wrote basic script test recursive du output against directory or filesystem selects largest directory , repeats, outputs results neatly. there way can combine array , if/then statements make more elegant , continue recurse until no more directories matched, printing outputs array?

#!/bin/bash  dir1=$1 du1=$(du -x --max-depth=1 $dir1 | sort -nr | awk '{ print $2 }' | \     xargs du -hx --max-depth=0 | egrep -v "sys|proc|boot|lost|media|mnt|selinux" | head -10 | tail -n +2)  dir2=$(echo "$du1"|head -1|awk '{print $2}') du2=$(du -x --max-depth=1 $dir2 | sort -nr | awk '{ print $2 }' | \     xargs du -hx --max-depth=0 | egrep -v "sys|proc|boot|lost|media|mnt|selinux" | head -10 | tail -n +2)  dir3=$(echo "$du2"|head -1|awk '{print $2}') du3=$(du -x --max-depth=1 $dir3 | sort -nr | awk '{ print $2 }' | \     xargs du -hx --max-depth=0 | egrep -v "sys|proc|boot|lost|media|mnt|selinux" | head -10 | tail -n +2)  dir4=$(echo "$du3"|head -1|awk '{print $2}') du4=$(du -x --max-depth=1 $dir4 | sort -nr | awk '{ print $2 }' | \     xargs du -hx --max-depth=0 | egrep -v "sys|proc|boot|lost|media|mnt|selinux" | head -10 | tail -n +2)  dir5=$(echo "$du4"|head -1|awk '{print $2}') du5=$(du -x --max-depth=1 $dir5 | sort -nr | awk '{ print $2 }' | \     xargs du -hx --max-depth=0 | egrep -v "sys|proc|boot|lost|media|mnt|selinux" | head -10 | tail -n +2)  dir6=$(echo "$du5"|head -1|awk '{print $2}') du6=$(du -x --max-depth=1 $dir6 | sort -nr | awk '{ print $2 }' | \     xargs du -hx --max-depth=0 | egrep -v "sys|proc|boot|lost|media|mnt|selinux" | head -10 | tail -n +2)  echo -e "##level1##" paste -d ' ' <(echo "$du1") <(echo "$(file $(echo "$du1" | \     awk '{print $2}')|cut -d' ' -f2- | sed -e 's/[a-za-z0-9]/[&/' -e 's/$/]/')") echo -e "##level2##" paste -d ' ' <(echo "$du2") <(echo "$(file $(echo "$du2" | \     awk '{print $2}')|cut -d' ' -f2- | sed -e 's/[a-za-z0-9]/[&/' -e 's/$/]/')") echo -e "##level3##" paste -d ' ' <(echo "$du3") <(echo "$(file $(echo "$du3" | \     awk '{print $2}')|cut -d' ' -f2- | sed -e 's/[a-za-z0-9]/[&/' -e 's/$/]/')") echo -e "##level4##" paste -d ' ' <(echo "$du4") <(echo "$(file $(echo "$du4" | \         awk '{print $2}')|cut -d' ' -f2- | sed -e 's/[a-za-z0-9]/[&/' -e 's/$/]/')") echo -e "##level5##" paste -d ' ' <(echo "$du5") <(echo "$(file $(echo "$du5" | \     awk '{print $2}')|cut -d' ' -f2- | sed -e 's/[a-za-z0-9]/[&/' -e 's/$/]/')") echo -e "##level6##" paste -d ' ' <(echo "$du6") <(echo "$(file $(echo "$du6" | \     awk '{print $2}')|cut -d' ' -f2- | sed -e 's/[a-za-z0-9]/[&/' -e 's/$/]/')") 

here example output:

#./rdu.sh / 2>/dev/null ##level1## 12g     /opt  [directory] 1.9g    /usr  [directory] 452m    /var  [directory] 352m    /root [directory] 179m    /home [directory] 116m    /lib  [directory] 46m     /tmp  [sticky directory] 28m     /sbin [directory] 21m     /etc  [directory] ##level2## 8.5g    /opt/zenoss [directory] 2.9g    /opt/zends  [directory] ##level3## 6.6g    /opt/zenoss/perf     [directory] 510m    /opt/zenoss/zenpacks [directory] 486m    /opt/zenoss/var      [directory] 461m    /opt/zenoss/lib      [directory] 250m    /opt/zenoss/log      [directory] 85m     /opt/zenoss/products [directory] 49m     /opt/zenoss/packs    [directory] 31m     /opt/zenoss/share    [directory] 26m     /opt/zenoss/webapps  [directory] ##level4## 6.5g    /opt/zenoss/perf/devices [directory] 59m     /opt/zenoss/perf/daemons [directory] ##level5## 289m    /opt/zenoss/perf/devices/10.0.4.218                    [directory] 288m    /opt/zenoss/perf/devices/10.215.68.9                   [directory] 287m    /opt/zenoss/perf/devices/10.0.4.18                     [directory] 161m    /opt/zenoss/perf/devices/<removed>                     [directory] 145m    /opt/zenoss/perf/devices/10.219.68.12                  [directory] 143m    /opt/zenoss/perf/devices/vms--                         [directory] 143m    /opt/zenoss/perf/devices/10.0.4.219                    [directory] 143m    /opt/zenoss/perf/devices/10.0.4.19                     [directory] 136m    /opt/zenoss/perf/devices/10.215.68.8                   [directory] ##level6## 279m    /opt/zenoss/perf/devices/10.0.4.218/ltmvirtualservers [directory] 7.1m    /opt/zenoss/perf/devices/10.0.4.218/os                [directory] 888k    /opt/zenoss/perf/devices/10.0.4.218/hw                [directory] 840k    /opt/zenoss/perf/devices/10.0.4.218/loadbalancerports [directory] 

you code doesn't work on system, cannot test it. can this:

function durec {     dir1=$1     level=$2     du1=$(du -x --max-depth=1 $dir1 | sort -nr | awk '{ print $2 }' | \         xargs du -hx --max-depth=0 | egrep -v "sys|proc|boot|lost|media|mnt|selinux" | head -10 | tail -n +2)     echo -e "##level$level##"     paste -d ' ' <(echo "$du1") <(echo "$(file $(echo "$du1" | \         awk '{print $2}')|cut -d' ' -f2- | sed -e 's/[a-za-z0-9]/[&/' -e 's/$/]/')")     let level++     dir2=$(echo "$du1"|head -1|awk '{print $2}')     if [ ! -z "$dir" ];         durec $dir2 $level     fi   }  # call function durec / 1 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -