Skip to main content

Office of Information Technology (OIT)

UT Arlington
OIT: Office of Information Technology

helpdesk@uta.edu ·  Work Order · 817-272-2208 · System Status

We are your IT partner!

Using UNIX: Advanced Topics Part 2

The "Using UNIX Guide" is available for download to read or print in PDF format. If you choose to print this document in an ACS lab, print in ranges of 20 pages (print pages 1-20, 21-40, 41-60, etc.) To view this document on the web, follow a link below.


Table of Contents

  1. Conventions
  2. Using the vi Editor
    1. Operating Modes in the vi Editor
    2. Aborting Commands
    3. Saving Files
    4. Cursor command
    5. Delete Commands
    6. Insert Commands
    7. Search Commands
    8. Change Commands
    9. Marking Text
    10. Moving Marked Text
    11. Yank Command
    12. Copying Yanked Text
    13. Reading Other Files Into Current File
    14. Repeating Commands
    15. Joining Lines
  3. Understanding Foreground and Background Processing
    1. Running a Background Job
    2. The nice Command
    3. The jobs Command
    4. The fg Command
    5. The stop Command
    6. The kill Command
  4. Miscellaneous Commands
    1. The history Command
    2. The alias Command
    3. The cal Command
    4. The date Command
    5. The who Command
    6. The quota Command
    7. The limit Command
  5. Shell and Environment Variables
    1. The set Command
    2. The echo Command
    3. The printenv Command
    4. The setenv Command
    5. The .login and .cshrc Files
    6. Using X Windows Systems
  6. Using X Windows With a Workstation
    1. Starting X Windows on an NCD X Windows System
    2. Accessing UNIX Documentation Using X Windows

Using the vi Editor

vi is a full-view line editor. You use vi to create and modify your programs and documents when you work on a UNIX-based system. There are several ways to start the vi editor, as demonstrated below.


vi-- will invoke the editor, without opening a file


vi filename-- will invoke the editor, and open the file or create the file if it does not exist


vi /directory/filename-- will invoke the editor, and open/create the file

Operating Modes in the vi Editor

Text Input Mode

vi has two text input modes,append and insert. Typing
a,from command mode, will put you in append mode (text will be added behind the cursor). Typing
i, from command mode, will put you in insert mode (text will be added before the cursor).

Command Mode

All vi commands must be entered from command mode. When the vi editor is started, you are automatically placed in command mode. To get to command mode from text input mode, press
<esc>. If you hear a beep when you press
<esc>,you are already in command mode. Note, vi commands are case sensitive - type uppercase and lowercase characters as such.

When you press
<esc> nothing appears on the screen.

Aborting Commands

To abort a command you can press
<esc>. If you abort a command, you will still be in command mode.

In addition to pressing
<esc>, you can type
u (from command mode) to reverse the effect of the last command that changed the buffer. If you type
U (from command mode), the effect of all the commands on the current line will be reversed, provided you have not moved from the line since the last change.

Also, you can correct typing errors from either of the text input modes by pressing the backspace key and typing over the incorrect text.

Saving Files

There are several ways to save files in vi, some of them are illustrated below.

:
wq or
ZZ or
:x

writes buffer to file, and exits vi


:w

writes buffer to file, and stays in vi


:q

exits vi without saving changes


:w
filename

writes buffer to filename (will not write to an existing file)


:w!
filename

writes buffer to filename (overwrites existing file)


:q!

exits vi, and aborts all changes

Cursor command

vi has many ways of moving the cursor around in a document. Normally, the arrow keys on your keyboard will work, as long as you are in command mode. However, if you find that the arrow keys are not functioning properly you can choose any of the following commands.


h

moves one character left, within current line


l

moves one character right, within current line


j

moves down one line


k

moves up one line


b

moves left to beginning of next word, or punctuation mark


B

moves left to beginning of next word, ignoring punctuation


w

moves right to beginning of next word, or punctuation mark


W

moves right to beginning of next word, ignoring punctuation


<ctrl-f>

scrolls forward one full screen


<ctrl-b>

scrolls backward one full screen


