From owner-freebsd-questions@FreeBSD.ORG Wed Apr 6 18:34:52 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 0D455106566B for ; Wed, 6 Apr 2011 18:34:52 +0000 (UTC) (envelope-from peter@vereshagin.org) Received: from mx1.skyriver.ru (ns1.skyriver.ru [89.108.118.221]) by mx1.freebsd.org (Postfix) with ESMTP id 9C9018FC14 for ; Wed, 6 Apr 2011 18:34:51 +0000 (UTC) Received: from localhost (raskin.torservers.net [74.120.15.150]) by mx1.skyriver.ru (Postfix) with ESMTPSA id A996A5AFE for ; Wed, 6 Apr 2011 22:10:32 +0400 (MSD) Date: Wed, 6 Apr 2011 22:34:39 +0400 From: Peter Vereshagin To: freebsd-questions@freebsd.org Message-ID: <20110406183439.GA5257@external.screwed.box> References: <0DD2BF5C-7387-4AFA-BF43-B1683F3773E8@d3photography.com> MIME-Version: 1.0 Content-Type: text/plain; charset=koi8-r Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Organization: ' X-Face: 8T>{1owI$Byj]]a; ^G]kRf*dkq>E-3':F>4ODP[#X4s"dr?^b&2G@'3lukno]A1wvJ_L(~u 6>I2ra/<,j1%@C[LN=>p#_}RIV+#:KTszp-X$bQOj,K Subject: Re: graphical representation of `du` X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 06 Apr 2011 18:34:52 -0000 Nobody knows that you're in for that, freebsd-questions! 2011/04/05 04:12:40 +0400 á×ÓÔÉÎ ëÉÍ => To freebsd-questions@freebsd.org : > Sun, 3 Apr 2011 20:57:24 +0100 ÐÉÓØÍÏ ÏÔ Chris Rees : > > > On 3 April 2011 20:26, á×ÓÔÉÎ ëÉÍ wrote: > > > Sun, 03 Apr 2011 12:01:24 +0200 ÐÉÓØÍÏ ÏÔ David Demelier > > : > > > > > >> On 02/04/2011 19:30, Chris Rees wrote: > > >> > On 2 April 2011 18:22, Chris Rees šwrote: > > >> >> On 2 April 2011 18:07, Mike Jeays šwrote: > > >> >>> On Sat, 2 Apr 2011 17:15:04 +0100 > > >> >>> Chris Rees šwrote: > > >> >>> > > >> >>>> du -h . | awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' | > > >> >>>> awk '{print($2" ["$1"]");}' | sed -e 's,[^-][^/]*/,--,g' -e 's,^,|,' > > >> >>> > > >> >>> > > >> >>> I confess to being impressed... > > >> >>> > > >> >> > > >> >> Yeah, but perhaps I should have used sed instead of the second awk; > > >> >> fewer processes: > > >> >> > > >> >> du -h | awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' | sed > > >> >> -e 's,^[^1-9]*\([^___CTRL-V+TAB______]*\)____CTRL-V+TAB_____*\(.*\)$,\2 > > >> >> \[\1\],;s,[^-][^/]*/,--,g;s,^,|,' > > >> >> > > >> >> That does exactly the same -- šwhere I've put ____CTRL-V+TAB______ you > > >> >> have to type Ctrl-V, then a literal [::tab::] key; BSD sed doesn't do > > >> >> \t. > > >> >> > > >> >> Chris > > >> >> > > >> > > > >> > Final version: > > >> > > > >> > http://www.bayofrum.net/~crees/graphical_du.sh > > >> > > > >> > Maybe I should port it... > > >> > > > >> > > >> Thanks! This rocks! :-) > > >> > > > > > > What a fun thread :) > > > > > > Here's my two cents, written as an sh(1) function that you can tack on to > > the end of your .profile or .shrc: > > > (Caveats: šI'm writing this on a Mac OS X machine, not on a FreeBSD machine, > > at the moment, but hopefully this'll still work. > > > Also, the following will mess up if you have directories whose names begin > > with "|".) > > > > > > # dg: š`du--graphical' > > > # Usage: šdg [dir ...] > > > # Based on script by Chris Rees > > > # 1459 Sunday, 3 April 2011 > > > > > > dg ( ) { > > > šdu -h "$@" | > > > š šawk '{FS="\t"; print $2"\t["$1"]"}' | > > > š šsort | > > > š šsed -e 's:[^/]*/:| :g' -e 's:\(^\(| \)*\)| \([^|].*\):\1+-\3:' > > > šreturn > > > š} > > > > I used the awk a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] etc to > > reverse the order, rather than alphabetise it because it's quicker: > > > > $ du -h . | time sort >/dev/null 2>time > > $ cat time > > 8.17 real 0.03 user 0.00 sys > > $ du -h . | time awk '{a[i++]=$2} END { for (j=i-1; j>=0;) print > > a[j--] }' >/dev/null 2>time2 > > $ cat time2 > > 7.77 real 0.14 user 0.00 sys > > > > YMMV of course! > > > > Chris > > I can't argue with that. If you're a sysadmin and are managing a large system, > the sort could take some time. On the other hand, there are times when a sort > might be useful. Then again, you could always just comment that line out :) > > Which reminds me, my sort line above may not sort intuitively in the case where > directory names contain characters that precede / in the ASCII character set; > for example, "mydir-old" sorts before "mydir/" in ASCII. A quick kludge is to > translate slashes into, oh I don't know, say carriage returns before the sort, > and then translate them back after the sort, as is done below. An inelegant > and inefficient solution, but it works. However, I'm going out on a limb by > assuming users won't be running this script under MS-DOS, where this kludge > wouldn't work. > > Another problem with my script above is that in some cases, if you run it on > multiple arguments, e. g., "dg dir1/subdir dir2/subdir," you can't tell from > the output to which parent directory the subdirectory refers; to deal with > this problem, the revised version below runs du on each argument one at a time. > However, I ended up having to duplicate the main command in the script (once > for "dg" with arguments, and once without), 'cause I'm not clever enough to > figure out a way to combine the two cases into one in time to post this. > > I also had a redundant [^|] in the sed expression which I took out; it > shouldn't be necessary, although the script will still mess up if any directory > names start with "| ". > > Finally, the revised version is repackaged as a proper sh(1) script like your > original script rather than as a function, to make it independent of a user's > particular shell. Obviously further variations and improvements could be made. > > Again I'm away from my FreeBSD machine and am writing this on a Mac OS X > machine; hopefully I didn't break anything. > > #!/bin/sh > # > # dg: `du--graphical' > # Usage: dg [dir ...] > # > # Based on script by Chris Rees > # 1459 Sunday, 3 April 2011 > # > # Modified: 1900 Monday, 4 April 2011 > > if [ "$1" ] > then for i in "$@" > do if [ "$2" ] > then echo > echo $i: > fi > du -h "$i" | > awk '{FS="\t"; print $2"\t["$1"]"}' | > tr / '\r' | > sort | > tr '\r' / | > sed -e 's:[^/]*/:| :g' -e 's:\(^\(| \)*\)| \(.*\):\1+-\3:' > done > else du -h | > awk '{FS="\t"; print $2"\t["$1"]"}' | > tr / '\r' | > sort | > tr '\r' / | > sed -e 's:[^/]*/:| :g' -e 's:\(^\(| \)*\)| \(.*\):\1+-\3:' > fi Again, why don't you guys just use perl to provide a graphical du? I believe perl is just present on every freebsd machine where graphical du is needed. 73! Peter pgp: A0E26627 (4A42 6841 2871 5EA7 52AB 12F8 0CE1 4AAC A0E2 6627) -- http://vereshagin.org