Sort directories by number of files contained

Sat, 2009-05-09

Here is a one-liner which sorts all the directories under ‘/’ by the number of files contained:

find / -type d -exec sh -c 'ls -a1 "$1" | wc -l' \"{}\" {} \; -print | \
sed  '$!N;s/\n/ /' | sort -gr

I recently needed this on a linux server where the inode quota was exceeded. Through the one-liner I discovered where most of the inodes where consumed.

Cycle through the last argument of previous commands in Bash

Wed, 2009-01-07

In Bash, when I want to repeat the last argument of the previous command, I usually type !$. I just discovered that you can also use ALT+. to cycle through the last argument of previous commands.

ESC-g for glob expansion in bash

Tue, 2009-01-06

Just like <TAB> expands a filename given a prefix you can also expand a file name given the middle part of a file name.


$ ls
abc1def  abc2def  abc3def  abc4def  abc5def
$ ls *2*<ESC>g

Which will expand to:

$ ls abc2def

via a stackoverflow question

Bash extended pattern matching

Sun, 2008-09-14

If you don’t have extended pattern matching enabled you can enable it with shopt -s extglob

From the bash manual page:

If the extglob shell option is enabled using the shopt builtin, several
extended pattern matching operators are recognized. In the following
description, a pattern-list is a list of one or more patterns separated
by a |. Composite patterns may be formed using one or more of the fol‐
lowing sub-patterns:

Matches zero or one occurrence of the given patterns
Matches zero or more occurrences of the given patterns
Matches one or more occurrences of the given patterns
Matches one of the given patterns
Matches anything except one of the given patterns


$ touch abc1d abc2d abcd abc11d abc3d
$ ls
abc11d abc1d abc2d abc3d abcd
$ ls abc?(1|2)d
abc1d abc2d abcd
$ ls abc*(1|2)d
abc11d abc1d abc2d abcd
$ ls abc+(1|2)d
abc11d abc1d abc2d
$ ls abc@(1|2)d
abc1d abc2d
$ ls abc!(1|2)d
abc11d abc3d abcd