Monday, March 11, 2013

Disk usage using awk

This command will give you the top 20 files or directories that take the most space in the current directory :

sudo du -k -x --max-depth=1 | sed 's/\.$/Total\n/g' | sort -rn | head -20 | awk 'BEGIN{printf "\n%20s %-30s","Size in Kbytes","file or directory"}{printf "\n%20'\''d %-30s", $1, $2} END{print "\n"}'

Here it is in action :)


> sudo du -k -x --max-depth=1 | sed 's/\.$/Total\n/g' | sort -rn | head -20 | awk 'BEGIN{printf "\n%20s %-30s","Size in Kbytes","file or directory"}{printf "\n%20'\''d %-30s", $1, $2} END{print "\n"}'

      Size in Kbytes file or directory
          12,964,287 Total
             455,377 ./Photos
             238,572 ./realtek-linux-audiopack-5.17
             235,719 ./IPMI
             135,153 ./realtek-linux-audiopack-5.16
             123,731 ./PCoIP_Driver_SW_v3-0-6_Linux(source
             119,884 ./alsa-driver-1.0.25
              97,912 ./pcoip_host_software
              69,968 ./nagios-plugins-1.4.16
              58,653 ./BMC
              53,357 ./PCoIP-Test
              50,866 ./scim-1.4.9
              50,793 ./sunbird
              48,648 ./leostream
              41,160 ./teradici-firmware
              24,886 ./cacti-0.8.8a
              19,548 ./wqy-zenhei-0.9.45
              16,258 ./leolog
              15,888 ./PCoIP_Host_Software_Driver_Linux_r4-0-5
              14,394 ./Leostream

>