home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 14.9 cd by Directory Initials Chapter 14
Moving Around in a Hurry
Next: 14.11 Finding (Anyone's) Home Directory, Quickly
 

14.10 Variables Help You Find Directories and Files

A UNIX system can have hundreds or thousands of directories - and a lot more files. Even if you remember all the pathnames, typing them over and over can be a pain.

Your account probably already has some helpful shell and environment variables (6.8 , 6.1 ) set up. You can add more from the command line or from your shell setup files (2.2 ) (like .cshrc or .profile ). To see what environment variables are set, use the env (System V) or printenv (Berkeley) command. The command set should show shell variables (some of these might be repeated in the environment). Here's part of what happens on my account:

% env


HOME=/home/jpeek
MH=/work/jpeek_mail/.mh_profile
PATH=/home/jpeek/.bin:/home/jpeek/.bin/show:/work/bin:...
RNINIT=/home/jpeek/.rnswitches
PAGER=/usr/local/bin/less
% set


active  /usr/lib/news/active
cwd     /home/jpeek/pwrtools
mail    (60 /usr/mail/jpeek)
maillog /usr/spool/smail/log/logfile

UNIX programs use a lot of those environment variables. For instance, my email system finds its setup file from MH . But I can use environment variables for other things, too. For instance, when I want to edit my email setup file, I can type vi $MH from any directory. The shell expands $MH to /work/jpeek_mail/.mh_profile and starts the editor. Check your environment and see what you've got; the names usually explain the variables pretty well.

The shell uses shell variables like $mail . I can check incoming messages with the command tail $mail[2] (25.14 , 47.5 ) (the [2] tells the C shell to pick the second word from the list in $mail ).

I've set other shell variables for myself. When I send some mail messages, I want to watch the system mail log to see the message being delivered. I just type:

-f
 



% tail -f $maillog


   ...
09/08/96 17:13:27: [m0kJN4x-0000AKC] new msg: from jpeek@jpeek.com
09/08/96 17:13:28: [m0kJN4x-0000AKC] <jim> ... delivered
09/08/96 17:13:42: [m0kJN4x-0000AKC] <allan@comex.com> ... delivered

Are there files or directories that you refer to a lot - ones that aren't right for the cdpath (14.5 ) or a shell alias? Pick a likely shell variable name and add the variable to your .cshrc or .profile . You can store more than one pathname in the same variable - either by separating them with spaces or by using wildcards:






echo
 
# C shell variables:
set worklog=~/todays_worklog   Single file, defined when set

set projfiles=(/work/beta/data_3.9*)   Many files, defined when set

set currdata='/work/beta/data_5*'   Many files, defined when used

# Bourne shell variables:
worklog=$HOME/todays_worklog   Single file, defined when set

projfiles="`echo /work/beta/data_3.9_*`"   Many files, defined when set

currdata='/work/beta/data_5*'   Many files, defined when used

Then:

  • You could type vi + $worklog any time you want to add a note to the end of the file todays_worklog in your home directory. (The + tells vi to start at the end of the file.)

  • The shell expands the asterisk (* ) when it sets the projfiles variable and stores a list of the files as they were when the variable was set . (If the list of files changes, it'll be reset when you start your next shell.) You could print all those files any time you wanted to by typing a command like lpr $projfiles . The C shell also lets you pick individual files (47.5 ) from the list - for instance, lpr $projfiles[9] would print the ninth file from the list.

  • When the currdata variable is set, the single quotes (" ) around it prevent expansion (8.14 ) of the wildcard (* ). Instead, the pathname /work/beta/data_5* is expanded when you use the variable - like pg $currdata  - to show you the files as they are at the time you use the variable.

You can also use variables to store the paths to directories. Use cd , ls , or any other command with the variables.

- JP


Previous: 14.9 cd by Directory Initials UNIX Power Tools Next: 14.11 Finding (Anyone's) Home Directory, Quickly
14.9 cd by Directory Initials Book Index 14.11 Finding (Anyone's) Home Directory, Quickly

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System