HOME 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 "-".
The "~" character, or tilde is shorthand for $HOME, our home directory.
The "~" is useful for other purposes as well. For instance:
will copy a file to your home directory, no matter where you are.
will copy your profile to wherever you happen to be.
If a user named "harry" is on our system, we can type
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
For example, if we'd last typed
Then we could jump over to /usr/test/lib by entering
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:
you can "cd" to any of the subdirectories below the directories in this list by
If you're in $HOME, and you type
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
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
Also note that $CDPATH works for partial pathnames too. If you have a
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
and stop there, before entering the text, and then type the escape key twice, the shell completes the pathname for us:
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
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.
HOME Object Libraries ctags Title Bar