Project13.Change Your Working Directory


Project 13. Change Your Working Directory

"Can I reduce the amount of typing I have to do to change my current working directory?"

This project shows you how to squeeze the last drop from the cd command. It covers the use of aliases with command cd, the cd path, and some quick cd TRicks.

Popular Destinations

You probably have a number of frequently visited directories, so speeding the process of getting to them can save time and typing. Here are a few ways of doing this in which we use the directory /var/log/httpd as an example.

Use Aliases

The simplest form of shortcut is to define an alias that switches you to a frequently visited directory.

$ alias cdhtl='cd /var/log/httpd' $ cdhtl $ pwd /var/log/httpd


Another method is to make an alias for the directory. Unfortunately, this will not work, because only the first word on a command line is a candidate for alias expansion, whereas our alias is the second word. The alias htl will not be expanded in this example.

$ alias htl='/var/log/httpd' $ cd htl -bash: cd: htl: No such file or directory


We can employ a clever trick, however, that defines an alias for the cd command itself, the alias being defined as cd followed by a space.

$ alias cd='cd' $ cd htl $ pwd /var/log/httpd


Why this works is not obvious; it's explained in Project 51.

Learn More

Project 51 covers aliases.


Teleport Using a CD Path

When given a relative pathname, the cd command will assume that the pathname is relative to the current working directory. Bash lets you specify a number of directories that the cd command should try instead. Set the variable CDPATH to contain a colon-separated list of directories. You must add dot to the start of the list; otherwise, the current directory will be tried last, which can be confusing. The cd command considers the directories in the order in which they are listed, stopping at the first for which the relative pathname leads to a valid directory. Let's set the CDPATH.

$ CDPATH=".:/var/log:~/Documents"


Next, change to the directory /var/log/httpd by typing simply

$ cd httpd /var/log/httpd


This works because /var/log is in the search path.

Similarly, the directory Letters is in ~/Documents, so we need type only

$ cd Letters /Users/saruman/Documents/Letters


Beware Tilde Expansion

Follow the example below, and consider why the alias works but the shell variable doesn't. We have defined an alias and a shell variable to help move quickly to the directory ~/Documents.

$ alias docs alias docs='cd ~/Documents' $ echo $docs ~/Documents


Try the alias method

$ docs $ pwd /Users/saruman/Documents


Try the variable method

$ cd $docs -bash: cd: ~/Documents: No such file or directory


Learn More

Project 14 discusses more directory-navigation tricks.


Why does the second method fail? The shell is doing a lot of expansionaliases, tilde (~) expanded to your home directory, and variables, and in that order. The alias docs is expanded to include a tilde and then the tilde is expanded to be the full pathname of your home directory. The variable docs, however, expands to include a tilde after tilde expansion has been completed; it's too late now to expand the tilde itself.

If there's a moral to this example, it's to avoid setting up shortcuts that themselves contain shortcuts. You'll have fewer unpleasant surprises if the contents of aliases and variables are fully expanded as they are assigned, so they don't have to be expanded as they are used.

cd Quickies

Finally, some common cd tricks are listed in Table 2.1.

Table 2.1. Features of cd

Command

Effect

cd or cd ~

Change to the directory given by $HOME

cd ~username

Change to the home directory of user username

cd - or cd ~-

Change to the previous working directory given by $OLDPWD

cd -P

Try cd -P /etc versus cd /etc and issue command pwd

cd directory

Set $PWD to be directory

help cd

Display information on Bash's built-in command cd


Learn More

Refer to "Bash Initialization" in Project 4 to learn more about the Bash startup sequence.


Make It So

To set shell options and environment variables permanently, place the commands in a Bash startup file. To change settings for all users, put the commands in /etc/bashrc and ensure that /etc/profile sources /etc/bashrc. To change settings for just yourself, put the commands in ~/.bashrc and ensure that ~/.bash_profile sources ~/.bashrc.




Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net