Introduction to Unix
Course Handout: (last update 07 July 2022)
These notes may be found at http://caligari.dartmouth.edu/~rc/classes/unix1. The online version has many links to additional information and may be more up to date than the printed notes
Introduction to UNIX
The goals of this course are to enable you to:
- Login to your UNIX account from any type of computer.
- Find online help.
- Use the basic UNIX commands to create files and directories.
- Edit and print files.
- Understand the UNIX tools philosophy.
Example commands are shown like this
.
Most commands are shown with links to their full man pages (ls
)
Output from commands is shown like this
Optional items are shown in brackets, [ like this ]
Some descriptions in these notes have more detail available,
and are denoted like this:
More details of this item would appear here. The printed notes
include all of the additional information
UNIX is simple and coherent, but it takes a genius (or at any rate a
programmer) to understand and appreciate the simplicity.
- Dennis Ritchie
If you have any trouble sounding condescending, find a Unix user to show you how it's done.
Dilbert newsletter 3.0, 1994
- Scott Adams
Table of Contents
(1)
Unix use at Dartmouth
- Interactive Use Systems
- Research Computing systems Andes, Polaris, Discovery
- Private and departmental desktops and clusters (inc. Linux). Many Unix systems in Engineering, Computer Science and Physics.
- MAC/OS X is UNIX under the hood (Mach/BSD Kernel with Apple GUI)
- Back-end Servers
- BlitzMail
- Research Computing File System RSTOR (access your files from anywhere in the world)
- Library Catalogs and Databases
- Webster - General purpose web-server.
This is the machine that serves Dartmouth web pages. Individuals and campus organizations can
get accounts.
- Cobweb is the test and
development web server - anyone can get an account there to develop web content and CGI scripts.
- DND (name directory) and DNS (hostname database)
- Administrative Computing, Oracle Databases
- Special purpose application servers: Remedy, Blackboard, CorpTime, License servers.
(2)
Research Computing Group, Computing Services
The Research Computing group provides:
Hardware
- Central file servers (AFS) - used by all of the systems below, and available for use by
any computer on campus.
- Sunray, Polaris
- General purpose SMP machines for mail, IRC, software development etc. All Northstar accounts are
enabled on these systems.
- Research systems
(Ozark, Andes). Dedicated multi-CPU systems for research users. A subset of the Northstar
accounts are enabled on these systems. They use the same home directories (in AFS) as the general purpose
systems.
- Discovery Cluster
A cluster of Linux nodes for parallel applications (beowulf cluster).
- Northstar-www - Web server for AFS accounts
- Gateway - Off-campus access machine.
Consulting
- Systems Administration.
- System maintenance, planning, new technology research ...
- Faculty workstation support. Workstation installation and configuration
- Workstation security advice.
- General Unix consulting, shell scripting, applications.
- Statistics
- Software development (parallel codes, tuning, optimization)
- Help questions: manager@[machine_name],
manager@northstar
- Training (scheduled minicourses and special sessions on demand).
(3)
History of Unix
- UNIX was "created" 1969. UNIX time is kept from Jan 1, 1970.
- Ken Thompson, Dennis Ritchie (AT&T Bell Labs), Multics, PDP7, PDP11.
The PDP11 was a 16-bit machine, so programs were restricted to 64kB, and this encouraged
modular programming and cooperating processes.
- Dennis Ritchie wrote "C", a descendant of "B" and "BCPL".
Thompson and Ritchie rewrote the Unix kernel in "C" (1973) - greatly increased portability
- 6th edition of UNIX distributed in 1975. First version with widespread use outside AT&T.
- 7th edition distributed in 1978. Unix ported to VAX at AT&T, and to Interdata 8/32 and 7/32, and the IBM VM/370.
This gave Unix a 32-bit address space (4GB).
- University of California, Berkeley -- BSD UNIX (Berkeley Software Distribution), TCP/IP. BSD 4.4 and
later spinoffs (FreeBSD/NetBSD/OpenBSD)
are free of AT&T code restrictions.
- AT&T SVR4 API spec, POSIX, XPG4
- Today there are many "flavors" of UNIX's with slight differences. Commercial versions (IRIX, Solaris, AIX, HPUX etc.)
mostly derived from SVR4 codebase.
- Largest growth recently is in Linux, an open source implementation of Unix developed by Linus Torvalds
(and supporting cast of thousands)
UNIX history map
UNIX history map pdf
The name "UNIX" is a trademark, originally of AT&T, but since sold several
times - most variants have names other than "UNIX", but all derive from AT&T code
or API specifications originally.
(4)
Features of Unix
- Scalable and portable
Runs in many environments. TV set-top boxes, digital video recorders, bank ATM, phone switches, real time embedded systems, Macs, PC's,
Workstations, Servers, Crays, 12,000 CPU parallel processors.
- Multiuser
Even for a one user machine, Unix provides a protected environment. Multiple logins allow private space and shared
resources.
- Preemptive multitasking
Each program gets a "chunk" of system resources.
Many users and programs can run at the same time (in geek speak,
it has efficient context switching, good virtual memory, idle programs
are blocked waiting for I/O and can be paged out -- so you can do more with less resources)
- Multiprocessing
Unix efficiently supports computer hardware with multiple CPUs.
- Robust
Protected execution space, keeps on running. Makes it good for OLTP
and high availability applications. Failing user programs do not crash the
whole system (usually). Servers can often run for more than a year without rebooting.
- Multiple types of software run on the same computer
- Graphical desktop login, highly interactive software
- Command line logins, text-based programs, background programs, shell scripts
- Unattended programs, network daemons, cron jobs, mail filtering
- Networks and the Internet; Remote Access
UNIX has many "built in" communication tools. Native networking is TCP/IP, but other protocols
possible. Remote access was designed into the system from the start (with dial-in lines, even before
the internet existed). Network services such as mail (smtp), http, ftp, usenet news, IRC, remote login,
remote X display, remote printing, are all available.
(5)
How to get a Research Computing account
General Research Account
Discovery Cluster accounts
- Limited to faculty, staff and graduate students who need access to additional computing resources
for their research. Coop membership model.
Gateway (remote access) accounts
- Available to all members of the Dartmouth Community
- SSH to gateway.dartmouth.edu and log in as newuser.
- Respond to the questions from the newuser script and your account should be created
within a few days. You will need to enter your DND name and password to authenticate.
(6)
Logging in from a Macintosh or PC
There are many ways to talk to a UNIX machine. Terminal programs typically implement the
secure shell (SSH), telnet and remote login (rlogin) protocols. In addition to those,
X server software provides the necessary control over the display that remote graphical software needs.
Information on recommended terminal and Xserver software for MacOS and MS Windows is at
www.dartmouth.edu/~rc/help/faq/connect.html.
- Telnet and rlogin authenticate in clear text over the network, and so are insecure to network sniffers.
- Secure connections (username and password never appear on the network except in encrypted form)
are mandatory for all logins to Dartmouth Research Computing systems.
(7)
Logging in to UNIX
Where programs run; the login model.
- Three main environments for running programs
- Interactive session - X windows (GUI: local console or remote)
- Interactive session - command line (local console, or remote ssh session)
- Background operation (daemons, cron jobs, no user interaction)
- Command line (terminal) logins vs. X windows.
"X windows" is the standard graphical user interface for Unix.
It is used on workstation consoles, and can be redirected to remote displays.
Whether or not it
can be used depends on the capabilities of the terminal ("terminal" means
"whatever you are sitting at" - it may be a real terminal or a Mac or PC running
terminal emulation software). X servers are available for MacOS and MS Windows.
X windows is location independant - remote X windows
programs run the same as local (console).
- Programs run on the UNIX machine and you see output on your terminal.
The terminal may be remote (over the network), directly wired, or be the
console device (workstations).
- When you log in with a command line session, a special program called a 'shell'
is run, commands that you type are processed by the shell. Most of this class concerns the command line
interface because:
- it is always available (often dial-in access is too slow for good X-windows
display)
- it is scriptable and can be used to set up unattended jobs
- it is basis for the underlying tools run by the GUI, and the GUI menu configurations
- Certain Client-Server programs (Ex. Mathematica) can use a Mac or PC for display and
UNIX for number crunching, with a custom network protocol linking the two.
- Displaying graphical output on the local console is generally faster than displaying
remotely. Some graphical applications are coded in such a way as to make remote display almost impossible.
Remote display to another Mac/UNIX/Linux workstation often works better than to a PC.
- Don't confuse the User Interface with the Operating System.
X windows provides the mechanism for graphical displays, but does not mandate any particular
look and feel.
There are many different graphical interfaces based on the X-windows core libraries;
CDE, Gnome, KDE, ... .
It may even be configured to look similar to MS Windows or MacOS.
Similarly, there are various command line shells available which determine the
look and feel of the text-based interface.
(8)
Getting Help
research . computing @ dartmouth
System error messages
foo: Command not found.
Probably the commonest error message you will see. Every command given to a Unix system, whether
it is typed at a command line, read from a script, or generated by clicking an icon, actually
tells Unix to find a program and run it. If the command is mistyped, or incorrectly installed,
or your account tells the system to look in the wrong places, you will see this error message.
If you are getting a repeatable error message from any Unix command, and you mail us for help, try to
include the exact error message (cut and paste it into a mail program if possible).
Panic Buttons.
logoff
To stop a program
To stop output
When in doubt
| exit , ctrl-d, logout
ctrl-c
'q' , or quit (works on man pages, more, etc)
'?' or help or 'h' will sometimes bring up help.
|
Learning to fish
- Man pages: The 'man'(ual) pages provide in- depth
documentation of most of the systems features. There is also a database of man pages that you can search.
Type in
man
-k process
. Man pages have a standard format that we will look at.
- Graphical user interface usually has online help available somewhere.
On the SGI Indys and O2s, help is in the "toolchest" menu. On CDE, it is in the main toolbar.
- Learn: Found on many UNIX systems but not all. "Learn" provides text-based tutorials on some aspects of Unix.
- Help: Typing the command "help" on some UNIX systems will bring up some form of help (non-standard)
- Application-specific help: Many applications, particularly those with
X-windows interface, have extensive online help available within the program. This
doesn't help if you are having trouble finding or starting the program, so we try to
ensure standard man pages exist too.
(9)
Man Pages are your Friend
command name (man page section)
- Name
- Brief description of the command.
- Syntax
- This tells you how to invoke the command and includes command line options.
- Description
- Tells you more about the command.
- Options
- Explains the options that a command can accept, command line options modify the way in which a command works.
- Examples
- Shows you how to use the command, this can be one of the most useful sections if you find an example that does what you want.
- Files
- Tells you which files this command uses or depends on. Especially useful for system administrators types.
- Bugs/Warnings/Restrictions/Diagnostics ...
- Commands are qualified near the end of the man page, this area can usually be ignored for simple invocations of commands but is the first place to look if you encounter problems.
- See also
- This is usually a list of related commands, or a list of a family of commands. Think of it as a primitive hyperlink.
(10)
Man page example
nimbus:~>man who
who(1)
Name
who - print who and where users are logged in
Syntax
who [who-file] [am i]
Description
The who command, without an argument, lists the login name, terminal
name, and login time for each current UNIX user.
Without an argument, who examines the /etc/utmp file to obtain its
information. If a file is given, that file is examined. Typically the
given file will be /usr/adm/wtmp, which contains a record of all the
logins since it was created. Then who lists logins, logouts, and
crashes since the creation of the wtmp file. Each login is listed with
user name, terminal name (with /dev/ suppressed), and date and time.
When an argument is given, logouts produce a similar line without a user
name. Reboots produce a line with `~' in the place of the device name,
and a fossil time indicative of when the system went down.
With two arguments, as in `who am I' (and also `who are you'), who tells
who you are logged in as.
Files
/etc/utmp
See Also
getuid(2), utmp(5)
(11)
Searching for man pages by keyword
To find the man pages on a subject use:
man
-k [subject]
man ps
man -k system
man -k talk
Man page sections:
1 .......... User commands.
2 .......... Programming interface (system calls).
3 .......... Programming interface (external functions and subroutines)
4 .......... File Format
5 .......... System (administrative) commands. Sometimes also (8)
+ .......... Other sections.
(12)
Storing Information -- The File System
- UNIX stores information in files. Files can be named almost anything.
File names
Files and directories (folders) can be named almost anything, as long as they
do not contain "/" (since it has to separate directories in a path).
In practice, many programs will get confused if filenames contain
spaces and certain punctuation characters. Safe characters are [a-zA-Z0-9-_.]. Mixed
case is allowed and all operations are case sensitive (unlike Mac and Windows).
In particular, " and ' can be placed in filenames easily on Macs
and then transferred to Unix - legal, but can cause headaches.
Some old Unix systems had a 14-character limit; current
ones have essentially no limit. Macs and Windows have a 32-character limit.
Macs and Windows preserve case for display, but internally
treat upper and lower case as the same.
- The file system structure (as seen by the user) is similar to the hierarchical DOS and Mac
file system structure.
- The top file for the mac is the "desktop" file, for DOS/Windows it is the drive (C:,D: etc.) and for UNIX
it's "/" which is usually called root.
- All disks etc. are attached (mounted) somewhere below the root. Users usually do not need to know or care which
disk their files are actually on, only the mount point, which determines the full path name to a file.
- Everything in Unix is a file. Direct access to hardware (printers, tape drives etc.) and logical
devices is made through special files. Most of these device interface files live in /dev.
- When you are logged on to UNIX you always have a location in the file system which is
your current working directory. Filenames are assumed to be in this current working
directory, unless some other directory is explicitly named. (More on this later)
ls
- The
ls
command lists files in a directory, and various information about the files. You will use this a lot.
ls -l
- The
-l
flag (long) lists most of the information stored about a file.
(13)
The UNIX file system (cont.)
The file system can be thought of as an upside down tree with "root" as the starting point.
The file system contains directories (folders) and files.
Directories can contain other directories as well as files. A directory is just a special
type of file, containing file names and pointers to the internal data structures which hold the
permissions, ownership etc. for the file. Directories are manipulated using special utilities.
(14)
The UNIX file system (cont.)
- Note that in UNIX, disks are mounted on to the file system at any level, not just the root
(cf. MacOS "desktop"). The boot disk always provides the root (/) directory.
- On a Macintosh you need to know the disk your file is on.
This is also true on a DOS/Windows machine. On a UNIX machine you just need to know the path to your file.
File systems can be remote, actually attached to some other computer on the
network. They can normally be treated just the same as local file systems, except
they may be a bit slower. The Northstar system keeps all the user files on central
servers. Some Central Research machines keep local files in addition to using the
remote file servers.
Most Unix systems can also mount disks with "foreign" file systems (e.g. MAC HFS, DOS FAT, NT, OS/2). Operations on
such file systems are restricted to conform to the procedures of the foreign system (permissions, owner, filename limits)
(15)
Home Directories
- Every user on a UNIX machine has a home directory. This is always the initial working
directory when you first log in.
- A home directory is owned by you (we will look at permissions later); a private work area.
- Home also has some special startup files that tell UNIX and the shell how to deal with you.
For example your ".forward" file tells the UNIX mailer where to forward your
mail. [.files are usually special in some way -- they are usually similar to stored user preferences]
(16)
Working with Directories
Creating, moving and removing directories.
mkdir
frog
- Makes a directory named "frog".
mv
frog toad
- Moves a directory, removes the old directory (effectively a rename). This only works if they are on the same filesystem.
cp
-r toad lizard
- Copies one directory to another. The "-r" flag says to recursively copy files and subdirectories. This is
only one (not the best) of several ways to copy a directory hierarchy.
rmdir
toad
- Removes a directory, if the directory is empty.
rm
-r toad
- Will remove a directory even if it is not empty. Again, "-r" indicates recursive action, deleting all the contents first.
Use with caution.
ln
-s lizard snake
- Links (similar to a Macintosh alias) one file to another. Think of it as
creating a "pointer" to a file or directory; good for creating easy access to a
shared resource. Take a look at this with a
ls -l.
. "lizard" is the real file, while "snake"
is the alias for it.
lrwxrwxrwx 1 jwallace web 6 Oct 14 20:02 snake -> lizard
(17)
Navigating Directories
Commands to try
pwd
- Print Working Directory. Shows you where you are in the file system - usually this information
is configured to be part of the shell prompt too.
cd
[directory]
- Change Directory. With no argument, this changes the current directory back to your home directory. With
a directory name, it tries to change directory to the named location, as long as it exists and you have permission to
go there. The directory name can be an absolute or relative path (more on this later).
cd ~username
- Shortcut to go to the home directory of username.
cd ..
- Moves up one directory
ls
- Lists the files in the current (working) directory.
ls path_name
- E.g.
ls /usr/local/bin
Lists the files in the named directory.
ls -al
- Shows a "long" listing of the files. Include "hidden" files because -a was given.
ls -FR
- Recursively lists all sub-directories and displays executable files with an appended
"*" and directories with an appended "/".
Don't do this starting at the root directory!
Command history in the tcsh: try pressing the arrow keys to recall previous
commands and edit them before re-executing.
(18)
Pathname Examples
- Path name components are separated by "/". A path name can be relative or absolute.
Relative path names start from the directory you are in and absolute path names start from "/".
./ = the current directory.
../ = the parent directory.
Note that "." and ".." are real directories which reside inside of every directory on a Unix filesystem.
Examples
../people2/Mary is a relative path.
projects/dataset1 is a relative path.
/people2/Mary is an absolute path.
(19)
Files: Creating, Listing and Examining
Sidebar: standard I/O and redirection
Most commands send their output to "standard output". The shell provides a
mechanism for redirecting this to a file, or to another program. The default is to the
terminal. Many programs also read some input (default is the keyboard of the terminal).
Again, there is a mechanism to tell any program to read its input from a file or another program instead.
-
> filename
- send output to the named file, creating it if needed. This is most common way new files
are created
>! filename
- send output to the named file, creating it if needed, and forcing overwrite
>> filename
- append output to the named file, creating it if needed
< filename
- read input from the named file
prog1 | prog2
- run "prog1" and send its output to the input of "prog2". Both
programs execute in "parallel". Actually they are usually timeshared, but appear to
both run simutaneously
These facilities are available to all programs, but it is up to the individual programs
whether they respect the conventions.
Creating a file
echo
" This is a test file" > test_file
- This creates a file using "echo" and "redirecting" the output to a file.
ls
-a > ls_file
- This creates a file by taking the output(standard output) of the "ls" command and " redirecting" the output to a file.
ls -FR /people2 | wc > how_many
- This creates a file by taking the standard output of the "ls" command and "piping" it in to the "wc" (word count) command and then redirecting" the output to a file.
touch
empty_file
- Creates a empty file! Useful way to signal an event.
Looking at files
more
[file_name]
- Will display the contents of a file one "page" at a time. Can also display the contents of a data "stream" one page at a time. Ex.
ls -FR | more
more /usr/man/cat1/cc.1
- The space bar will move you to the next page and the return key will move you down a line. A "q" will end the more (more can also search through a file see the man page).
cat
/usr/man/cat1/cc.1
- Streams the file to "standard output". What else can cat do??
head
[file_name]
- Display the first few lines of a file
tail
[file_name]
.
- Display the last few lines of a file
Most of these utilities are intended for examining text files, with lines separated by
"Newline" characters. Most files in Unix tend to be text files. The internal structure
of binary files is application specific, and with the exception of cat
,
these utilities are not very useful with them. Two utilities, strings
and
od
are very useful for examining binary files outside of their specific
applications.
(20)
Files: Copying, Moving and Deleting
cp
how_many not_enough
- Copies the first file to the second file, does not remove the first file.
cp
-r unix2 unix2copy
- Copies an entire directory. Creates a new directory.
mv
not_enough booty
- Moves file "not_enough" to file "booty". If the destination name is on the same device
as the source, this is just a rename (and so it is very fast). For diffferent devices, a
mv
is equivalent to a cp
followed by rm
rm
not_enough booty
- Removes files "not_enough" and "booty". BEWARE of filenames with embedded spaces (see below).
Type in "which rm
" (which
tells you what command is being run,
many times it will tell you the path) .
Try rm -f
on a file.
(21)
Unix Editors
UNIX editors are powerful tools but (sometimes) not very user friendly.
vi
is the standard Unix text-mode (no X windows) editor. The main advantage of it
is that it is always available on every system. It is powerful, but not the most intuitive to learn.
nedit
is a good X windows editor, available on most platforms. It is similar
in style to MacOS and Windows editors, but has many advanced features too.
Emacs
is the most powerful editor, great for programmers. It is extendible, uses
X windows if available, but runs in text mode if needed.
Integrates well with other gnu tools (e.g. gdb).
Xemacs
is a more GUIfied version of Emacs. Pull down menus for the most commonly
use functions make it easier to learn.
Joe
and pico
are simpler editors (text mode only). Pico is the message composition editor
used inside of the pine mail client, and it very simple to learn.
vim
is a vi-clone with many enhancements - sort of midway between
emacs and vi. It also uses X windows for additional GUI features if available, but can run in text mode.
Ex
and ed
are also editors, best avoided. They are line-mode editors (as opposed
to full-screen text mode).
edit is often aliased
to "your favourite editor", but can be linked to ed on some systems.
Note that these are editors, not word processors or
text formatters.
(22)
The vi editor
Vi has modes. Insert mode and command mode. When you first enter vi you are in command mode and anything you
type will be interpreted as a command. This of this as selecting actions from the menus in MS Word.
Vi needs to be able to address the terminal window you are working in, so the setting of the $TERM variable
is important. Some systems get confused by resizing the terminal window while vi is running.
When you are in command mode many commands which affect more than one line of the file are prefaced with a ":".
An example is :w
which writes out or saves the current file.
When you are in insert mode you can type in text, when you are in command mode you can issue commands to vi
(search for a string, save the file ...).
Basic vi commands:
vi
file_name
or vi
- Enters vi using the named file as the file to edit.
view
file_name
- Opens a file in read only mode. Means that you cannot easily change the file. If you make a change to the file you can force it to write with
:w!
.
<esc>
- The escape key, tells vi to leave insert mode and go into command mode. You cannot exit vi unless you are in command mode.
ZZ
- Exit vi.
:set showmode
- Shows insert mode in the lower right hand corner of the screen. Very helpful when first learning vi.
:q!
- Force a quite without saving (if you mess up)
(23)
Vi navigation
Vi commands continued.
i, I
- Enter insert mode before current character.
I
inserts at start of line.
a, A
- Enter append mode (like insert, but after current character).
A
appends to end of line
o, O
- Enter insert mode on a new line below current line (open).
O
goes to insert mode on a new line above the current line.
ESC
- The Escape key stops insert mode and returns to command mode.
Pressing Escape in command mode just beeps.
Vi navigation
On almost all terminals and terminal emulation software, the arrow keys will work as expected.
Alternately, in command mode, the h,j,k,l
keys behave as arrow keys.
(24)
Vi commands
Positioning in a file
[ctrl] - F
- Forward one screen.
[ctrl] - B
- Backward one screen.
[ctrl] - D
- Scroll down half screen.
[ctrl] - U
- Scroll up half screen.
:1
- Go to first line in file.
:$
- Go to last line in file.
Interrupting, Canceling
[esc]
- End an insert or incomplete command.
[ctrl] - C
- Interrupt.
[ctrl] - L
- Redraw screen.
Deleting and removing.
dd
- Delete the current line.
x
- Delete current character.
dw
- Deletes a word.
de
- Deletes a word but leaves punctuation.
3 dd
- Deletes 3 lines.
Other commands
:e
- Re-reads the last saved version of the file.
:wq
- Writes the file and quits.
:q!
- Quits and does not write out changes.
(25)
The login shell
- When you log in to a UNIX machine with a command line session, or open a
terminal window in a graphical session, you get a shell.
A shell takes commands that you type in decides what to do with them.
- There are many different shells available. Csh(C-shell), sh (Bourne shell),
tcsh, kcsh, bash (bourne again shell) --- you can even write your own shell.
- Your login files (.cshrc .profile) set up some defaults when you log in.
These defaults create an environment for you.
This environment includes setting your search path so that common commands will work, setting
the man page search path, setting the default printer, terminal type and more.
You can customize these log in files for your environment.
- Shells have some nice features that every user can take advantage of: history, aliases etc.
- The shell is also a programming language. The simplest of shell scripts is just a file
containing commands as typed.
- Some commands are interpreted directly by the shell (internal commands), while
for most of them, the shell will create a new process.
All the internal commands are specific to the particular shell you are using.
- The login "shell" may be set to a custom application, not a general purpose shell (e.g.
Webster, or the "newuser" login on some systems)
- Unattended background operations, e.g.
cron
or at
jobs still have
a shell process to interpret the commands, even though there is no user interface.
(26)
More on shells
The Environment
The environment variables are a way to pass information to programs - any program can
examine these strings and modify its behaviour (e.g. PRINTER is looked at by the
lpr
program. Some of these environment variables can be changed,
others are preset by the system.
env
HOME=/classes/rcomp1
SHELL=/usr/local/bin/tcsh
TERM=vt100
USER=rcomp1
PATH=/bin:/usr/bin:/usr/local/bin:/usr/bin/X11:/usr/ucb:.
HOSTTYPE=sun4
VENDOR=sun
OSTYPE=solaris
MACHTYPE=sparc
SHLVL=1
PWD=/classes/rcomp1
LOGNAME=rcomp1
GROUP=users
HOST=sunray.dartmouth.edu
MANPATH=/usr/local/man:/usr/man:/usr/share/man
PRINTER=berry-public
Command Line Arguments
The other way information is passed to applications is through the command line arguments. Everything
following the program name on the command line, except things interpreted directly by the shell,
is made available to the program. Typically these are option flags (to modify the behaviour of the program)
and filenames (for the program to act on). The program may then also have a direct interaction
with the use via a text or graphical interface. Most program use all of the methods.
Many featuresof Unix are designed to help in automating procedures (Unix users are lazy). Passing
information via the environment and the command line is scriptable, while direct interaction via the
keyboard and mouse is not.
Job control
Shells also allow you to place jobs in the background and run multiple processes from one
shell session, although multiple windows are easier for this.
ls
-FCR > save_it &
- The "&" says make this a background job.
^Z
- control-Z sends a "suspend" signal to a currently running process
bg
- continue execution of a suspended process in the background (must not try to
read from the keyboard, but can write to the screen)
fg
- resume execution of a suspended program in the foreground
jobs
- list background jobs running in current shell session.
(27)
Shell commands
Some quick shell commands to try:
These commands are all csh
/tcsh
internal commands, designed to make life easier
at the command line
source
.cshrc
[.login]
- Re-reads your log in files as if(almost) you had logged in again. Good for making changes to login files and testing them. Don't make changes to log in files and then log out and back in to test them. You might not be able to log back in.
history
- Shows you the history of commands that you have issued. You can raise or lower this number.
[Up Arrow]/[Down Arrow]
- The Up/Down arrow keys will move through the history list of previous commands, so that you
can re-execute them with minmal retyping.
!!
- Execute the last command again.
!23
- Execute command number 23 again.
alias
- Shows aliases. An alias is a command shortcut. Can make things much simpler and faster.
alias his history
- Creates an alias called "his" for the command "history".
(28)
Printing
In Unix, the functions of preparing a file for printing (translating formats etc.) and
sending the file to the printer, are usually separate. At Dartmouth essentially all
public printers expect to be sent postscript, and the print spoolers detect when a file is already
in postscript form and pass it unchanged. Anything else is assumed to be pure text, and is
automatically translated to postscript. Programs which prepare their own formatted print
files need to be set for postscript output.
All print jobs are queued. Access to the printer is controlled by a spool queue.
Printers may be local or remote - there is no difference in the way they are used.
All public printers at Dartmouth are remote. The "built-in" print command provided
by many applications actually prepares a temporary file with the material to be
printed, and calls one of the following utilities.
lpr
[-Pprinter] file [file ...]
lpr
sends the named file(s) to the printer (actually, append this job to the queue for the printer). If no printer is named, the
variable $PRINTER is used. If it is not set, the system
default printer is used. If no files are named, standard input is read, so that lpr
can be the end of a pipe.
lpq
[-Pprinter]
- Check the queue for the named printer.
lprm
[-Pprinter] job-number
- Remove a print job from the queue, if it hasn't started printing yet.
lp
[-dprinter] file [file ...]
lp
is functionally similar to lpr
. On most modern systems, both commands are available.
"lpr" comes from the BSD heritage, while "lp" is from the SysV heritage. The "lpstat" and "cancel" commands are also
part of the SysV printing tools.
lpstat
- Check printer status, similar to "lpq"
cancel
- Cancel a print job from the queue, similar to "lprm"
Utilities to provide more control over the formatting of plain text files vary from
system to system.
Check out pr
and lwf
on Nimbus and Northstar, or postprint
on Sunray.
(29)
Other UNIX commands
An assortment of other useful UNIX commands --- remember that many commands can be "joined" together with
the pipe "|" and that output from a command can be sent to a file with the redirect symbol.
Line oriented commands
grep
- Searches a file for the string and reports on all matching lines.
find
- Searches though a file system. E.g.
find /usr/share -name \*zip\*
will search the filesystem below "/usr/share" for filenames with "zip" in them.
sort
- Sorts (alphabetic or numeric) lines in a file.
cut
- Cuts fields out of a file.
who
- Show who is logged to the system and what they are doing. The command
w
is almost identical to who
mail
- A very basic mail user interface. Not recommended for reading mail, but very handy
for sending mail. Typically used to mail yourself a notification that a long-running
computation has finally finished.
at
- Execute a command at a future time - useful for background jobs
script
- Capture a terminal session to a file. Saves what you and the computer type to the
screen (stdout). Saves the output to file "typescript".
sed
- A stream editor, really nice in pipes.
awk
- Pattern matching language. Very powerful.
Screen oriented commands
talk
- Talk to another person on the internet.
irc
- Internet relay chat. Talk with lots of people on the internet.
pine
- A mail reader. There are other mail readers available too. You can even use a mail
program at the end of a pipe. E.g.
w | grep sam | mail $USER
.
This runs the "w" command and
looks for user (string) sam; the output is then mailed to $USER (always set to your username).
textblitz
- A purely text-based interface to the Blitzmail system
top
- Show the most active processes on the system. An animated, sorted,
ps
listing.
(30)
References, Resources, Man pages etc.
The following is a sampling of the many available books on the subject.
The links are to publishers web sites, or Amazon.com.
- Learning the UNIX Operating System,
by Peek, Todino and Strang.
5th Edn. O'Reilly (2002).
Generic reference, mostly command line tools.
More info
- Unix in a Nutshell,
by Robbins. 3rd Edn. O'Reilly (2002). SVR4/Solaris
Other versions available, tuned to BSD, SCO etc.
More info
- Unix for Oracle DBAs: pocket reference,
by D. Burleson, O'Reilly (2001)
Small, dense reference intended for Oracle DBAs migrating to Unix
More info
- UNIX: Visual Quickstart Guide,
by D. S. Ray and E. J. Ray, Peachpit Press, (1998)
Command line tools only. Almost no pictures, in spite of the title.
More info
- Unix Made Easy,
by J. Muster, McGraw-Hill 3rd Edn. (2002)
Large book but not very dense. Mostly command line tools
More info
- Unix for the Impatient,
by Abrahams and Larson, Addison Wesley, 2nd Edn. (1997)
Command line tools only. Looks good but is fairly old.
More info
- Life with UNIX: A guide for Everyone,
by Don Libes and Sandy Ressler.
Prentice Hall, out of print.
- Unix Unleashed,
by Anderson and Johnston, SAMS, 4th Edn. (2002)
More of an administrator's handbook. Huge.
More info
- The UNIX Time-Sharing System,
Dennis M. Ritchie and Ken Thompson, Comm. ACM 17, 7 (July 1974), 365-375.
The original publication describing Unix.
More info
- The Art of Unix Programming,
Eric Steven Raymond, Addison Wesley, (2004).
The author has made the entire text available online via a Creative Commons license at
(www.catb.org/~esr/writings/taoup/html/), as well as a traditional printed version.
A great book on the Unix way of doing things. Heavy on philosophy, light on technical details.
More info
Introduction to Unix: Course Handout
(last update 07 July 2022) ©Dartmouth College
http://caligari.dartmouth.edu/~rc/classes/unix1