Duchs

From LQWiki
Jump to navigation Jump to search

This command is non-standard but can be very useful the command originated from the O'Rielly book 101 linux server hacks.

it requires a working version of perl.


#!/bin/sh
ls --color=no . |perl -npe "s/ /\\ /g;s/\n/\0/g;" | 
xargs -0 du -bcs -- |sort -rn |head $* | 
perl -ne '($s, $i) = split(/[^\d]/, $_,2);if ($s<1024) {$a=$s;$u="b"} \
    elsif (($s >=1024) && ($s < 1048576)) {$u="kb"; $a=$s/1024;} \
    elsif (($s >=1048576) && ($s < 1073741824)) {$a=$s/1048576;$u="mb"}\ 
    else {$a=$s/1073741824;$u="gb";}printf("%03.2f%s %s",$a,$u,$i);'

ED Note: you may need to remove the \'s at the ends of the lines and make it one line when saving to a file


The internals of the command itself are not very pretty, but it uses du to get the size of each directory in the current one, then uses the program sort to sort all of the entries and then runs the whole thing through head to print the top 9 directories, to print more simply run the command like this (also here is a sample output)


ryan@ryan /tmp $ duchs -n 15
4.17gb total
1.95gb Halo_Combat_Evolved-FLT
626.68mb cdtemp
361.38mb Program Files
155.15mb SavageDemoInstaller-Linux.zip
145.67mb tmp
64.47mb mistlands
54.52mb Twisted-1.0.8alpha3
36.85mb mistdemo
34.27mb DX23ec.tmp
33.66mb galeon-1.3.8
29.22mb mistlands-tp1.linux.i386.tar.gz
22.64mb NVIDIA-Linux-x86-1.0-4496-pkg2
20.52mb thg10_athlon64_video.avi
19.78mb thg_video_10_athlon64.zip

the options to it get passed immediately to the head command.