UNIX: Level I
This on-line course was written by Alan Ferrenberg. He would
welcome any comments you might have
on this course.
This page has been accessed
times
since May 14, 1996.
Conventions
Typewriter font = commands
Italic typewriter font = arguments to commands
Bold typewriter font = response from computer
% = system prompt
ENTER = press the ENTER key
^u = hold the Ctrl key and type u
Getting Help
- Your local system administrator should be your first point of contact
- The EITS Helpdesk provides e-mail and phone support to UGA faculty,
staff and students for general computing-related questions
- e-mail: helpdesk@listserv.uga.edu
- phone: 542-3106
Workstation Support
EITS provides UNIX and workstation support to the UGA computing
community through the Workstation Support Group. To contact WSG
Flavors of Unix
- All the basic commands are the same. Differ mainly at the
programmer's level.
- Major versions are the Berkeley Standard Distribution (BSD)
and AT&T System V.
- Some Workstation Variants: SunOS & Solaris(SUN), AIX(IBM),
IRIX(SGI), ULTRIX(DEC), HP-UX(HP)
- Some PC & Mac Variants: BSDI, LINUX, SCO, ESIX, XENIX,
AUX
The Unix Environment
- A shell is a program that reads commands and executes them.
- The common shells are the C shell (csh),
the Bourne shell (sh),
and the Korn shell (ksh).
- The shells differ in scripting syntax and a few other minor
ways.
- You may be able to change your login shell using the chsh
command.
The Unix Environment
- If the following files exist in your home directory, they
will be executed whenever you login:
- csh: .cshrc and .login
- sh and ksh: .profile
- Place commands or tasks into these files that you would like
performed whenever you login.
The Unix Environment
It is possible to create aliases for commands in your login files. For
example, to alias the command ls -l to be dir:
- csh:
- alias dir 'ls -l'
- sh:
- dir() /bin/ls -l $*
- ksh:
- alias -x dir='ls -l'
Shell Variables
- Shell variables are simply strings of characters that hold
other values, for example, $HOME may stand for /home/jones.
- Shell variables are indicated to the shell by a "$"
as the first character.
- To see all of your built-in shell variables, use the set
command.
Shell Variables
In general, shell variables that are special to the shell are
written in uppercase, and user-assigned shell variables are written
in lowercase:
- Examples:
- $HOME
- $myvar
- To see the value of a specific shell variable, use the echo command:
echo $HOME
Shell Variables
Several environmental variables are special to the shell.
These include:
- $HOME your login directory
- $PATH directories that will be searched for commands
- $TERM your terminal type
- $DISPLAY your local hostname for X windows applications
Shell Variables
To set shell environmental variables:
- csh:
- setenv PATH /usr/bin:/bin
- setenv PATH $PATH\:$HOME/bin
- sh, ksh:
- PATH=/usr/bin:/bin
- export PATH
- PATH=$PATH\:/usr/bin:/bin
- export PATH
General Command Syntax
The general form of a command in UNIX is:
- command -options parameters
command is the name of any file both executable and
locatable. A file is locatable if it is in one of the directories
defined by the PATH variable or the path is supplied in the name.
- Example:
- ls -l /home/jones/dir1
General Command Syntax
Filename shorthand: there are some special characters that
the shell recognizes as shorthand for filenames. These include:
? - substitute any character at that position: file?.txt (file1.txt,
file2.txt, ...)
* - substitute any number of any character(s) at that position: file*.txt (file1.txt, file1a.txt, file.txt, ...)
General Command Syntax
- Shell variables in commands are expanded by the shell.
- Shell variables are indicated by a "$" as the first
character (e.g. $PATH, $HOME).
- Examples:
- echo $PATH
- cd $HOME
- ls $dir1
General Command Syntax
It is possible to redirect command input and output away from
the keyboard and screen and from/to files:
- <
- Read standard input from file
- >
- Write standard output to new file
- >>
- Append standard output to file
- |
- Send standard output from the first command to the standard input
of the next
- Examples:
- ls > dir.list
- ls $dir1 | more
The UNIX Filesystem
- The general form is a tree.
The top of the tree, commonly called the root, is denoted by a
single forward slash (/). All files are located "below"
the root directory.
The UNIX Filesystem
The UNIX Filesystem
Filenames are specified in one of three forms:
- Absolute paths begin with a slash:
- more /home/jones/adir/kermit.txt
- Relative paths (no slash at the beginning) start their
search in the current directory:
- more adir/kermit.txt
- Filenames with no paths are presumed to be in the current
directory:
- more kermit.txt
The UNIX Filesystem
- Show the current directory with the pwd command.
- The cd command (change directory) is used to move
around the directory tree.
- Examples for the tree given above are:
- cd /
- cd /usr/jones
- cd
The UNIX Filesystem
- There are two special relative path symbols, '.' and '..'
. These indicate the current directory (.) and its parent (..).
- Examples:
- ls ../smith
- cd ..
mkdir:
Create a New Directory
- Create new directories with the mkdir command. It makes
the named directory.
- Some UNIX systems allow creation of many levels of directory
at once.
- Examples:
- mkdir project
- mkdir project/data
rmdir:
Remove a Directory
- Delete a directory with the rmdir command.
- The directory to be deleted must be empty of both files and
other directories.
- Directories must be deleted from the bottom up.
- Examples:
- rmdir project/data
- rmdir project
cat:
Print a File to the Screen
cat is an all-purpose command:
- Print one or more files to the screen:
- Example:
- cat filename(s)
- Create a file:
- Example:
- cat > x.y <ENTER>
type in
some text
^d (ends the input)
- Concatenate (append) files:
- Example:
- cat a b c > d
mv: Move a
File
The mv command moves and/or renames a file.
- Rename a file:
- Example:
- mv a.dat a.old
- Move a file to a different place in the filesystem:
- Examples:
- mv a.dat ../save (directory)
- mv a.dat ../save/a7.old
- The destination file is replaced if it exists.
- The original file is removed, so use with caution.
cp: Copy a
File
- The cp command copies a file from one name or place
to another.
- Examples:
- cp a.dat a.old
- cp a.dat ../save (directory)
- cp a.dat ../save/a.old
- The destination file is replaced if it exists.
rm:
Remove/Delete a File
- The rm command is used to remove or
delete one or more files.
- The -i option will ask you about each file
to be removed.
- There is no way to recover a file after it has been removed.
- Examples:
- rm x.y
- rm a.dat b.dat c.dat d.dat
- rm -i *.o
ls: List Files
in Directories
- The ls command lists the files in directories.
Some of the more common options are:
-F Flag type of file with "/", "*", "=", "@"
-l List files in a long, descriptive format
-a Show all entries, including "dot" files
-t List files in reverse order of time created
- Examples:
- ls
- ls -Fla
- ls -Fla *.c
- ls -Fla adir (directory)
find: Find
Files and Manipulate Them
file:
Determine the Type of a File
- The file command is used to determine the type of
contents of a file.
- Use this before using either cat or more
to look at a file at a terminal.
- Types are: text, binary executable, commands, data, etc.
- Never bring binary executable files to a terminal and be careful
with data.
- Examples:
- file x file * | more
more: Look at
a Screen of Output
- The more command is used to look at the contents
of a file, one screenful at a time.
- more also allows you to break up output to standard
output to one screen at a time.
- To continue, press the spacebar; to quit, "q"
- For help, press "h" while more-ing a file.
- Examples:
- more letter.doc
- more *.doc
- ls /usr/lib | more
tail: Look
at the End of a File
- The tail command displays the end of a file.
- Specify the number of lines displayed by including a number preceded
by a "-".
- The tail of a file being created by another process may be viewed
continuously (this flag varies from system to system; check the
tail man page).
- Examples:
- tail letter.doc
- tail -100 letter.doc | more
diff: Compare
Files
- diff reports differences between files.
- Output lines are flagged with "<" for the first
file and ">" for the second file.
- Example:
- diff file1 file2
1c1
< this is file1
---
> this is file2
Processes and Job Control
- Unix is a multiuser, multitasking operating system (task =
process).
- On a Unix system, it is possible to run many programs and
commands (processes) at the same time.
- Each user can have multiple processes running at the same
time.
- The ps command is used to look
at processes running on the system.
- The arguments to and output from this command depends on the
flavor of Unix that you are running.
- ps by itself will show all running processes that are
associated with your current terminal.
- Example of output from ps
PID TTY TIME CMD
16035 pts/1 0:00 ps
19628 pts/1 0:00 ksh
- System V systems (AIX, IRIX, Solaris, Linux):
- ps -ef all processes, full listing
- ps -fu user all processes associated with user,
full listing
- BSD systems (SunOS, AIX, BSD/OS):
- ps aux all processes, full listing
- ps u all processes associated with running user,
full listing
Processes and Job Control
When a command or program is run from the command line, in most
cases you will have to wait for it to finish before you can enter
other commands. This is known as running interactively.
It is possible, however, to run commands and programs detached
from the controlling terminal; this is known as running in
the background. To run a command in the background, place an ampersand
(&) at the end of the command line.
- Example: Find all the files in your directories that end in
.txt and list them, but don't wait for the command to complete:
- find $HOME -name \*.txt -print > txt.list &
Note that the output is redirected to a file - otherwise, it would
be sent to standard output and may interfere with what else you
are doing.
- If the program expects input from the keyboard, this input
must be redirected from a file or else the program will fail.
Processes and Job Control
- Typical session:
runme > cmd.out &
[1] 19518
jobs
[1] + Running runme > cmd.out &
[1] + 19518 Done runme > cmd.out &
- The number in square brackets is the jobid and is identified
by a leading "%"
Processes and Job Control
Other useful commands:
- jobs -- list your background
jobs
- kill pid -- kill the
specified process
- Example:
- kill 12345
- kill %jobid kill the background job
- Example:
- kill %1
- ^z suspend the current interactive job
Processes and Job Control
- bg put a suspended job in the background
- fg %jobid bring a background job to the foreground
- nohup continue to execute
the command after you log out, i.e., make the command immune to hangups:
- Example:
- nohup command &
- See the manual page for your shell (csh, ksh etc.) for additional
job control commands
File Archiving Commands
- There are a number of commands built into Unix to provide
the ability to archive and compress files.
- A number of public domain programs are also available.
File Archiving Commands
- The tar command is used to write data
to tape and create multi-file archives on disk.
- Examples:
- Create file containing all files in a directory:
- tar cvf files.tar adir
- Extract these files:
- tar xvf files.tar
- List the files in the tar archive:
- tar tvf files.tar
File Archiving Commands
- Use the compress command to shrink
files. The compress command will add a ".Z" to the end of the compressed file:
- compress letter.doc
- To look at the contents without uncompressing, use the zcat command:
- zcat letter.doc.Z |more
- Run uncompress to restore them:
- uncompress letter.doc.Z
- Often, tar and compress are used together
to create file archives for storage or distribution.
- These files will have a name of the form "something.tar.Z"
- To look at the contents of a ".tar.Z" file:
- zcat files.tar.Z | tar tvf - | more
- To untar the file:
- zcat files.tar.Z | tar xvf -
File Archiving Commands
- gzip (Gnu zip) is a public domain compression program.
The standard suffix added is ".gz" or ".z"
- To zip files:
- gzip file.txt
- To unzip files:
- gunzip file.txt.gz
- To look at the contents without unzipping the file:
- gunzip -c file.txt.gz
- Other commands may be available, including:
- zip and unzip - compatible with DOS zip. File
suffix is ".zip". This is a public domain program and
not included with the standard Unix operating system.
- pack and unpack - usually included with
Unix. These files have the suffix ".z"