Here's a handy shell function called c
for people who cd
(I first saw Marc Brumlik's posting of it on Usenet years ago, as a
C shell alias.
He and I have both made some changes to it since then.)
This function is great for shells that don't have
filename completion (9.8
This function works a bit like
but it's faster because the "initials" only match directories - and you don't
have to press TAB or ESC after each part of the pathname.
Instead, you just type the initials (first letter, or more) of each directory
in the pathname.
Start at the root directory.
Put a dot (
) after each part.
Here are three examples.
The first one shows that there's no subdirectory of root whose name
starts with q
The second one matches the directory /usr/include/hsfs
c: no match for /q*/.
In the next example, trying to change to /usr/include/pascal
the abbreviations aren't unique the first time.
The function shows me all the matches; the second time, I add another letter
("a") to make the name unique:
c: too many matches for u.i.p.:
/usr/include/pascal/. /usr/include/pixrect/. /usr/include/protocols/.
The Bourne shell function is straightforward; it's shown below.
The C shell alias needs some trickery, and there are two versions of it
on the Power Tools disk: one if you already have an alias for cd
and another if you don't.
(The C shell
used in the c
alias won't work with a cd
Although the csh
manual page admits it won't work, I'd call that another
C shell bug (47.2
# Delete dots. Surround every letter with "/" and "*".
# Add a final "/." to be sure this only matches a directory:
dirpat="`echo $dir | sed 's/\([^.]*\)\./\/\1*/g'`/."
# In case $dirpat is empty, set dummy "x" then shift it away:
set x $dirpat; shift
# Do the cd if we got one match, else print error:
if [ "$1" = "$dirpat" ]; then
# pattern didn't match (shell didn't expand it)
echo "c: no match for $dirpat" 1>&2
elif [ $# = 1 ]; then
echo "c: too many matches for $dir:" 1>&2
ls -d "$@"
unset dir dirpat
The function starts by building a wildcard pattern to match the
For example, if you type
makes the pattern
Next, the shell expands the wildcards onto its command-line parameters;
the trailing dot makes sure the pattern only matches a directory (as in
If the Bourne shell can't match a wildcard pattern, it leaves the
pattern unchanged; the first
test spots that.
If there was just one match, there'll be one command-line parameter
left, and the shell cd
Otherwise, there were too many matches; the function shows them so you
can make your pattern longer and more specific.