<ctrl-d>

scrolls forward one-half page


<ctrl-u>

scrolls backward one-half page


H

moves cursor to top left of screen


M

moves cursor to middle left of screen


L

moves cursor to bottom left of screen


G

moves cursor to the beginning of the last line of file/document

In addition to the commands listed above, you can add numbers to the front of the command to make it perform its function multiple times. For example, typing 6b will move the cursor back six words.

Delete Commands

Below are just a few of the commands you can use to delete characters, words, or lines.


x

deletes character at the cursor position


dd

deletes current line


dw

deletes one word right

You can prefix the deletion commands with numbers to process multiple functions simultaneously. For example, typing
2dd will delete the next two lines.

Insert Commands

With vi you can insert text and/or blank lines. To enter text, issue one of the following commands. Remember to press
<esc>, to return to command mode.


i

puts you in insert mode, insertion occurs before the current cursor position


I

moves cursor to beginning of current line, and enters insert mode


o

creates a blank line following the current line, moves the cursor to thebeginning of the new line, and enters insert mode


O

creates a blank line before the current line, moves the cursor to the beginning of the new line, and enters insert mode


a

puts you in append mode, text will be added after current cursor position


A

moves cursor to end of current line, and enters append mode

Search Commands

You can use the search command to help you locate a text string. If you want a match for just the characters entered as the string, be sure to include a space preceding and following the string.


/string

searches forward from cursor position for string


?string

searches backward from the cursor position for string


n

repeats search in same direction


N

repeats search in opposite direction

If you want to find all occurrences of the word ear. Enter the command "
/ear ". If you type "
/ear",you will find all occurrences of the word ear, and also fear, earth, bear, etc.

Change Commands

vi has several commands that allow you to change characters, words, and lines. Some of the commands and descriptions are below.


r

allows you to replace one character at current cursor position


cw

allows you to replace one word at current cursor position


ct
x

allows you to replace text from current cursor position to a specified character
x


xp

transposes the letter at the cursor position with the following letter


ddp

swaps position of the current line and the following line


R

allows you to replace all characters beginning at the cursor position; at end of line,editor enters insert mode

You can prefix r and cw with numbers to make multiple changes.

Paste Command

When a string is deleted itis stored in a temporary buffer. Deleted text stays in this buffer until you modify the buffer by deleting another string or exit the editor. The text strings stored in this buffer can be removed and placed back in the text. This can be done by using the commands below.


p

places last deleted text following the cursor


P

places last deleted text before the cursor

Marking Text

To make a bookmark in a document place the cursor at the location where you want the bookmark; type
m
x
(where
x is any letter you choose to symbolize that particular bookmark). To return to the marked position type
`
x
(grave accent {backward single quote} followed by the character
x you specified). Typing
'
x
(apostrophe {forward single quote} followed by the character
x you specified), will return you to the beginning of the line that contains the mark. You can have more than one bookmark in a file. Marks are forgotten when you exit vi or close that document.

Moving Marked Text

You can delete the text between your mark and the current cursor position by typing
d`
x
(backward single quote). Then, you can move the cursor to where you want to add the text, and type
p or
P.

Yank Command

The
yank command copies text into a temporary buffer. It does not change the original text when it makes the copy.Text remains in the buffer until you enter another command that places text into the buffer, or exit the editor.

Your original text is not deleted, just copied.


yy or Y

copies the line at the cursor into the buffer


nyy or nY

copies
n lines from the cursor position into the buffer


yw

copies the word at the cursor into the buffer


nyw

copies
n words following the cursor position into the buffer

Copying Yanked Text

You can use the
yank command with the paste command to copy text to another location in the current document.

Reading Other Files Into Current File

You can insert another file into the current file by typing
:r
filename
. The inserted file will be added starting on the line following the cursor position.

Repeating Commands

If you type a period
(.), the last command that modified the buffer will be repeated. You can use this instead of having to retype commands.

Joining Lines

Typing
J allows you to join the current line and the following line. Typing
n

J will join
n lines to the current line.


Understanding Foreground and Background Processing

Running a Background Job

