ECN No Name Newsletter: September, 1987

The ECN No Name Newsletter is no longer being published. This is an archived issue.

[previous article] [next article]

The BACK Command Toggling Between Directories

Tim Kechkaylo

Were you ever many levels deep in directories, and briefly needed to move into another directory? The first time you typed the necessary pathname to return you to your former working directory, it was tedious but OK. But if you end up popping back and forth between the two directories a few times, all that typing of long pathnames begins to be a drag. Well, don't do it anymore.

You can create an alias, back, that will keep track of the previous pathname. This alias will return you to the previous directory from which you issued the last cd (change directory) command. Repetitive use of the back command will allow you to toggle between the two working directories. HOWEVER, before you can use this nifty alias, it is necessary for you to be running csh (C shell) to make it operational. If you are unsure whether you are using csh, see your site specialist.

To activate this option, place these two lines in your.cshrc file in your HOME directory:

  alias cd      'set old=$cwd; chdir \!*'
  alias back    'set back=$old; set old=$cwd;
                cd $back; unset back; dirs'

Note: In order for these aliases to take effect, type
        source.cshrc   or login again.

How does this alias work? The placement of the above commands in your.cshrc file causes the pathname of the directory you are in to be stored when you issue the cd command. Later, all that is necessary to return to the previous directory is the command back. The system will respond with the pathname of the directory to which you have returned; replacing the HOME directory portion of the pathname with a ~. For example, you log in and move from your HOME directory into the directory "math153" with:

  % cd /a/your.login/fall.87/class/math153

Then you move to the "engr100" directory, type:

  % cd /a/your.login/spring.86/engr100
  % vi file             (or anything else)

You to return to the "math153" directory, type:

  % back
  ~/fall.87/class/math153
  % vi homework1        (or something else)

When you return to the "engr100" directory, type:

  % back
  ~/spring.86/engr100
  % cat file            (or anything else)

Returning again to the "math153" directory, type:

  % back
  ~/fall.87/class/math153

There you are! A quick, simple and nifty way to save your fingers and your blood pressure.

More advanced UNIX csh users may discover, the standard pushd (push directory) and popd (pop directory) commands to be of greater benefit than the back alias. The use of the pushd command allows a user to form a directory stack as you move from one directory to another directory. Later a user can relocate immediately to any directory in the "stack" by use of the popd command. More information about how to use these commands is available in the UNIX User's Supplementary Documents (USD) page 4-21 or referencing the csh manual page as found online or in the UNIX Programmer's Manual.


webmaster@ecn.purdue.edu
Last modified: Saturday, 01-Nov-97 12:18:06 EST

[HTML Check] HTML