Introduction To UNIX Course Notes: Naval Research Laboratory
Introduction To UNIX Course Notes: Naval Research Laboratory
Washington, DC 20375-5320
Introduction to UNIX
Course Notes
1. Introduction ..........................................................................................1
2. UNIX History, Versions, and Shells ................................................2
3. Sample Session ....................................................................................3
3.1. Logging In ...................................................................................3
3.2. Changing Shells ..........................................................................3
3.3. Terminal Setup............................................................................4
3.4. Changing Passwords .................................................................4
3.5. Help ..............................................................................................5
3.6. Logging Out ................................................................................5
4. Commands, Files, and Directories ...................................................6
4.1. Command Syntax .......................................................................6
4.2. File Types.....................................................................................6
4.3. File Naming Conventions .........................................................7
4.4. Wildcards.....................................................................................8
4.5. Directory Structure.....................................................................9
4.6. Pathnames .................................................................................10
4.7. The Path Variable .....................................................................11
5. File Manipulation Commands ........................................................12
5.1. Listing Files ...............................................................................12
5.2. Displaying Files ........................................................................13
5.3. Copying Files ............................................................................14
5.4. Moving (Renaming) Files........................................................15
5.5. Removing (Deleting) Files.......................................................16
5.6. Printing Files .............................................................................17
6. Directory Manipulation Commands..............................................18
6.1. Creating Directories .................................................................18
6.2. Changing Directories ...............................................................18
6.3. Displaying Your Current Directory.......................................19
6.4. Removing (Deleting) Directories ...........................................19
7. Miscellaneous Commands...............................................................20
7.1. Listing Users .............................................................................20
7.2. Finding Files..............................................................................20
7.3. Searching Within Files .............................................................21
7.4. Sorting Files...............................................................................21
7.5. Comparing files ........................................................................21
8. vi Editor ...............................................................................................22
9. File Protection.....................................................................................27
9.1. User and Permission Types ....................................................27
9.2. Displaying File Protection.......................................................27
9.3. Changing File Protection.........................................................28
9.4. Default Protection ....................................................................29
10. Command Processing .......................................................................30
10.1. I/O Files Redirection ...............................................................30
10.2. History .......................................................................................33
10.3. Command Aliasing ..................................................................34
11. Job Processing ....................................................................................35
11.1. Background Jobs.......................................................................35
11.2. Background I/O .......................................................................35
11.3. Job Control ................................................................................36
11.4. Process Status............................................................................36
12. Initialization Files .............................................................................37
13. UNIX Electronic Mail........................................................................38
13.1. Email Addresses .......................................................................38
13.2. Sending Mail .............................................................................39
13.3. Reading Mail .............................................................................41
13.4. Mail Setup Files (.mailrc) ........................................................43
13.5. Mail Forwarding.......................................................................43
1. Introduction
This introductory level course is designed to provide new UNIX
users with an understanding of basic concepts and fundamen-
tals. The course begins by covering the necessary background
information you must have to effectively use the system. You
will learn about:
• the various versions of UNIX and shells
• basic UNIX processing—from logging in, setting up your
terminal, issuing commands, and getting help, to logging
out
• UNIX directory structure and file naming conventions
1
Introduction to UNIX
Versions of UNIX
• Berkeley UNIX (BSD)
• AT&T UNIX (System V)
2
Introduction to UNIX
3. Sample Session
3.1. Logging In
login: stu01
Password: xxx (password does not echo on screen)
3
Introduction to UNIX
Terminal Options
The stty command sets or reports terminal I/O options:
% stty [-a] [option]
-a show all settings
stty is commonly used to change the keys used to erase
(delete) characters or lines and interrupt processes:
% stty erase '^?' kill '^U' intr '^C'
3.4. Changing Passwords
Passwords should be immediately changed whenever you first
login to a new system and periodically thereafter.
% passwd
Changing password for stu01 on amp.
Old password:
New password:
Retype new password:
%
Note—The CCS Training workstation, "amp," uses NIS for
account administration. Use the following:
% yppasswd
4
Introduction to UNIX
3.5. Help
The man command can be used to reference the online UNIX ref-
erence manual. Information can be viewed on:
• Specific commands
• General topics (using the -k option)
Examples
To view information on the passwd command:
% man passwd
% logout
% Ctrl-D
5
Introduction to UNIX
Examples
% ls -a
% cat test.dat
% cp -i test.dat old.dat
6
Introduction to UNIX
Notes
• UNIX is case sensitive—upper and lowercase letters are
distinct in file names, command names, passwords, etc.
• There are no file "types" incorporated into the file name as
in other operating systems. However, some utilities
expect filenames to end in a particular extension. For
example, the C compiler (cc) expects source file names
ending in ".c"
• There are no multiple versions of files in UNIX as there are
in some other operating systems. Be careful not to acci-
dentally overwrite existing files when editing, copying,
and renaming.
7
Introduction to UNIX
4.4. Wildcards
8
Introduction to UNIX
program.c test.dat
9
Introduction to UNIX
4.6. Pathnames
There are two ways to specify a file:
• Full path name
• Relative path name
Example
/home/vonk/test.dat
Examples
test.dat
./myprog
../stu01/program.c
10
Introduction to UNIX
The path variable tells the system where to find command exe-
cutables and is set as follows:
% set path=(directory ... directory)
Directories are named in the order you wish them searched. For
example:
% set path=(/bin /usr/local/bin .)
Once the path is set, you can execute any program, including
executables of your own, by simply entering their filename:
% ls
% myprog
11
Introduction to UNIX
% ls -l test.dat
- rw- r-- r-- 1 vonk group 334 Jun 10 15:42 test.dat
filename
last modification
size (in bytes)
group
owner
links
file protection
file type
12
Introduction to UNIX
13
Introduction to UNIX
Format
% cp file1 file2
% cp file1 [file2 ...] directory
% cp -r directory1 directory2
Notes
• if target filename already exists, it will be overwritten
• use the -i option to prompt for confirmation in case target
file exists and is about to be overwritten (answer "y" or "n")
14
Introduction to UNIX
Format
% mv file1 file2
% mv file1 [file2 ...] directory
% mv directory1 directory2
Notes
• if target filename already exists, it will be overwritten
• when moving directories, if target directory exists, the
original directory is moved to a subdirectory of the target
directory
• use the -i option to prompt for confirmation in case target
file exists and is about to be overwritten
Examples
% mv test.dat myprog.dat
% mv program.c sub1.c sub2.c source
% mv class intro-unix
15
Introduction to UNIX
Format
% rm filename
% rm -r directory
Notes
• write permission on directory is required
• read or write permission on the files themselves is not nec-
essary (in this case, the file permission is displayed and
you are prompted for confirmation of deletion)
• can not remove "." or ".."
16
Introduction to UNIX
To delete a job from the print queue, use the lprm command:
% lprm job-id
17
Introduction to UNIX
Example
% mkdir class
Example
% cd class
18
Introduction to UNIX
19
Introduction to UNIX
7. Miscellaneous Commands
7.1. Listing Users
% who identifies all users currently logged in
% whoami identifies the current session
20
Introduction to UNIX
See the sort man page for more information on sort options.
7.5. Comparing files
The diff command displays differences between pairs of files,
producing a list of lines that must be changed (c), appended (a),
or deleted (d) to make the first file match the second. Lines from
the first file are prefixed by "<", lines from the second by ">":
% diff filename1 filename2
The -b option ignores trailing blanks and treats all other strings
of blanks as equivalent. The -i option removes case sensitivity.
21
Introduction to UNIX
8. vi Editor
Full screen editor available on almost all UNIX systems. vi is
different than most editors in that it has two modes:
• Command mode Keystrokes used as commands
• Insert mode Characters inserted into file
% vi filename
i, a — insert mode command
<ESC> — escape
22
Introduction to UNIX
Cursor Movement
Moving by characters h (or backspace) move left one character
j move down one line
k move up one line
l (or space) move right one character
23
Introduction to UNIX
Inserting Text
Inserting Text The following commands place you in insert mode:
i insert text before cursor
a append text after cursor
I insert text at beginning of line
A append text at end of line
o insert text in new line below present line
O insert text in new line above present line
Use Escape to return to command mode
Replacing Text r char substitute char for current character
R overwrite text (end with Esc)
cw change current word (end with Esc)
Setting Margins :set wrapmargin=x
Sets right margin to x, automatic word wrap
24
Introduction to UNIX
:v/string/command
execute command on lines not containing string
Substitution :x,ys/oldstring/newstring/flags
25
Introduction to UNIX
Miscellaneous Commands
Undoing Commands u undo last change
U undo recent changes made to current
line
Repeating Com- . repeat last command that made an
mands editing change
Reading in Files :r filename read filename into file
(insert below current line)
Shell Commands :!command execute shell command
:sh invoke subshell (use the exit com-
mand to return to vi)
:r !command read shell command output into file
(insert below current line)
Joining Lines J join next line to current line
Line Numbers Ctrl-g display current line number and
number of lines in file
26
Introduction to UNIX
9. File Protection
9.1. User and Permission Types
User types
u file owner (u is short for user)
g group members
o all other users
Permission types
r allows users to read or copy a file
w allows users to write to, modify, or copy a file; you
must have write access to a directory to delete its file,
regardless of an individual file’s protection
x allows users to execute a file (for directories, execute
permission allows users to use the directory name in
a pathname)
27
Introduction to UNIX
Examples
To add execute access to file program for its owner:
% chmod u+x myscript
28
Introduction to UNIX
29
Introduction to UNIX
Standard Error
(default: screen)
Redirection Operators
The C shell alllows users to easily redirect input and output:
% command < file redirects standard input from
file
% command > file redirects standard output to file
(if file already exists, it is over-
written)
% command >& file redirects both standard and error
output to file (if file already
exists, it is overwritten)
% (command > file) >& errfile
redirects standard output to file
and error output to errfile (if
file or errfile already exist,
they are overwritten)
30
Introduction to UNIX
Examples
% cat file1 file2 > file3
% cat > file
% cat
% ls -al > listing
% program < input.dat > results.dat
31
Introduction to UNIX
Pipes
Output from one command to be piped into another command
as follows:
% command1 | command2
Output from command1 is piped into input for command2.
Examples
% ls -al | more
% who | sort | lpr
% man -k network | more
32
Introduction to UNIX
10.2. History
The history mechanism keeps track of previous commands:
% set history=n
33
Introduction to UNIX
34
Introduction to UNIX
35
Introduction to UNIX
36
Introduction to UNIX
37
Introduction to UNIX
38
Introduction to UNIX
You will be prompted for a subject, after which the message can
be entered, followed by <CTRL-D>.
39
Introduction to UNIX
40
Introduction to UNIX
If you have no new or unread messages, the mail utility will not
be invoked and a message will be displayed.
Previously read messages are stored in the file "~/mbox". To
read them, use the following:
% Mail -f
41
Introduction to UNIX
42
Introduction to UNIX
smith@ccfsun.nrl.navy.mil
43