UNIX systems are multitasking systems. This means that you can run more than one process at the same time. A process can be anything from listing the contents of a directory to running a program.

A foreground process is any task that you run on your terminal you'll return to the command prompt only when you complete your entire task. You may have only one foreground process running at any time. You can suspend a foreground process by pressing
<ctrl-z>. After suspending a foreground process, you return immediately to the command prompt.

To run multiple processes on a UNIX system, you must run them in the background. A process running in the background is called a background job. Background jobs should not require your intervention. You can run several background jobs simultaneously you are notified when a background job is completed.

Running a Background Job

The nice Command
The jobs Command
The fg Command
The stop Command
The kill Command

To run a process as a background job, follow the command for that process with an ampersand
(&). The job is then assigned a job number and a unique process identification number (PID). After starting a job in the background, you'll return to the system prompt where you can execute other commands or start other jobs.

A trade off exists between running several jobs in the background simultaneously and the system response.Running several jobs in the background can significantly degrade the interactive response you receive on a UNIX system.

The nice Command

When running a process as a background job, you should precede the command for that process with the
nice command. The format of the
nice command used to run a process as a background job is:


nice [-n] command [options]&

where
n is an optional number between 1 and 20, command is the command for that process, and options are any optional arguments for that command. The optional number included after the nice command determines the priority level of the background job the lower the number, the greater the priority. If you do not include a number with the
nice command, 10 is used as the default.

If you want to be able to logout of your account and have a job continue to run in background, precede the previous command by the word
nohup. The previous command would become:


nohup nice [-n] command [options]&

In the following example, the
nice command is used with the FORTRAN compiler command, f77, at a nice level of 20; the object of the compiler command is the FORTRAN program called random.f (for information on compilers, please see "Running Programs on UNIX"; for information on the jobs command, see "The jobs Command"):

%
nice -20 f77 random.f & <return>
[1] 20394
%
jobs <return>
[1] + Running f77 random.f
%

The jobs Command

The
jobs command displays the status of any background jobs. The format of the
jobs command is simply:


jobs

In the following example, the
jobs command displays the single background job running:

%
jobs <return>
[1] + Running f77 random.f
%

If you have used the
nohup command to run a job in background and then logged out of your account, issuing the
jobs command after you log back in will not show the status of that job. To see its status, enter the following command:


ps auxww | grep
userid

If the job is still running, its status will be shown. If the job has completed, it will not appear.

The fg Command

The
fg (foreground) command returns a background job to the foreground. The format of the
fg command is:


fg %
n

where
n is the job number. If the
fg command is used without any objects, the most current job in the job list is brought into the foreground.

The stop Command

The
stop command stops a job currently running in the background. The format of the
stop command is:


stop % n where n is the job number.

The kill Command

The kill command cancels a job running in the background. The format of the kill command is: kill %
n

or
kill -9 %
n

where
n is the job number. Specifyingthe -9 option with the
kill command kills the process under all circumstances.
The following examples illustrate the concept of running jobs in the foreground and background:
%
more file1 <return>
This is line number 1
This is line number 2
.
.
.
--More--(57%)
<ctrl-z>
Suspended
%
ls -l <return>
total 8
-rw------- 1 jsmith 1865 Nov 19 12:00file1
-rw-rw---- 1 jsmith 256 Nov 01 09:05file2
-rw----rw- 1 jsmith 865 Dec 25 05:00file3
drw-rw---- 2 jsmith 512 May12 11:23letters


<ctrl-z>
Suspended
%
cat file1 file2 > file3 & <return>
[3] 5421
%
jobs <return>
[1] Suspended more file1
[2] - Suspended ls -l
[3] + Running cat file1 file2 > file3
%
diff file1 file2 > file.dif &<return>
[4] 5427
%

The first number represents the job number and the second number is the unique process identification number(PID) assigned to the job.

%
jobs <return>
[1] Suspended more file1
[2] Suspended ls -l
[3] - Running cat file1 file2 > file3
[4] + Running diff file1 file2 > file.dif
%
[3] Done cat file1 file2 > file3
%

