You are on page 1of 8

UNIX COMMANDS

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

3.1.1 The man Command


To obtain information on any Unix command, use the on-line manual pages.
Syntax: man command
If you do not know the name of the actual command you need, execute man with the -k option.
man -k keyword
For example, to find out all the commands related to directories, type
man -k directory
This command outputs information to your screen about the commands that pertain to directories,
along with a brief description of each command's functions. To access man pages for local utilities, add
/usr/local/man to your path variable.
3.1.2 Printing a Hardcopy of a man page
To print a hardcopy of a manual page, redirect the output to a file, and then send this file to a printer
using one of the printing commands, as in the following example:
To generate a file for hardcopy output of the ls command, type
man ls > ls.txt (SGI, Sun, HP Alpha)
You can then send the formatted file to a printer using qpr ls.txt.
3.2 Directory Commands

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

3.3 File Commands

3.3.1 Listing Directory Files (ls)


To list the files in any directory, use the ls command.
Syntax: ls [options] [filename_spec]
The ls command by itself will list the files in your present working directory. By default, the ls command
lists only the names of the files in the directory. The following examples show some of the more
commonly used options.
To list all the Unix hidden files, such as .login or .cshrc in
your home directory, enter
ls -a
For a long listing that shows file protections, size, and date,
enter
ls -l
To recursively list all the files in the current directory and
any subdirectories of the current directory, enter
ls -R
It is sometimes useful to use the ls command in conjunction
with other Unix commands. For instance, if there are too many files
in a directory to fit on one screen, the output can display to the
screen a page at a time.
ls -l | more
To search for a subset of files, use the ls command with
the Unix search command, grep. To find all files written
on a particular date, enter
ls -l |grep 'Jun 25'
3.3.2 File Protections (chmod)
Following are the protections that can be set on a file with their symbolic or numeric representations.
Symbolic Numeric Protection
r 4 Read access
w 2 Write access
x 1 Execute access
With the chmod command, you can modify protections on a file for three classes of users: the user (u),
group (g), or others (o) by entering
Syntax: chmod ugo filename
The protection for each class of user can be obtained by adding the numerical equivalent for each type
of access. For instance, to give the owner read, write, and execute access, it would be 4+2+1 or 7. To
give group read and write access to the file would be 4+2 or 6, and to give world read access would be 4.
The resulting command would be
chmod 764 filename
You can use wild card characters in the filename specification, or specify multiple files separating them
by spaces.
The following sample listing of a file shows owner with read/write access and group and world with read
only. The command, chmod 644 would produce these protections.
-rw-r--r-- 1 waukau 5417 Jun 6 05:28 test
To include execute access to this file for all user classes, enter chmod 755, which produces these
protections.
-rwxr-xr-x 1 waukau 5417 Jun 6 05:28 test
You can also use the symbolic representation for adding or removing access to files. For example, to give
execute access to all categories (user, group, and others), enter
chmod +x filename
To remove write access to a file for group and others, enter
chmod g-w, o-w filename
3.3.3 Default File Protections (umask)
You can set up a default file protection for all new files created by using the umask command in your
.login file.
Syntax: umask ugo
The access modes specified in the umask command are the complement of the chmod command. You
specify those access modes you do not want a particular class to have, as in the following examples.
To remove write access for group and others, specify
umask 022
To remove all access for group and others, specify
umask 077
3.3.4 Copying Files (cp)
Use the cp command to make a copy of a file.
Syntax: cp [options] old_filename new_filename
3.3.5 Renaming Files (mv)
To rename a file, use the mv command.
Syntax: mv [options] old_filename new_filename
3.3.6 Deleting Files (rm)
To delete a file or files, use the rm command.
Syntax: rm [options] filename
You can use wild cards to remove multiple files. When deleting with wild cards, it is best to have the rm
command ask you whether you want to delete each individal file. To do this, enter
rm -i *.f
You are prompted for a response for each file that matches this criterion (in this case, all files that have
the suffix, .f). If you answer y, the file is deleted. If you answer n (or any other response), the file is not
deleted.
3.3.7 File Searches (grep)
At times, it is necessary to search through your files for a particular string of characters. The command
to perform searches is grep.
Syntax: grep string file1 [file2 . . .]
By default, the grep command matches the letter case of the string specified, as in the following
examples.
To search all files in the directory for the string, "May," enter
grep May *
To find any occurrence of the string, "May," regardless of letter
case, use the -i option.
grep -i May *
This would find strings such as "may," "maybe," or "MAY," along
with any other occurrence of that string.
It is useful to pipe (|) the output of the grep command into the more command to see the listing one
screen at a time, as shown below. For further information on pipes, see Section 3.7.1.
grep -i May * | more
Note: Certain special characters must be "escaped" with a "\" when searching for them in regular
expressions.
3.3.8 Viewing Files (more, cat, head and tail)
There are a number of commands that can be used for viewing files in the Unix operating system. This
section will describe a few of these.
The more Command
The more command outputs a file to the screen a page at a time. To view the next page, press the space
bar. To see just one new line at a time, press RETURN. The more utility also allows you to perform some
basic editing functions such as string searches, moving forward and backward in the file, and entering
the vi editor. CTRL-c aborts a more session.
Syntax: more [options] filename
Following are some helpful options to use with the more utility.
q Exit immediately from more.
ib Move backward by screens, where i is the number
of screens
if Move forward by screens, where i is the number
of screens
/string Search forward for string. Backward searches are not
possible.
The cat Command
The cat command will also display file contents to a screen.
Syntax: cat filename
Also, use cat for quickly creating a short file, by entering the following.
cat > filename
After pressing the return key, type in the text. To save and exit the file, press CTRL-D.
An additional feature of cat is that it allows you to concatenate two files together by entering the
following
cat file1 >> file2
This command appends file1 to the end of file2.
The head Command
To view the first ten lines in a file, use the head command.
Syntax: head [-count] filename
If the -count option is specified, you can display more than ten lines. The following command would
display the first 20 lines.
head -20 filename
The tail Command
To view the last ten lines in a file, use the tail command.
Syntax: tail [-count] filename
If the -count option is specified, you can display more than ten lines. The following command would
display the last 20 lines.
tail -20 filename
It is helpful to "pipe" the head and tail command outputs into the more command when -count exceeds
20. See Section 3.7.1.
3.3.9 Pattern Matching
In the previous sections, examples were given for matching exact strings. There are also certain
metacharacters available in Unix that allow more flexibility in performing string searches.
Matching Any Single Character in a String Expression
You can use a period (.) to match any single character in an expression, and concatenate periods to
represent an exact number of characters, as in the following example.
To search a file for dollar amounts in the ten-thousand-dollar range, enter
grep 10,... proposal
This would find matches such as 10,234 and 10,456. However, 10234, typed without a comma, would
not match.
Matching Any Single Character in a File Name Specification
When specifying file names, use a question mark (?) to match any single character, and concatenate
question marks to represent an exact number of characters. For example, entering
ls -l plot?.dat
would find files such as plot1.dat, plot2.dat or plots.dat. It would not find the file plot22.dat.
Matching Any Number of Occurrences of a Character
In an expression or filename specification, an asterisk (*) stands for any string of characters, even a null
one. For example, to find all derivatives of the word meteorology in a file, enter
grep 'meteor*' filename
This would show occurrences of the words, "meteorological", "meteorologist", "meteors", etc.
As another example, to find all files with the file extension .f, enter
ls -l *.f

3.4 Process Monitoring

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

3.5.1 The df Command


The df command lists the current total disk usage for all file systems accessible by your workstation.
Syntax: df -k
3.5.2 The du Command
To determine your current disk quota usage, use the du command.
Syntax: du [options] directory
By default, the du command by itself will give a summary of quota usage for the directory specified and
all subdirectories below it.
du /users/username
For a summary of total disk usage, use the -s option.
du -s /users/username
For a summary of your own disk usage, enter
du -s ~
3.5.3 The quota Command
On the Sun, HP Alpha, and Linux workstations, the quota command displays your current usage. This
command does not display relevant information on the SGI systems.
Syntax: quota -v username

3.6 Command History

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.

3.7 Unix Tools

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

You might also like