Unix Commands: Path Variable
Unix Commands: Path Variable
This chapter provides general information on a number of basic Unix commands. All of these commands
contain numerous options. Some of the more common options will be discussed.
3.1 On-Line Help
The Unix file system allows users to create several levels of subdirectories for logically grouping files.
This section describes the basic utilities for creating these directories and moving around within them.
3.2.1 Creating a Directory (mkdir)
The mkdir command allows you to create directories.
Syntax: mkdir [options] directory
From a login directory, such as /users/jones, create a subdirectory, memos, with the following
command.
mkdir memos
This creates the directory, /users/jones/memos
You can create multiple directories with one command. From the login directory, /users/jones, the
command,
mkdir projects samples
creates the directories, /users/jones/projects and /users/jones/samples. You can also specify a directory
that is not under your current directory by specifying its absolute position. From the directory,
/users/jones/samples, the following command would create the directory, /users/jones/memos/old.
mkdir /users/jones/memos/old
3.2.2 Removing Directories (rmdir)
To remove a directory, use the rmdir command. Note that you must first delete all files within a
directory before deleting the directory itself.
Syntax: rmdir directory
From the directory, /users/jones/memos, the following command would delete the subdirectory, old.
rmdir old
To remove that same directory from the directory, /users/jones, use the command
rmdir memos/old
An alternative is the recursive option with the remove command (rm).
Syntax: rm -ri directory
Note: Use the above command with extreme caution, as it will very quickly delete every file and
subdirectory below the specified directory.
3.2.3 Moving around Directories (cd)
To move around in your directory structure, use the change-directory command, cd. The cd command
specified by itself without any arguments will always return you to your login directory.
Syntax: cd directory
For example, from the main directory, /users/jones, change to the sub-directory, /users/jones/memos,
with the command
cd memos
From the subdirectory, /users/jones/memos, change to the directory, /users/jones/samples with the
command,
cd /users/jones/samples
3.2.4 Directory Shortcuts
You can use shortcuts when specifying your directory pathname. Examples here show how to use these
shortcuts with the cd command, but they can be used with any of the Unix commands when specifying
filenames. Your home directory can be used as a starting point when specifying pathnames by using a
tilde (~). The tilde instructs the shell to start the pathname from your home directory.
For example, from the directory, /users/jones/memos/old, change to the subdirectory,
/users/jones/samples with the command
cd ~/samples
You can also use cd to move to the home directory of another user.
cd ~username
In addition, use the .. notation for moving around directories. The .. refers to the parent directory of
your current directory.
For example, from the directory, /users/jones/memos, change to the subdirectory,
/users/jones/samples, with the command,
cd ../samples
The .. says to move up to the parent directory, in this case /users/jones, and then down to the sub-
directory, samples. You can also use cd .. to back up from subdirectories level by level.
3.2.5 Present Working Directory (pwd)
When moving around the Unix file system, it is helpful to check your present working directory, with the
pwd command.
Syntax: pwd
It is important to recognize that in an X Window environment, a single user can generate a minimum of
five processes just by logging on to a system, and can expand this number exponentially, depending on
the number of windows open and software being executed. Therefore, you need to be conscientious
about the number of CPU and memory resources you are using. This section will describe some of the
Unix commands that you can use to monitor your processes.
3.4.1 The ps Command
The ps command is a general utility for checking process status. It reports information such as current
percentage of the CPU being used by processes, percent of memory, accumulated CPU time, and other
statistics. It is not a real-time display, but takes a snapshot of the system.
Syntax: ps [options]
The ps command specified by itself will display your current processes. To display information about all
processes on the system, enter
ps -ef (on SGI, Sun, and HP Alpha)
If there is more information than will fit on one screen, use the ps option with the more utility, as
follows:
ps -aux | more
To find information about a specific user on the system, use the ps command with the grep search
utility, as follows:
ps -aux | grep username
Below is a sample output from the ps command and a table that explains the information displayed.
UID PID PPID C STIME TTY TIME CMD
root 157 1 0 Mar 18 ? 0:03 /usr/sbin/inetd -s
root 260 1 0 Mar 18 ? 0:02 /usr/sbin/vold
smith 10566 10564 0 07:02:31 pts/4 0:01 /usr/local/bin/tcsh
jones 11741 11739 1 11:12:30 pts/8 0:00 -csh
smith 10644 10510 0 07:09:25 pts/2 0:14 emacs NEW_CUTILS.c
brown 11656 11440 0 10:53:33 pts/7 0:00 idt -soft ppi-cesar.cgm
Column Description
UID User ID of process
PID Process id number
PPID Process ID of parent process
Processor utilization for scheduling
C
(obsolete)
STIME Starting time of process
Controlling terminal of process, ? when
TTY
no controlling terminal
Cumulative execution time for the
TIME
process
CMD Command name
The columns and information displayed varies between architectures. Use the man command to check
for specifics.
3.4.2 The top Command
The top command is a pseudo-real-time command that is useful for examining current processes on the
system and for changing process priorities.
Syntax: top
To exit the top command, type q. To show other options, type h.
3.4.3 The who and w Commands
Other commands to determine who is currently on the system are the who and w commands. The w
command provides additional information about login and idle times for different processes.
Syntax: who
Syntax: w
3.4.4 Killing Processes
There are a number of ways to kill processes on a workstation. To kill a job that is in the foreground,
press CTRL-c.
Use the kill command along with the process ID (PID). (PID information can be obtained from the ps
command).
Syntax: kill -9 pid
To kill a background job, use the jobs command to find its job ID (n) and enter
kill -9 %n
Another useful utility is pkill, available on the Sun, Linux, and SGI architectures. Your current processes
will be listed to the screen one at a time sorted by the age of the process, with your oldest process
(most likely your login shell) appearing first. You will be given the option to kill each process. Responding
yes to the inquiry will kill the process; a carriage return will retain the process.
Syntax: pkill
3.4.5 Checking the Load on Other Workstations
If a workstation appears to be heavily loaded, you can check the load on other workstations with the
ruptim command.
Syntax: ruptim [workstation]
Following is a sample output from this utility.
oak number of users: 1 load average: 0.26, 0.16, 0.00
fir number of users: 15 load average: 0.26, 0.06, 0.00
pine number of users: 25 load average: 0.21, 0.03, 0.01
juniper number of users: 15 load average: 0.39, 0.12, 0.01
cypress number of users: 9 load average: 0.22, 0.04, 0.00
balsam number of users: 15 load average: 0.18, 0.15, 0.00
walnut number of users: 3 load average: 2.00, 1.94, 1.56
beech number of users: 4 load average: 1.17, 1.15, 1.00
The last three columns are 1-, 5-, and 15-minute averages. Values indicate whether the CPU is
underutilized (< 1.0), 100% utilized (1.0), or processes are waiting (> 1.0). If you receive the error
message, permission denied when executing this command, you need to modify your .rhosts file. The
.rhosts file should include the systems being checked as well as the system you are logged on to.
3.4.6 Changing Process Priority
All Unix processes have a dynamically calculated priority. This priority changes over time as a process
accumulates CPU time. To execute a program that will use several minutes of CPU time over a short
wall-clock period, change the process priority so that it does not monopolize the machine. To do this,
use the nice command when executing your process:
Syntax: nice -number argument
The higher the number specified, the lower the priority of the process. The default is 10, and you can
specify numbers as high as 20. Note that you can only increase this number. For example, a process
started with a value of 18 cannot be lowered to 10 with nice -10, except by root.
For instance, to execute a program called a.out at a low priority, enter
nice -n 19 a.out
If you have already started a process without the nice command, you can use the renice command to
change the priority of the process.
Syntax: renice -number process_id
Processes can also be reniced from the top command (see Section 3.4.2) by typing r followed by the
priority number and process ID (PID).
3.5 Disk Usage
The shell keeps a history list of recently executed commands if you instruct it to do so. The following line
should be placed in your .login and .cshrc files.
set history=nn
where nn is the number of lines you wish to retain; 40 is a good number. There are a number of ways to
re-execute commands maintained in the history file. !! entered on the command line executes the most
recently executed command.
If you enter the history command, a list of your most recently executed commands will be displayed
with a line number next to each command. To re-execute a specific command, enter
!line_number
Another mechanism for recalling a previously executed command is to enter the first few letters of the
previous command. For example, if you had previously entered a command to recursively look through
your directory structure for all directory files, using
ls -lR | grep drw
You could recall that command with the following.
!ls
The shell would then reexecute the most recent command line that began with ls.
The Unix operating system provides a variety of utilities that allow you to perform functions such as
combining multiple commands and redirecting your output.
3.7.1 Pipes
Pipes allow you to connect various combinations of Unix commands together. The pipe symbol is the
vertical line, |, on your keyboard.
For example, to search a directory listing for a particular string, and output the information to the screen
a page at a time, enter
ls -l | grep 'string' | more
To search the current system processes for a particular user, enter
ps -ef | grep username
3.7.2 I/O Redirection
In general, Unix commands send output to "standard out" (stdout), your terminal screen. At times, you
may want this information written to a file instead. Unix allows you to either redirect output to a file or
to have a Unix command read input from a file. The symbol > outputs information to a file, and < reads
input from a file.
For example, to perform a search on a directory listing and write that output to a file called "source.txt,"
enter
ls -l |grep '*.f' > source.txt
To have the Mail utility read in information from a file called "input.txt," enter
mail user@host < input.txt
3.7.3 The sort Command
The sort utility sorts the lines in a file. By default, it sorts the file in ASCII order, with numbers preceding
alphabetic characters.
Syntax: sort filename
You can also use the sort command in combination with other commands using pipes. To sort the
output from a who command, enter
who | sort
3.7.4 File Differences (diff)
It is sometimes useful to check the differences between two files. The diff command performs this
function.
Syntax: diff [options] file1 file2
3.7.5 Counting Lines, Words, and Characters (wc)
It is often useful to know the number of lines, words, and characters in a file. The wc utility accomplishes
this task. By default, it prints out all three of these fields.
Syntax: wc [options] filename
This command is useful when combined with other commands. For example, to find the number of files
in a directory, enter
ls -l | wc -l