As shown in the above example, you are informed when any background job finishes.

%
jobs <return>
[1] Suspended more file1
[2] - Suspended ls -l
[4] + Running diff file1 file2 > file.dif
%
fg %1 <return>
more file1
This is line number 1
This is line number 2
.
.
.q
%
kill %2 <return>
[2] Terminated ls -l
%
kill %4 <return>
[4] Terminated diff file1 file2 > file.dif
%
jobs <return>
%

If you try logging out and you have any stopped jobs, you are notified by the system of those jobs. If you do not need the stopped jobs, you should kill them. In either case, you can use the logout command or
<ctrl-d> a second time to log out.


Miscellaneous Commands


The history Command

The alias Command
The cal Command
The date Command
The who Command
The quota Command
The limit Command

The history Command

The history command displays the last 20 commands used (by default), but this number can be increased or decreased.

You can reissue a previous command with one of the following commands:


!!-- Execute the last command entered.
!
n

-- Execute command number
n on the history list.
!
xxx
-- Execute the most recent command on the history list starting with xxx.
!-n-- Execute the nth command ago.

The following example shows the history command being used:

% history <return>
1 more file1
2 pwd
3 cd /
4 cd
5 history
% !2 <return>
pwd
/home/dept/jsmith
% history <return>
1 more file1
2 pwd
3 cd /
4 cd
5 history
6 pwd
7 history
% !-7 <return>
more file1
This is line number 1
This is line number 2
.
.
.q
% history <return>
1 more file1
2 pwd
3 cd /
4 cd
5 history
6 pwd
7 history
8 more file1
9 history
% !c <return>
cd
% pwd <return>
/home/dept/jsmith
%

The alias Command

The alias command is used formaking a nickname or abbreviation for a UNIX command. You can consequently designate your own name for any UNIX command. The format of the alias command is:

alias nickname UNIX_command

To display a list of all current aliases, use the alias command with no object. In the following example, the alias command is used first to nickname the more command as type and then the ls command, including the -l(long) option, as long:

% alias type more <return>
% alias long 'ls -l' <return>
% type file1 <return>
This is line number 1
This is line number 2
.
.
.q
% long <return>
total 8
-rw------- 1 jsmith 1865 Nov 19 12:00file1
-rw-rw---- 1 jsmith 256 Nov 01 09:05file2
-rw----rw- 1 jsmith 865 Dec 25 05:00file3
drw-rw---- 2 jsmith 512 May12 11:23letters
% alias <return>
type more
long ls -l
%

The cal Command

The cal (calendar) command displays the calendar for the specified month and year. The format of the cal command is:

cal month year

If you exclude the month number from the cal command, the calendar for the entire specified year is displayed.

% cal 4 1992 <return>
     April 1992
S  M  Tu  W Th  F  S
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

%

You must specify the entire year with the cal command. For example, cal 90 displays the calendar for 90 AD, not 1990. To display the calendar for 1990, you must use cal 1990. The cal command with no object displays the calendar for the current month.

The date Command

The date command displays the current date and time on your screen:

% date <return>
Thu Aug 28 08:56:28 CDT 1997
%

The who Command

The who command displays all users currently logged in to a UNIX system. The who command displays the user's login name, the device to which the user's terminal is connected, and the login time and date.

In the following example, the who command shows three users currently logged in:

% who <return>
opr5    console Aug 20 07:54  
jsmith  ttyp0   Aug 20 09:38 (129.107.1.100)
bcarson ttyp3   Aug 20 10:43 (129.107.1.100)

The quota Command

The quota command displays the disk quota for your account. To check your disk quota, type the following:

% quota <return>
Disk quotas for user sbh3284 (uid 4210):
File system blocks  quota limit grace files quota limit grace 

/home 1084 4096 6144 84 0 0

The limit Command

The limit command shows the maximum computing resources that can be allocated to a process. To see this listing, type the following:

% limit <return>
cpu time 3:00:00
file size unlimited
data size 81920 kbytes
stack size 2048 kbytes
core dump size unlimited
memory use 1022184 kbytes
vmemory use 1048576 kbytes
descriptors 96


