[nycbug-talk] fun one liners

George Georgalis george at galis.org
Thu Jun 8 16:47:32 EDT 2006


On Thu, Jun 08, 2006 at 11:43:52AM -0401, Ray Lai wrote:
>On Thu, Jun 08, 2006 at 11:38:27AM -0400, Pete Wright wrote:
>> hey all,
>> so i've been following a thread on the freebsd-stable@ list where a user
>> was having problems finding which files are open by a given process.
>> well...this lead to a lengthy discussion where everyone seemed to reply
>> /usr/port/sysutils/lsof ;)
>> 
>> although one guy posted this:
>> % fstat | grep 'httpd.*/var ' | awk '{print $6}' | xargs -n 1 sudo find \
>> -x /var -inum | sort -u
>> %
>> 
>> now that's a fun oneliner i've been missing for a while, which leads to
>> think: what other good oneliners do peep's on talk@ have lurking in their
>> $HOME's....
>
>I frequently use:
>
>	$ du -ks * 2>/dev/null | sort -n
>
>to find out which directories are hogging up the most space.

for that I use a "two line" function (bourne
compatible), dusum, shows the cumulative sum and
each file/dir size:

dusum () { # sort $1 (defaults to $PWD) according to disk use, also show cumulative sum. 
[ -z "$1" ] && d="./" || d="$1"
find "$d" -maxdepth 1 -mindepth 1 -print0 \
        | xargs -0 du -sk | sort -n \
        | awk '{sum += $1; printf "%+11sk %+10sk %s %s %s\n", sum, $1, $2, $3, $4}' ;}


Here's another function, for reversing octets

ptr () { # reverse a dotted quad or subnet
rev="`echo "$1" | cut -d\. -f1`.$2" ; ip="`echo "$1" | cut -d\. -f2-`"
[ "$ip" = "$1" ] && echo "${rev}" || ptr $ip $rev ;}

arpa () { # use ptr to reverse a network, and add in-addr.arpa
[ -z "$1" ] && echo -n An ip address on the command line returns its\ 
echo "`ptr "$1"`in-addr.arpa." ;}


and my all time favorite, dirper, show the perms of
file $1 (or $PWD) and all the parent directories.
(if you don't use Linux you can take out 3 lines)

dirper () { # reveal dir permissions of "$*" or "$PWD"
d="$*";d="${d#./}";[ -z "$d" -o "$d" = "." -o "$d" = "./" ] && d="$PWD"
[ "`dirname "$d"`" = '.' ] && d="$PWD/$d";
case "`uname`" in Linux) ls -Ldl --full-time "$d" ;; *) ls -LTdl "$d" \
| awk '{printf "%-10s %+8s:%-8s %+8s %8s %2s %3s %s ", \
        $1, $3, $4, $5, $8, $7, $6, $9}' ; ls -Ld "${d}"
;; esac ; [ "$d" = "/" ] && return || dirper `dirname "$d"`;}


// George


-- 
George Georgalis, systems architect, administrator <IXOYE><
http://galis.org/ cell:646-331-2027 mailto:george at galis.org



More information about the talk mailing list