Полезная информация

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
% 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:


% 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:

# 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


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 ToolsNext: 14.11 Finding (Anyone's) Home Directory, Quickly
14.9 cd by Directory Initials Book Index14.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