Shell and Environment Variables

The set Command
The echo Command
The printenv Command
The setenv Command
The .login and .cshrc Files

UNIX systems use two types of variables to define your working environment: shell variables and environment variables.

Shell variables are known only to the shell in which they were created. If you start a new shell (command interpreter), the shell variables created in the previous shell are not transferred to the current shell.

Environment variables are global variables and are independent of the shell. Environment variables stay in effect regardless of what shell is used.

On a UNIX system, some shell and environment variables are predefined by the system. Also, environment variables are usually represented by uppercase names whereas shell variables are represented by lowercase names. A shell variable with the same name as an environment variable still has the same function.

Some of the definable shell variables are:

Variable

Description

argv

Argument to the shell.

autologout

Number of minutes of idle time before a user is logged out.

cd path

Alternative directory tree search.

history=n

Remember last n commands.

home

Home directory of user.

mail

Directory where shell checks for user's mail.

noclobber

Prevents overwriting an existing file during redirection.

user

Login User ID.

savehist=n

Remember last n commands at the beginning of next terminal session.

prompt=string

Change the default command prompt to string.

path

Current path specification- list of directories the system may search to resolve command requests.

shell

Shell used to process commands.

term

Terminal type.

status

Returned by last command: 0=no error, 1=error.

Some of the definable environment variables are:

Variable

Description

HOME

Home directory.

SHELL

User's login shell.

TERM

Terminal type.

USER

Login User ID

PATH

Search path used to resolve command requests.

DISPLAY

Name of X Windows capable display device.

The set Command

The set command displays current shell variables and their values. You can also designate a new value for a variable with the set command. In the following example, current shell variables are shown using the set command:

% set <return>
argv ()
autologout 0
cd path /home/dept/jsmith
cwd /home/dept/jsmith
history 20
home /home/dept/jsmith
mail /usr/spool/mail/jsmith
path (. /home/dept/jsmith/bin /home/dept/bin )
prompt %
shell /bin/csh
status 0
term vt52
user jsmith
%

In the following example, the shell variables term, prompt, and autologout are assigned values using the set command:

%set term=vt100 <return>
% set prompt="UNIX> " <return>
UNIX> set autologout=20 <return>
UNIX> set <return>
argv ()
autologout 20
cd path /home/dept/jsmith
cwd /home/dept/jsmith
history 20
home /home/dept/jsmith
mail /usr/spool/mail/jsmith
path (. /home/dept/jsmith/bin /home/dept/bin )
prompt UNIX>
shell /bin/csh
status 0
term vt100
user jsmith
UNIX>

The echo Command

To display the value of a shell variable, type echo followed by a dollar sign ($) and the name of the variable. You must place a dollar sign before the variable name to display the value of the variable otherwise the name of the variable is displayed. In the following example, the echo command is used to display both text and the current values for the cwd and user variables:

% echo "Hello there" <return>
Hello there
% echo "cwd" <return>
cwd
% echo "$cwd" <return>
/home/dept/jsmith
% echo "$user" <return>
jsmith
%

In the following example, the echo command displays the values of the environment variables USER and TERM:

% echo "USER" <return>
USER
% echo "$USER" <return>
jsmith
% echo "$TERM" <return>
vt100
%

The printenv Command

The printenv command displays all environment variables and their values. In the following example, the printenv command displays the environment variables:

% printenv <return>
TERM=vt100
HOME=/home/dept/jsmith
SHELL=/bin/csh
USER=jsmith
PATH=.:/home/dept/jsmith/bin:/home/dept/bin
%

The setenv Command

The setenv command sets the value of environment variables. In the following example, the environment variable TERM is set to vt102 with the setenv command:

% setenv TERM vt102 <return>

The .login and .cshrc Files

When you log in to a UNIX system using the C shell (the default shell), the system searches your home directory for two files, .cshrc and .login, and executes all of the shell commands in those files. The commands in the .cshrc file are executed first, then commands in the .login file are executed. The two files differ in that the commands in .cshrc are executed every time you start a new C shell, whereas the commands in .login are executed only when you log in. If you start a new Cshell, the commands in .login are not re-executed. For users who invoke the Bourne shell when they login, the system searches for a .profile file and executes all the shell commands in that file.

