Unix Articles


HOME Object Libraries ctags Title Bar

INDEX

HOME
Content...
About this site...
Object Libraries
ctags
Title Bar

File System Shortcuts for cd


There are a surprising number of ways to get from one Unix filesystem directory to another in most popular Unix shell programs.

If you use ksh, the Korn shell from AT&T, or bash, the "Bourne again" shell from the GNU Project, here are some handy ways to move between distant directories in a hurry.

If you've ever been tempted to write a dozen or more aliases to move among different directories, here are some more portable ways to accomplish the same result!

-

One of the most useful is also one of the most simple: the minus sign "-". Entering cd - is an instant trip back to the last directory we used. Entering it a second time "toggles" us back again. This is a speedy way to work between two directories.

~

The "~" character, or tilde is shorthand for $HOME, our home directory. cd ~ is a quick trip home. Then again, cd with no arguments is even faster.

The "~" is useful for other purposes as well. For instance:

cp testfile ~

will copy a file to your home directory, no matter where you are.

cp ~/.profile .

will copy your profile to wherever you happen to be.

~user

If a user named "harry" is on our system, we can type cd ~harry to hop over to Harry's home directory (providing his permissions allow it). To see what files he has there, we could use ls -a ~harry.

cd [new] [old]

This one takes some explaining, but those who do a lot of climbing around in complex filesystems may find it handy. This form of "cd" takes two arguments. The first argument is a string to insert in the previous cd [whatever] command, and the second argument is the string we want it to replace.

For example, if we'd last typed

cd /usr/local/lib

Then we could jump over to /usr/test/lib by entering

cd test local


$CDPATH

Another extremely useful feature of ksh, bash,, csh, and tcsh is the shell variable $CDPATH ($cdpath for csh and tcsh). This variable is similar to $PATH in that it holds a list of directories.

What it provides is a list of paths that the "cd" command will search for whatever subdirectory you provide as its argument.

If you have the following defined ( in .kshrc or .bashrc ), for instance:

export CDPATH="$HOME/:/usr/local/:/home/devel/:/usr/vue/"

you can "cd" to any of the subdirectories below the directories in this list by typing cd [subdirectory] without typing the entire path - regardless of your current location.

If you're in $HOME, and you type cd lib, "cd" will look in $HOME for a lib directory first. If it doesn't see one there, it will look in /usr/local/ next. Since most systems have a directory named /usr/local/lib, this is our destination. Of course, it always helps to check with pwd to make certain.

What happens if we have a local directory with the same name as one of the others defined in $CDPATH?

What I have found is that the rule, at least for ksh and bash is

  1. If you put a trailing slash "/" on each path in $CDPATH, you'll "cd" to the local directory.
  2. If you don't put a trailing slash on each pathname, you'll "cd" to the first pathname in the $CDPATH list that contains a matching subdirectory.

The point here is that it's a good idea to use those trailing slashes if you want to change to local subdirectories first.

The csh shell has a different format for shell variable definitions. The equivalent example would be

set cdpath = ( $home /usr/local/ /home/devel/ /usr/vue/ )

Also note that $CDPATH works for partial pathnames too. If you have a /usr/local/lib/X11 directory, cd lib/X11 is a shortcut to this location for the given example.


Pathname Completion

The shells ksh and bash both provide another useful feature, which, among other things, can help us get across directories. These shells have the ability to "guess" the directory name you're typing after you've typed the first few characters. Here's an example. In ksh, if we enter

cd /usr/loc

and stop there, before entering the text, and then type the escape key twice, the shell completes the pathname for us:

cd /usr/local/

and the cursor waits at the end of the line for us to add the next part of the path. After some practice, pathname and filename completion can save a lot of keystrokes!

What happens if there are two or more directories that match the part we've typed? For instance, if we enter

cd /usr/lo

and type the escape key twice, no completion is performed. This will happen whenever the text we've entered matches two or more possible directories in the given path.

In this case, we can type the sequence [esc][=] instead. The following text is then displayed:

$ cd /usr/lo      
1) local/
2) lost+found/
$ cd /usr/lo

what's happened is that a list of the possible selections that match the text is displayed. The unfinished command is reprinted, and the cursor waits for us to add some additional characters so we can complete enough of the pathname for completion to work properly.

bash needs no equivalent for the [esc][=] sequence, since it will automatically generate a list of possible entries if more than one exists:


bash$ cd /home/j
john jrl jsiler
bash$ cd /home/j

Admittedly, it's still handy to have a well-named alias or two to allow moving down well traveled filesystem paths quickly. Given the above selections, though, we can save a lot of time writing them, and fly across breathtakingly large paths with comparative ease.


The above text Copyright © 1997 by Scott Chilcote.

HOME Object Libraries ctags Title Bar