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 DestinationsYou 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 AliasesThe 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
Teleport Using a CD PathWhen 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 ExpansionFollow 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
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 QuickiesFinally, some common cd tricks are listed in Table 2.1.
Learn More
Make It SoTo 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. |