Usually, all of the environment variables are defined in .login and all of the shell variables are defined in .cshrc. In the following example, the cat command displays the contents of files .login and .cshrc:

% cat .login <return>
setenv SHELL /bin/csh
set mail=/usr/spool/mail/$user
% cat .cshrc <return>
set path=(. $HOME/bin /home/dept/bin /bin)
set history=20
alias dir 'ls -l'
alias rm 'rm -i'
alias cp 'cp -i'
alias mv 'mv -i'
set noclobber
set autologout=20
%

You can start a new C shell under the parent shell by issuing the csh command. All of the commands defined in the .cshrc file are automatically executed in this newly created C shell. Pressing <ctrl-d> kills the most recently created C shell.


Using X Windows Systems

Using X Windows With a Workstation
Starting X Windows on an NCD X Windows System
Accessing UNIX Documentation Using X Windows

To use X Windows you must have a terminal or workstation that supports X Windows protocols. If you are uncertain whether your terminal or workstation can support the X Windows protocols, please consult your documentation or call Academic Computing Services at 272-2208.

The Teleray VT100 compatible terminals in the computing facilities do not support X Windows protocols. Theterminals in the computing facilities that support X Windows protocols are the NCD HMXpro terminals.

If your terminal or workstation does support X Windows protocols, then you can use X Windows with any host system that supports X Windows applications.

Using X Windows With a Workstation

To use X Windows with a workstation, follow these steps:

1. Log in to the local workstation.

2. Start X Windows on the local workstation.

3. From the local workstation, enter the command xhost +node (where node is the name of the remote node where you will be running X Windows applications).

4. Connect and login to the remote node.

5. If the remote system is administered by Academic Computing Services, then enter the command setx on the remote system to set the DISPLAY environment variable.
or
If the remote system is not administered by Academic Computing Services, then enter setenv DISPLAY <IP address>:0,where <IP address> is the IP address of your local system (you must type setenv in lowercase letters and DISPLAY in uppercase letters).

You may then start any XWindows applications on the remote system and they will display on the local workstation.

Starting X Windows on an NCD X Windows System

To start an X Windows session from an NCD X Windows System, follow these steps:

1. Select host from the chooser window.

2. Connect and login to the remote node.

or (for a Telnet session):

1. Press the setup key and select terminals, new terminal.

2. Connect and login to the remote node.

3. If the system is not administered by Academic Computing Services at UT Arlington, then enter the following:

% setenv DISPLAY <terminal>:0<return>

where <terminal> is the number of the terminal which can be found at the top of the monitor. To enter this amount, you must be in the csh or tcsh shell and setenv must be in lowercase letters and DISPLAY must be in upper case letters. For example:

% setenv DISPLAY xncd02:0 <return>

You may then start any XWindows applications on the remote system and they will display on your XTerminal.

If you subsequently use Telnet to connect with another system not administered by Academic Computing Services at UT Arlington, you must perform step 3 listed above once again.

Accessing UNIX Documentation Using X Windows

To access UNIX documentation while using X Windows, type dxbook on the Omega system or answerbook on the Gamma system. These applications display online documentation on your workstation screen. When you start the application, you will see two types of windows.

The selection window displays lists of available online documentation. When you do not have a book open, the selection window contains lists of related documentation grouped into bookshelves. When you do have a book open, the selection window contains the table of contents or index of that book.

The topic window lists thecontents of a book after that book is opened.

To open a book, follow these steps:

Open a book by double-clicking on that book's title once opened, you'll see the Table of Contents for that book in the selection window and that book's first page in the topic window. For more information on using the dxbook or answerbook commands, including a complete list of options, use the man command.

Available To:

  • Event-Based
  • Students Admitted to UT Arlington
  • Departments and Offices at UT Arlington
  • Retirees
  • Vendors
  • Visitors and Guests
  • Faculty (currently appointed) and Staff

System requirements: Linux, None, Solaris