2. COURSE AIMS
• By the end of the course you should be able:
• To be able to explain why we use UNIX and what we use it for
• Navigate the file system
• Create, copy, rename, move and delete files and directories
• Use redirection and pipe facilities
• To be able to explain jobs and processes
• Customise your login environment
23 December 2016, Page 2
3. AGENDA
• A Very Brief History of UNIX
• Logging On
• Getting Started
• Working With Files
• Displaying File Contents
• Working With Directories
• The vi Editor
• Redirection and Pipes
• Recalling Commands
• The Superuser, File Permissions and Access Control
• Working With Processes
• Shell Environment
• Other Useful Stuff!
23 December 2016, Page 3
5. A VERY BRIEF HISTORY OF UNIX
The forerunner to UNIX, called Multics was first developed in the
1960’s.
AT & T pulled out of the consortium but Ken Thompson continued to
develop on the GE-645 Mainframe.
With help from Dennis Ritchie of C fame, they rewrote Space Travel
in assembly to run on a DEC PDP-7.
From there it was a small step for Ken to write a new Operating
System for the PDP-7.
In the 1970s the project was named Unics as a contrast to Multics;
the spelling was later changed to Unix.
23 December 2016, Page 5
6. A VERY BRIEF HISTORY OF UNIX
In 1973 UNIX was completely rewritten in C.
At this point AT & T made UNIX available to Universities and
commercial firms under licence.
In the 1980s AT & T released a commercial version of UNIX that
would later be know as System V
University of California, Berkeley, worked on their own version of
UNIX that would later become BSD UNIX.
23 December 2016, Page 6
7. A VERY BRIEF HISTORY OF UNIX
In 1991 Linus Torvolds wrote the first version of the Operating
System Linux.
The dot-com crash in the early 2000s led to a considerable
consolidation of UNIX operating systems. All the UNIX
variations available now are based either on System V, BSD or
Linux.
Read more at http://en.wikipedia.org/wiki/Unix
23 December 2016, Page 7
8. WHY USE UNIX?
UNIX is a mature group of operating systems with a proven track
record for performance, reliability, and security in a server
environment.
The thirty years of continual development has produced a group of
operating systems, and extremely powerful multiprocessor server
hardware tailor made to its needs, that are still at the forefront of
computer processing today.
UNIX was designed to be able to run multiple processes
simultaneously and allow multiple users to use the system at the
same time and it handles these tasks superbly.
23 December 2016, Page 8
9. THE OPERATING SYSTEM MODEL
All of the different flavours of UNIX have the same architectural
model:
23 December 2016, Page 9
The Kernel
Commands and
Utilities
The Shell
10. THE KERNEL
The Kernel is the core of the Operating System. It contains routines
and procedures that control all of the hardware that is attached to
the system. Some of these my be coded into the kernel directly,
some may be in the form of software drivers.
The kernel controls many aspects of the how the machine works. It
controls all access to the hardware, allocating resources to
processes that need them. It manages disk processing and
memory, swapping processes between the two as required. It also
controls what users can access the system and what they can do.
23 December 2016, Page 10
11. COMMANDS AND UTILITIES
The Kernel does not contain any commands. A user can not call
something in the Kernel directory. Nearly all of the commands and
utilities that exist are held as executable programs on the disk.
There are a wide range of commands and utilities available that
allow us to carry out all the actions that we need to. These include:
• Extensive commands for managing files and directories
• A variety of tools for searching and manipulating data
• Administrative tools for maintaining the system and working with user accounts
• Network tools
• Performance management tools
• ... And many others!
23 December 2016, Page 11
12. THE SHELL
The Shell is a special kind of program that provides the user an
environment to work in. The shell maybe a graphical interface using
X-Windows (like the KDE or Gnome, both used in Linux) or it can be
a command line interface such as the Korn Shell. In either case it
allows the users to communicate with the system.
There are many different shells available including C, Bourne, Bash
(Bourne Again Shell), korn, zsh.
The shell environment is where we run all our commands. It also
provides us with an environment to string commands together into a
shell script.
23 December 2016, Page 12
13. FILE AND DIRECTORIES
UNIX filenames are case sensitive. This means that the following
filename examples are all different:
stuff STUFF Stuff StuFF
Since all commands and utilities are also held as files on the disk,
those command calls are also case sensitive:
ls –f is not the same as ls -F.
23 December 2016, Page 13
14. FILE AND DIRECTORIES
You can also use – (dash) and _ (underscore) in file names. Other
characters can be used (such as $ or &) but they have special
meanings to the shell and so are used infrequently.
While it is possible to create filenames with spaces in, this should be
avoided. While in a graphical environment this would be handled by
the gui, on the command line you have to remember to quote the
filenames. IT is much simpler to avoid the user of spaces
altogether..
23 December 2016, Page 14
15. FILE AND DIRECTORIES
The special character . (period) can also be used but has a special
meaning when used as the start of a file name: it denotes the file as
hidden.
By convention filenames are lowercase and with a few exceptions
this is true of all UNIX flavours
Filenames must be unique within each directory.
23 December 2016, Page 15
16. DIRECTORY STRUCTURE
The following diagram demonstrates the hierarchical structure of a
UNIX filesystem:
(root)
23 December 2016,, Page 16
/
usrdev
local
varetc homesbin bin
binsbin spoo
l
adm
ghzAbc def
17. DIRECTORY STRUCTURE
root is the topmost level of the directory structure and is referenced
by /.
/dev Contains device files which point to hardware
devices connected to the system.
/sbin Contains system administration utilities.
/bin Normally points to /usr/bin and contains
standard commands.
/usr/bin Contains System V command files.
/var Contains logs and other transient files (such as
printer spools and emails).
/etc Contains system configuration files, including
user definition files.
/home Users home directories.
23 December 2016, Page 17
18. WHEN IS A FILE NOT A FILE?
UNIX differs from most operating systems in that it is file orientated.
As far as UNIX is concerned, disk drives, terminals, modems,
network adaptors etc are all just files.
Therefore to understand UNIX you need to understand what files
are. A file is nothing more than a stream of bytes. There are no
special file structures and only a few special types (for keeping track
of disks and a few other purposes).
This means you can access hardware on the UNIX system just as if
you were accessing a file.
23 December 2016, Page 18
19. TRANSFERRING FILES
Be careful when moving UNIX files to other systems.
UNIX uses a single linefeed character between lines of text where
Windows uses carriage-return/linefeed combination.
A common problem we experience is having ^M characters in shell
scripts on UNIX. This comes from having the file in windows format
and then transferring it to UNIX. (Windows and UNIX files use
different sequences of non-printable characters as line terminators).
It’s best to convert the file before you transfer it to UNIX using
something like UltraEdit. You can convert it on the UNIX box but the
tools are not always available (dos2unix).
23 December 2016, Page 19
21. CONNECTING TO A UNIX MACHINE
Telnet is available on most UNIX boxes and allows us to connect to
that machine.
However it is an insecure protocol (passwords and commands are
sent via plain text across the network) so you should use
alternatives if they are available.
Whether you use Telnet or Secure Shell you still need to know the
name of the UNIX box you want to connect to. You can use the
physical name of the box (lupus-1 for instance) or you can use the
Call By Service name.
23 December 2016, Page 21
22. CONNECTING TO A UNIX MACHINE
The Call By Service name is something that is implemented by
Networks via the DNS. It allows users to connect to the same name
regardless of the name and address of the underlying box. If there
is a hardware failure and the standby machine is brought into action,
you don’t have to make any changes to be able to connect to it, you
still just call the Call by Service name.
PuTTY is the company’s choice of Telnet and ssh client. It is free
and is available for install on all machines. So, lets fire it up...
23 December 2016, Page 22
23. PUTTY CONFIGURATION
There are many settings that you can change in PuTTY, the window
title to the background and text colours. You can also select the
connection method that you connect with (telnet or ssh). Once you
have set up your session the way you like it you can save these
details so you don’t have to change them each time you log on.
One thing you will want to change is the backspace key. By default
PuTTY will issue a ^? each time you press backspace. You will
want to change this so that a real backspace character is issued
instead.
23 December 2016,, Page 23
24. 23 December 2016, Page 24
Setting this will mean that a
real backspace character will
be issued via PuTTY.
Click on the Keyboard menu on the right and then select Control-H
under The Backspace Key heading.
25. 23 December 2016, Page 25
Type the call by service name
for the UNIX box you want to
connect to here.
Now the backspace key has been changed you can log onto a UNIX
box.
Then hit return or click Open
26. 23 December 2016, Page 26
And now you are logged on! The administrators can change the
messages that are issued when you log on so you might not always get
what you see above.
27. To log out you type exit or hit <ctrl> + <d>.
If you are running puTTY then the window will close when you exit
otherwise you will get a message saying your connection has been
terminated.
23 December 2016, Page 27
29. COMMAND SYNTAX
The following syntax is common to the majority of UNIX and Linux
commands:
<command> [ <options> ] [ <arguments> ]
Options modify what the command will do. Arguments are what the
command will act against.
Different commands will take different options, some of which
maybe upper or lower case, depending on the command.
Normally when a command completes successfully there will be no
response back to the screen, you will just get your command prompt
back.
23 December 2016, Page 29
30. COMMAND SYNTAX
The command must be separated from the argument by spaces. If
multiple arguments are used each must be separated by spaces.
Some commands will use a default argument if none is specified.
ls /tmp /etc mysub
Lists the contents of the /tmp, /etc and mysub directories.
cat file1 file2
Concatenates the text in the files file1 and file2 and displays it.
pwd
Shows your current working directory
23 December 2016, Page 30
31. COMMAND SYNTAX
Options, often also called flags, modify how the command will
operate. Often they modify the output of the command but they can
affect any aspect of what the command is doing. When specified,
any options must be separated from the command by one or more
spaces.
Options are normally a hyphen followed by a single character.
Where multiple options are specified they can be grouped together
with a single hyphen. If an option includes some additional data
then it should immediately follow the option.
Some Linux commands give you the choice of using option words.
When an option word is used it is preceded with a double hyphen (-
-)
23 December 2016, Page 31
32. COMMAND SYNTAX
ls –a
Lists all files both hidden and non-hidden files in the current working
directory.
ps –e –f ps –ef
Both commands present a full listing of all running processes on the
system.
ls –aF /home /tmp
Lists all files in the /home and /tmp. Each entry is displayed with a
flag to indicate it’s type.
who –-users (Linux only)
Adds a column showing idle time to the standard who command.
The standard UNIX equivalent would be who –u and this also
works for Linux.
23 December 2016, Page 32
33. GETTING HELP
If you ever get stuck with a command in UNIX there is an online
manual that you can use. Not all commands are in there but most
common ones are.
To look up help on the command cat, you would run:
man cat
Of course, if the command has no manual page then you could just
try running it with a –h flag, or some random flags to force it to throw
an error and issue a help page.
23 December 2016, Page 33
34. EXERCISE 1
1. List all the files in your home directory. Are there any hidden files?
2. How do you find help on the command ps?
3. How do you read the man pages for the command man?
23 December 2016, Page 34
35. EXERCISE 1 ANSWERS
1. ls –lA
Hidden files are those starting with a period, for example .login
2. man ps
3. man man
23 December 2016, Page 35
37. LISTING FILES
ls /tmp /etc mysub
Lists the contents of the /tmp, /etc and mysub directories.
ls –l /tmp
Shows you a long listing for the /tmp directory. This includes the
following information:
-rwxr-xr-- 1 m6345 appsup 29 Jan 19 11:50
myfile
Permissions User Last modified
File type Links Group File Size
Filename
Your output may vary on different versions of UNIX.
23 December 2016, Page 37
38. COPYING FILES
Use the cp command to copy files:
cp [-i] <file-list> <destination>
The destination can be either a file or a directory. If a directory is
specified then multiple files can be listed in the <file-list>.
If a destination file is specified then it will be overwritten if it exists,
unless you specify the –i flag. If specified this causes cp to prompt
to overwrite an existing file.
23 December 2016, Page 38
39. COPYING FILES
cp file1 file2
Copies file1 to file2 in the current working directory
cp file1 /app/ice1/data
/app/ice1/data is a directory and this copies the file file1 into
it, leaving the name the same.
cp file1 file2 file3 /tmp
Copies file1, file2 and file3 to /tmp. It will prompt to
overwrite any files that already exist in /tmp.
cp /tmp/file1 /app/ice1/data/newfile
Copies /tmp/file1 to /app/ice1/data and rename it as
newfile.
23 December 2016, Page 39
40. MOVING AND RENAMING FILES
Use the mv command to move and rename files:
mv [-i] <file-list> <destination>
The destination can be either a file or a directory. If a directory is
specified then multiple files can be listed in the <file-list>.
When a directory is specified then the listed files will be moved to
that directory.
If the destination is a file then only a single file can be specified in
the <file-list> and the file will be renamed.
As with cp files will be overwritten unless the –i flag is specified.
23 December 2016, Page 40
41. MOVING AND RENAMING FILES
mv file1 file2
Renames file1 to file2 in the current directory.
mv file1 /tmp
Moves file1 to /tmp.
mv –i file1 file2 file3 /app/ice1/data
Moves file1, file2 and file3 to the directory
/app/ice1/data.
mv subdir /tmp
Moves the directory subdir to /tmp. Directories can also be
renamed by specifying a new name.
23 December 2016, Page 41
42. COPYING AND MOVING FILES
When you copy a file you create a new version of it. As such you
will be the owner of the new file and it will have permissions that
relate to your setup. If you want to preserve the ownership and
permissions of the file when you copy it, include the –p flag:
cp –p /tmp/some_file some_unchanged_file
When you move a file it will keep the same ownership and
permissions that it has when you start. However if you do not have
permission to delete the file then the move will issue an error. The
file will be copied to the target location, it just won’t be deleted from
the source location.
23 December 2016, Page 42
43. DELETING FILES
Use the rm command to delete files:
rm [-i] <file-list>
All listed files are deleted. If the –I option is used then the user is
prompted to confirm the deletion of each file before it occurs. Each
file is deleted as it is confirmed. If you change your mind part way
through a long list of files the earlier files will already have been
deleted!
DELETED FILES CAN NOT BE RECOVERED UNLESS DONE SO
FROM A BACKUP. THERE IS NO RECYCLE BIN!
23 December 2016, Page 43
44. DELETING FILES
rm /home/spike/report_data
Deletes the file report_data from the directory /home/spike.
rm –i file1 file2 file3
The files file1, file2 and file3 will be deleted from the current
directory. The user will be asked to confirm each deletion.
23 December 2016, Page 44
46. DISPLAYING FILE CONTENTS
There are various commands that you can use to view the contents
of files. The following slides deal with the commonly used ones.
cat file1 file2
Concatenates the text in the files file1 and file2. The entire
contents of the files are output to the screen. If there are more rows
in the file than there are lines on the screen then the excess output
will scroll off the top of the screen.
head [–n] file1
Displays the top n lines of file1 to the screen. If n is not specified
then the top 10 rows are shown.
23 December 2016, Page 46
47. DISPLAYING FILE CONTENTS
tail [–n –f] file1
The opposite of head, tail shows you the bottom n rows of a file.
Again if n is not specified then the bottom 10 rows will be shown. If
you wish to see lines being appended to the bottom of the file (such
as a log file) then the –f flag can be used.
tail –f /logs/script.log
Shows you lines as they are appended to the file
/logs/script.log. The command will not return until you hit
<ctrl> + <c>.
23 December 2016, Page 47
48. DISPLAYING FILE CONTENTS
more file1
Displays file1 a screen at a time. To advance by a screen press
<space>. To advance by a single line press <enter>.
less file1
less is only available on Linux. It is similar to the more command
but it allows you to move backwards through the file, as well as
forwards.
23 December 2016, Page 48
49. OTHER FILE COMMANDS
touch file1
Touch will create file1 in the current directory. If you specify a full
path (such as /tmp/file1) the file will be created there. The file
will be empty. If the file already exists then the last modified date is
updated as long as you have access permissions to that file.
wc file1
wc (word count) counts all the characters, words and lines in a file.
This can be useful to see if a file is growing without having to tail
it. If you only want to count rows use the –l flag. Other flags are
available, check the man pages to find out what.
23 December 2016, Page 49
50. EXERCISE 2
1. List the contents of the /etc directory. Is there a file called
hosts? When was it last modified?
2. How many lines are there in the /etc/hosts file?
3. Can you look at the first 20 lines of the /etc/hosts file?
4. Copy the file /etc/group to your home directory.
5. Rename the group file in your home directory to mygroup.
6. Find out how to count the number of characters in mygroup using
wc.
7. View the contents of the new mygroup file.
23 December 2016, Page 50
51. EXERCISE 2 ANSWERS
1. ls –l /etc
ls –l hosts
2. wc –l /etc/hosts
3. head -20 /etc/hosts
4. cp /etc/group /home/<username>
5. mv group mygroup
6. man wc
wc –m mygroup
7. cat mygroup
or
more mygroup
23 December 2016, Page 51
53. LISTING DIRECTORIES
Directory contents can be listed using the same ls command that is
used for files:
ls [<options>] [<filename-list>]
When a directory is specified its contents will be listed. If no
filename is specified then the current directory is listed. If only files
are specified then only details relating to those files will be shown.
23 December 2016, Page 53
54. LISTING DIRECTORIES
There are a number of flags that modify the output. They are (in no
particular order):
-F flags different file types
-a shows all files both hidden and non hidden
-l a long listing
-t sort files in modification time order (newest first)
-r sort files in reverse order (by time if –t is used or
alphabetical if not)
-R recursively list subdirectories encountered.
-d list only directory attributes
-s list size in blocks for all entries
23 December 2016, Page 54
55. LISTING DIRECTORIES
To display a long listing use the –l flag:
-rwxr-xr-- 1 m6345 appsup 29 Jan 19 11:50
myfile
Permissions User Last modified
File type Links Group File Size
Filename
Depending on your version of UNIX this output may be slightly
different (normally around the date field).
23 December 2016, Page 55
56. LISTING DIRECTORIES
The file type will be one of the following:
d a directory
b or c a block or character device
l a symbolic link or shortcut
p a named pipe or FIFO file
s a socket
- None of the above, an ordinary plain file
23 December 2016, Page 56
57. SPECIAL DIRECTORY FILES
In each directory there are two special files:
. - Refers to the current directory
.. - Refers to the parent directory
cp /tmp/file1 .
Copies the file /tmp/file1 into the current directory.
cd ..
Moves the current working directory up one level.
23 December 2016, Page 57
58. NAVIGATING DIRECTORIES
Use the cd command to navigate between directories:
cd /home/m6345
Changes the current working directory to /home/m6345.
cd /tmp
Changes the current working directory to /tmp.
cd –
Returns you to the previous directory you were in. In the case of the
previous examples, /home/m6345.
23 December 2016, Page 58
59. NAVIGATING DIRECTORIES
cd ..
Moves you one level up the directory tree. If you were in
/home/m6345, you would end up in /home.
This can be combined with further changes. For instance:
cd ../n1098
Would have moved you from /home/m6345 to /home/n1098.
cd on its own will return you to your home directory.
pwd
Shows you the current working directory.
23 December 2016, Page 59
60. CREATING DIRECTORIES
Use the mkdir command to create directories:
mkdir [-p] <directory-list>
mkdir scripts work
Creates directories called scripts and work in the current
directory.
mkdir /home/m6345/temp
Creates a directory called temp in /home/m6345. If /home/m6345
doesn’t exist an error will be reported.
mkdir –p /home/m9999/work_files
Creates a directory called work_files in /home/m9999. If either
/home or m9999 do not exist they will also be created.
23 December 2016, Page 60
61. COPYING DIRECTORIES
Use the cp command to copy directories. The –r flag must be
included:
cp –r <source-directory> <destination-directory>
The source directory and its contents are copied to the destination
directory. The destination directory should exist or an error will be
raised.
cp –r mysub /home/m6345
Copies mysub and all its contents to /home/m6345. mysub
becomes a subdirectory of /home/m6345.
23 December 2016, Page 61
62. MOVING AND RENAMING DIRECTORIES
Use the mv command for moving and renaming directories:
mv <source-directory> <destination-directory>
The source directory will be moved and become a sub-directory if
the destination directory already exists.
The source directory is renamed if the destination directory does not
exist.
mv scripts tmp
Moves scripts into tmp if tmp already exists. If tmp does not
exist then scripts will be renamed to tmp.
23 December 2016, Page 62
63. DELETING DIRECTORIES
Use the rmdir command to remove:
rmdir <directory-list>
rmdir will only remove empty directories. If the directory contains
any files the deletion will fail.
rmdir work_files test
Removes the directories work_files and test if they are empty.
If a directory has only the special files . and .. within it, it will be
removed by rmdir.
23 December 2016, Page 63
64. DELETING DIRECTORIES
The rm command can also be used to remove diretories:
rm –r work_files
No warning is issued unless the –i flag is used.
Use extreme caution when using this command. If you use an
absolute pathname then you could remove an entire filesystem.
As with all deletions: THERE IS NO UNDELETE!
23 December 2016, Page 64
65. FILENAME WILDCARDS
Most commands that deal with files will accept multiple filenames for
processing. If several files with similar names are to be referenced
this can be done in a single argument using wildcards.
• * represents zero or more matches of any character.
• ? represents a single occurrence of any character.
• [...] represent a single occurrence of any of the characters listed between the
brackets.
The exception to this is if you want to see hidden files. If either of
the first two wildcard characters are used the . must be specified.
23 December 2016, Page 65
66. FILENAME WILDCARDS
ls –l s*
Lists all files beginning with s in the current directory.
mv .???? /tmp
Moves all hidden files with 5 character names (including the leading
.) to the directory /tmp.
cp * /home/m6345
Copies all files in the current directory (except hidden files) to the
directory /home/m6345.
rm *[0-4]
Deletes all files (except hidden files) that end in the numbers 0, 1, 2,
3 or 4.
23 December 2016, Page 66
67. FILENAME WILDCARDS
To handle those times when you want everything that does NOT
match you can use an exclamation mark in the [..] wildcard
notation. It has to be specified as the first character in the class:
ls –l [!A-Z]*
Lists all non-hidden files that DO NOT start with the an uppercase
character.
23 December 2016, Page 67
68. EXERCISE 3
1. Verify you are in your home directory.
2. Create a new directory called hidden_files in your home
directory.
3. Copy one of the hidden files in your home directory to the new
directory. Rename the file so that it is not hidden.
4. Verify the file has been copied to the new directory.
5. Copy the file called issue from /etc to your directory
hidden_files. Do this without changing directory.
6. Rename the issue file in your directory to myissue (not the one
in /etc!)
7. Copy the directory hidden_files and it’s contents to a new
directory called myfiles.
8. Delete the directory hidden_files and its contents.
23 December 2016, Page 68
72. THE VI EDITOR
The vi editor is often the editor of choice when on a UNIX system.
There are others like emacs and ed but vi is the most popular.
vi allows you to edit a file from the command line. While some of
the commands appear to be quite random you need to remember
that this program was written in the day when the only keys on the
keyboard were a-z, 0-9 and a few other punctuation marks!
In Linux vi has been replaced by vim a more GUI friendly version
that makes use of today’s more advanced terminal emulators.
However we will only cover vi here.
23 December 2016, Page 72
73. THE VI EDITOR
vi operates in three distinct modes:
• Command Mode – Allows you to move around the screen and to issue editing
commands such as copy and paste.
• ex Mode – also known as last line mode, is entered from the Command Mode. It’s
main purpose is to run commands. For example text search and replace and setting
vi options.
• Input Mode – Allows you to input text. Only entering and deleting text (using
<backspace>) can be carried out in this mode. The <Esc> key terminates Input
Mode and switches back to Command Mode.
23 December 2016, Page 73
77. 23 December 2016, Page 77
Save the file by entering Ex Mode and typing :wq!
78. VI COMMANDS
vi provides a vast array of commands. The more
common are shown on the vi crib sheet that you’ve been
given.
Some vi defaults can be changed by editing your .exrc
file. However we’ll save further discussion for another
day.
23 December 2016, Page 78
79. VI ALTERNATIVES
As mentioned earlier there are alternatives to vi.
One such alternative is to use a windows based editor that
allows you to open files from a UNIX box. UltraEdit is a popular
choice and is available under licence to . It uses FTP to open
and save files on the UNIX box while allowing you to work with a
windows editor.
You will need to get UltraEdit installed on your PC as it is not part
of the standard build. You will also need an account on the UNIX
box where the files you wish to edit reside.
When saving a file back to the UNIX box make sure you have
converted the file format to UNIX (File -> Conversions -> Dos to
UNIX). This will prevent ^M’s appearing in the script.
23 December 2016, Page 79
81. INPUT/OUTPUT REDIRECTION
One of the major leaps in UNIX was the abstraction of
communication for processes. In UNIX a program does not need to
know how to write to the screen or a file, or how to get input from the
keyboard. All that communication is handled via streams that are
provided by the Kernel.
Each process has three default streams that it can interact with.
These streams, also called channels, provide input to the program
and allows two channels of output, one for errors and one for
everything else.
The three channels are:
• Standard in, also known as STDIN is the input channel, stream 0
• Standard out, also known as STDOUT is the output channel, stream 1
• Standard error, also known as STDERR is the error channel, stream 2
23 December 2016, Page 81
82. INPUT/OUTPUT REDIRECTION
Each of the three streams have default locations:
STDIN is usually input from the keyboard
STDOUT is usually output to the screen
STDERR is usually output to the screen
However, it is possible to redirect these streams so that output
comes, or goes to a different location.
Input could be taken from a file or another command while output
could be directed to a file, printer or some other device attached to
the system.
23 December 2016, Page 82
83. OUTPUT REDIRECTION
To redirect output you use the > character.
cat file1 > new_file
This cats file1 and redirects STDOUT to a file called new_file.
echo “This is a test” > test_file
Sends the string This is a test to a file called test_file.
grep ORAERR log_file > /dev/null
Grep for ORAERR and send the output to /dev/null.
run_ftp.ksh > run_ftp.log
Redirects all STDOUT from the script run_ftp.ksh to a file called
run_ftp.log.
23 December 2016, Page 83
84. ERROR REDIRECTION
As well as STDOUT a program may also generate error messages.
These are redirected by preceding the redirection character > with
the channel number for errors, 2.
cat file1 file2 2> errors1
Cats file1 and file2. If either of the files do not exist then the
error is sent to a file called errors1.
touch file1 2> /dev/null
Creates file1 and direct any errors to /dev/null.
cp file1 /tmp/file2 2> errors2
Copies file1 to /tmp/file2 and direct errors to a file called
errors2.
23 December 2016, Page 84
85. REDIRECTING BOTH OUTPUT AND ERRORS
You can redirect both STDOUT and STDERR at the same time.
cat file1 file2 > log 2> errors2
Cats file1 and file2 to a file called log and directs any errors to
errors2.
If you want to send STDOUT and STDERR to the same file you can
use the following:
cat file1 file2 > log 2>&1
Cats file1 and file2 to a file called log. Any errors are
redirected to stream 1, which is redirected to log.
23 December 2016, Page 85
86. APPENDING OUTPUT
When output is redirected to a file, if that file does not exist, it is
created. When using the > character any existing file will be
overwritten. However, you may want to append the output to an
existing file. To do that use the >> construct. Both STDOUT and
STDERR can be redirected in this way.
cat file1 >> log
Appends the contents of file1 to log.
cat file1 file2 >> log 2>> error1
Appends the contents of file1 and file2 to log and send any
errors to error1.
If sending STDOUT and STDERR to the same use:
cat file1 file2 >> log 2>>&1
23 December 2016, Page 86
87. INPUT REDIRECTION
Most processes that run will have predefined input paths set up.
The UNIX mail program for instance normally takes input from the
keyboard for the body of the email. However you can redirect that
input from a file by using the < character.
mail –sMyFile < email_text
Calls mail and uses the contents of the file email_text as the
body of the email, rather than taking input form the keyboard.
wc –l < file1
Redirects input to wc from file1. This is almost the same as
running
wc –l file1. Can you work out what the difference is?
23 December 2016, Page 87
88. PIPES
It is possible to pass the STDOUT of one command into the STDIN of
another by using pipes. The | character is used for this.
ps –ef | grep oracle
Lists all the processes running on the system and extract all those
with the work oracle in them.
cat file1 | tr 'a-z' 'A-Z‘ > newfile
Translates all lowercase characters in file1 to uppercase and
outputs the result to newfile.
Making use of input redirection this could be written as:
tr ‘a-z’ ‘A-Z’ < file1 > newfile
23 December 2016, Page 88
89. THE TEE COMMAND
The tee command can be used to redirect STDOUT to multiple
destinations. It is so called as it acts as a T-junction in the pipeline.
who | tee users | wc –l
Runs the who command. The listing is sent to a file called users
and also passed to the wc command. The number of users logged
on is output to STDOUT.
If the file you tee to already exists it will be overwritten. If you
include the
–a flag then the file will be appended to.
echo “This is a message” | tee -a log
Displays the message on the STDOUT and also sends it to a file
called log.
23 December 2016, Page 89
90. EXERCISE 4
1. Check you are in your home directory.
2. List the contents of the /etc directory and redirect the output to a
file called etc_listing. Display the contents of etc_listing.
3. Using vi, edit the etc_listing file. Add a row to the file
containing your name. Display the contents once you have exited
vi.
4. Append the date to the etc_listing file, without using vi.
Display the file to ensure it has worked.
5. Display the contents of the files mygroup and mypasswd in your
home directory to the screen. Redirect any errors to a new file
called errors1.
6. Repeat the step above but now redirect ALL output to a file called
output1.
7. What’s the minor difference between:
more output and cat output | more
23 December 2016, Page 90
91. EXERCISE 4 ANSWERS
1. pwd
2. ls –l /etc > etc_listing
more etc_listing
3. vi etc_listing
more etc_listing
4. date >> etc_listing
tail etc_listing
5. cat mygroup mypasswed 2> errors1
6. cat mygroup mypasswed > output1 2>&1
7. Involving the pipe means that you run extra processes. If
you are ever piping cat into something consider if you can use
input redirection instead!
23 December 2016, Page 91
93. RECALLING COMMANDS
You can recall previously issued commands when you are on the
command line.
To see previously issued commands run:
history
The last 16 commands will be displayed along with a number. This
is their place in the command history list. If you want to see more
then run:
history -50 | more
Since 50 commands will probably go off the top of the screen you’ll
want to pipe to more to see them a screen at a time.
23 December 2016, Page 93
94. RERUNNING COMMANDS
Once you know what command you want to rerun and it’s number in
the history list, you can easily rerun it.
In the Korn shell you can issue r 245 to rerun the command at
position 245 in the history file. To run the last command you can just
issue r on its own.
In the Bash shell you can issue !245.
!! Will run the last command issued.
23 December 2016, Page 94
95. EDITING THE HISTORY FILE
In the Korn shell you can edit the history file and commands that you
recall to the command line using the vi editor. This functionality is
not enabled by default and so you need to issue:
set –o vi
You can then use the same editing commands that you would in vi
on the command line. k will move up one line while j will move
down one line.
If you want to search for a particular command, hit <Esc>, then /
and then type part of the command you are trying to find. If the first
command returned is not the one, hit n to search for the next.
23 December 2016, Page 95
96. EDITING THE HISTORY FILE
Under the Bash shell running on Linux it is possible to use the
cursor keys to navigate the command history.
23 December 2016, Page 96
98. THE SUPERUSER - ROOT
Most of the user accounts on a UNIX system are not special. Those
accounts will be limited in what they can do and access by the group
permissions that they have.
The exception to this is the superuser account, by convention called
root.
This account has unlimited privileges on the UNIX machine. It can
look at any file, kill any process and modify any permissions. It can
also shutdown the system, mount and unmount filesystems and
carry out a variety of administrative tasks.
23 December 2016, Page 98
99. THE SUPERUSER - ROOT
The superuser account has the User ID number of 0 and Group ID
number of 0.
Some programs will bypass checks they normally make if you are
running with a User ID number of 0. For instance as root you can
change a users password without knowing it.
You are unlikely to have access to root unless you are working as
a UNIX Administrator. However if you ever reach those dizzy
heights remember, just because you can, doesn’t mean you should!
23 December 2016, Page 99
100. FILE LISTING
When a user is created it is a assigned a unique ID (UID) and is
associated with a group and given a Group ID (GID).
Every file and directory on the system have a number of attributes
associated with them. These hold the owner and group information
along with the access permissions for the file.
Running ls –l displays these attributes:
-rwxr-xr-- 1 m6345 appsup 29 Jan 19 11:50
myfile
Permissions User
File type Links Group
23 December 2016, Page 100
101. IMPORTANT FILES
There are two system files that contain the user and group
information.
/etc/passwd contains one row for each user on the system. It
contains their user ID, their password, UID, GID, user information
(that could be a name or department), their home directory and their
default shell.
/etc/group contains one row for each group on the system. It
contains the group name, GID and a list of user IDs associated with
that group.
Commands such as login and ls use these files to translate the
UID and GID into their proper names. Commands that lookup
users, such as who, logname and id also display information from
these files.
23 December 2016, Page 101
102. FILE PERMISSIONS
The permission attributes in the ls –l listing are divided into three
sets of permissions: one for the owner, one for the group and one
for everyone else (a group called others). Each set is made up of
three characters that indicate the allowed permissions:
If a permission is not permitted then a hyphen is displayed.
23 December 2016, Page 102
r Indicates Read permissions are permitted
w Indicates Write permissions are permitted
x Indicates Execute permission are permitted
103. FILE PERMISSIONS
So, the permission:
rwxr-xr--
allows read, write and execute access for the owner, read and
execute access for the group and read only for others.
23 December 2016, Page 103
104. FILE PERMISSIONS
For ordinary files:
• Read permission allows the user to read the contents of the file.
• Write permission allows the user to modify the contents of the file either by using an
editor or by redirecting output to it.
• Execute permission allows a program file or shell script to be executed from the
command line..
23 December 2016, Page 104
105. FILE PERMISSIONS
For directories:
• A directory listing can be made with only read permission. However to execute a long
ls –l listing execute permission is required.
• To create or delete files in a given directory a user must have read and execute
permission.
• To cd to a directory the user must have execute permission on that directory.
23 December 2016, Page 105
106. FILE PERMISSIONS
• A user does not need read or write permission on a file to be able to rename or delete
it. A warning before deletion will be issued by this can be confirmed.
• A user does not need read or write permission to move a file. However read
permission is required if moving to another filesystem.
• These actions can be carried out irrespective of the owner of the file concerned.
• A user can rename and delete files in any directory that has write and execute
permissions, regardless of the ownership of those files.
23 December 2016, Page 106
107. FILE PERMISSIONS
• Given the previous slides you will want to ensure that people can not access
the files in your directory detrimentally.
• To protect your home directory (stopping users renaming and deleting your files
or from copying files into or moving files out of your home directory)
• you can change the permissions on your home directory to restrict access. The
following permissions are normally used and allow read access to your files:
rwxr-xr-x
• To prevent read access a more restrictive set of permissions could be used:
• rwx------
23 December 2016, Page 107
108. CHANGING PERMISSIONS
Use the chmod command to change the permissions of a file:
chmod <permissions> <file-list>
You can specify the permissions to change to using either symbolic
or octal notation.
The permissions of a file can only be changed by the owner of that
file or by the superuser.
23 December 2016, Page 108
109. CHANGING PERMISSIONS – SYMBOLIC NOTATION
Symbolic notation has three elements:
[<who>]<operator><permissions>
Where:
• <who> is any combination of the letters u (owner), g (group) and o (others). The letter
a represents the combination ugo and this is assumed if <who> is omitted.
• <operator> is either + (add), - (remove) or = (set).
• <permissions> is any combination of the letters r, w and x.
23 December 2016, Page 109
110. CHANGING PERMISSIONS – SYMBOLIC NOTATION
chmod u+x file1
Adds execute permissions for the owner.
chmod +r file1
Adds read permissions for everyone
chmod o-rwx file1
Removes read, write and execute permissions for others.
chmod u=rwx,g=rx,o=r file1
Sets read, write and execute permissions for the owner, read and
execute for the group and read for others:
23 December 2016, Page 110
111. CHANGING PERMISSIONS – OCTAL NOTATION
You can also use octal notation. Each type of permission has a
decimal value and these are combined to give an overall
permission.
The decimal values are:
r (read) = 4
w (write) = 2
x (execute) = 1
- (no permission)= 0
Permissions can be expressed as three digits, once each for the
owner, the group and others by adding together the assigned
values.
23 December 2016, Page 111
112. CHANGING PERMISSIONS – OCTAL NOTATION
These are the most commonly used permissions for files:
chmod 644 myfile
Sets read and write permissions for the owner and read only
permissions for the group and others (rw-r--r--).
chmod 755 myfile
Sets read, write and execute permissions for the owner and read
and execute permissions for both the group and others (rwxr-xr-
x).
chmod 660 myfile
Sets read and write permissions for the owner and group and no
permissions for others (rw-rw----).
23 December 2016, Page 112
113. OTHER PERMISSIONS
There are three other permissions settings that you should be aware
of although we will not discuss them further on this course.
Set User ID (SUID) and Set Group ID (SGID) apply to executable
programs. They can be recognised in an ls –l listing by an s in
the permissions. When you run a program with SUID or SGID set,
your user accounts assumes the user ID or group of the program.
For instance, the program passwd that is used to change your
password has to write to the /etc/passwd file. However since that
file is owned by root only root can write to it. When you run
passwd you run it with root permissions (due to the SUID being set)
and can therefore write your new password to /etc/passwd.
23 December 2016, Page 113
114. OTHER PERMISSIONS
SGID can also be set on a directory. This means any files created
in that directory are owned by the same group that owns the
directory.
The Sticky Bit applies only to directories. This can be recognised in
an
ls –l listing by a t in the permissions. This prevents users from
deleting any files that don’t belong to them in a shared directory
(such as /tmp).
23 December 2016, Page 114
115. CHANGING RELATED ATTRIBUTES
As well as the access permissions you can also change the owner
and group of a file.
To change the owner issue:
chown <new_user> <files>
To change the group issue:
chgrp <new_user> <files>
On some systems only the superuser can change these
permissions. Also be aware that you won’t be able to write a
program, set the permissions to executable, set the SUID and then
pass ownership of the file to root to get round system security!
23 December 2016, Page 115
116. LINKING FILES
A single file can be accessed from multiple locations with the use of
links. You might do this for instance to provide the same profile to a
number of different users.
In many of the standard Perl binary locations link back to one binary
(check out /bin/perl for instance).
The command to create links is:
ln [-s] <source-file> < link-destination>
Two types of link can be made, Hard links and Symbolic links.
Symbolic links are created by including the –s flag in the command
call.
23 December 2016, Page 116
117. HARD LINKS
Hard Links can only be created if the source file exists. ls –l
shows the number of hard links in the listing.
Use the rm command to remove hard links. A file remains
accessible from other linked locations until all links are removed.
A hard link does not identify the source file. However changing the
ownership of permissions of either the original or linked files will
result in all files being updated.
Hard links can only point to files with the same filesystem and can
also point to directories.
23 December 2016, Page 117
118. HARD LINKS
ln /home/m6345/my_file /tmp/spikes_file
Creates a hard link for the file /home/m6345/myfile in /tmp
called spikes_file.
If /home/m6345/myfile is deleted the file in /tmp will persist until
it is deleted as well.
23 December 2016, Page 118
119. SYMBOLIC LINKS
A Symbolic Link, created by including the –s flag, is basically a
shortcut to the file. An ls –l list shows the file type as l. It will
also show you where the symbolic links points to.
Read, write and execute always apply to symbolic links. Access to
the source file is controlled by permissions on the file.
Use the rm command to remove symbolic links. The link remains
even if the source file is deleted. However you can’t create the link
if the source file does not exist.
Symbolic links can point to files in different filesystems and can also
be made for directories.
23 December 2016, Page 119
120. SYMBOLIC LINKS
ln –s /home/m6345/file1 /tmp/spikes_file
Allows /home/m6345/file1 to be accessed from
/tmp/spikes_file.
23 December 2016, Page 120
121. LINKS
Symbolic links are generally easier to manage (since you can
always see where they lead) and provide most users with the
functionality that they need.
There are Hard Links within most UNIX systems. One common use
is to allow the same program to be called with different names.
Run this command on minepw01 and you will see the two programs
have the same size and permissions. Depending on what you call,
a directory or a FIFO will be created:
ls -l /bin/mkdir /bin/mkfifo
-r-xr-xr-x 2 bin bin 20480 Nov 14 2000 /bin/mkdir
-r-xr-xr-x 2 bin bin 20480 Nov 14 2000
/bin/mkfifo
23 December 2016, Page 121
122. EXERCISE 5
1. Change the permissions on the file mygroup in your home
directory to give read permission only to the owner and no other
permissions to anyone else.
What is the result of the following actions:
Use cp to copy the file.
Try and edit the file with vi. Can you open it? What about
making changes?
Can you append STDOUT to the file? (Try and echo something
and use >> to redirect it to the file
Use rm to delete the file.
23 December 2016, Page 122
123. EXERCISE 5 (CONTINUED)
2. Change the permissions on your directory called my_files
to
r--r--r--.
What happens when you run the following commands?
ls my_files
cd my_files
cp my_files/myissue ~
rm my_files/login
23 December 2016, Page 123
124. EXERCISE 5 (CONTINUED)
3. Create a new file called show_date.ksh with the following
text in it:
echo “The date is : c”
date
Add execute permissions to the file and then run the script using:
./.show_date.ksh
4. Create a hard link to show_date.ksh called hard_date.
Create a symbolic link to show_date.ksh called soft_link.
Can you execute hard_date and soft_date? Delete
show_date.ksh. Can you still run hard_date and
soft_date?
23 December 2016, Page 124
125. EXERCISE 5 ANSWERS
1. chmod 400 mygroup OR
chmod =r mygroup
cp mygroup mygroup2 – Works
vi mygroup – Works, but you can not save any changes
echo “test” >> mygroup – Fails
rm mygroup – Prompts for deletion but can still remove the file.
2. chmod 444 my_files OR
chmod =r my_files
ls myfiles – works
cp myfiles/myissue ~ – fails
cd myfiles – fails
rm myhosts – fails
23 December 2016, Page 125
126. EXERCISE 5 ANSWERS
3. vi show_date.ksh
4. ln show_date.ksh hard_date
ln –s show_date.ksh soft_date
Once you delete show_date, only head_date works.
23 December 2016, Page 126
128. PROCESSES
A process is any program that is currently occupying memory or
swapped out to virtual memory . Processes can exist in a number of
states including running, waiting in a queue for CPU time or
sleeping.
There are two types of process:
• System processes are run in the background either by start up processes or directly by
the kernel. These processes are known as daemons and are normally owned by
root. They normally control such aspects of the system such as swapping, job
scheduling, network interfacing etc.
• User processes are run either by or on behalf of users. These processes are started at
the command prompt and are run in the foreground.
23 December 2016, Page 128
129. PROCESSES
The operating system will keep track of all running processes and
records information such as the process owner, a unique process ID
(PID) and the parent process ID (PPID). (The PPID is used to ‘wake
up’ the parent call when the child completes).
To view the process table use the ps command.
ps [<options>]
To show all the processes for the current user, run ps with no
options. It will tell you the PID, the terminal the process is running
on, the amount of CPU being used and the name of the process.
23 December 2016, Page 129
130. PROCESSES
ps –e
Lists all the processes that are currently running on the system. It
will be a long list so you’ll probably want to pipe it to more.
ps –u icebat1
Lists all the processes that are running and are owned by the user
icebat1.
Including the –f flag in any of these calls will generate a more
detailed process listing which will include the PID, PPID, the CPU
the process is running, the start time, the amount of CPU time used
and the process along with any arguments.
23 December 2016, Page 130
131. KILLING PROCESSES
Use the kill command to kill a process:
kill [<signal>] <pid-number>
There are many signals that can be used but the common ones are:
1HUP “Hang-up” Sent to all of a user’s processes when they log
out.
2INT “Interupt” Sent to a foreground process when a user hits
<ctrl>+<c>
9KILL “Kill” Sent to processes by the kill -9 command.
Terminates the process immediately, not allowing it to
tidy up.
15 TERM “Termination” Instructs the process to terminate cleanly.
A complete list can be found by running kill –l.
23 December 2016, Page 131
132. KILLING PROCESSES
kill -1 <pid-number>
Sends the Hang-up single to the listed PID. You can list multiple
PIDS if there are multiple processes you want to Hang-up. You can
also use the name of the signal.
kill -HUP <pid-number>
Has the same effect as the first example.
If no signal is supplied the -15 “Terminate” is used.
Normally when a parent process exits any child processes will be
kiiled. However this is not always the case and orphan processes
are the result. These should also be killed.
23 December 2016, Page 132
133. KILLING PROCESSES
You should only ever use the -9 signal to kill a process as a last
resort. Using this signal terminates the process immediately. The
process is not given the chance to tidy up (close files, release
sockets, tell it’s children it’s going away etc).
To kill a process you should work through the following signals in
order:
-15 : Terminate
-2 : Interrupt
-1 : Hang up
-10 : BUS Error
But try not to use -9. Don’t bring out the combine harvester to
weed a flowerpot..
23 December 2016, Page 133
134. BACKGROUND PROCESSES
As well as the kernel running background processes it is possible for
users to do the same. Any long running command that does not
output to the screen (perhaps because you have redirected STDOUT
to a file) can be run in the background.
gzip -9 * 2> gzerrors &
Runs gzip with the -9 flag on all files in the current directory,
redirecting any errors to a file called gzerrors , in the background.
Note the & - it is the character that tells the shell to run the process
in the background.
If you do not redirect the output then anything generated will go to
the screen and interrupt what the user is doing.
23 December 2016, Page 134
135. JOB CONTROL
There are a number of built in commands available in all shells
except the Bourne shell that allow you to manage running jobs.
If you have started a long running process but not placed it in the
background, you can suspend the job by issuing the ^Z signal. This
is done by using the keys <ctrl>+<z>. Your environment may not
be set up for this. To ensure it is, run the following command:
stty susp ^z
Once a job is suspended you can place it in the background, run
other jobs or bring it back to the foreground.
23 December 2016, Page 135
136. JOB CONTROL
To view all the jobs you currently have running use:
jobs [-l]
Lists all the jobs you have running in background mode or
suspended along with their job number (which is allocated by the
shell). The –l flag will include the jobs PID in the listing.
Once you know a job’s number you can bring it to the foreground:
fg [%<job-number>]
If the job number is omitted then the last job suspended will be
brought forward.
23 December 2016, Page 136
137. JOB CONTROL
To kill a job running in the background use:
kill –STOP %<job-number>
On some systems the alias stop can also be used:
stop %<job-number>
To put a job that is suspended into background mode use:
bg [%<job-number]
If no job number is included then the last suspended job will be put
into background mode.
A lower priority is given to background jobs .
23 December 2016, Page 137
138. NOHUP
If you want to run a program while you are logged off the system
you will need to ensure that the program ignores the Hang-up
signal, HUP. If it doesn’t (you’re running a shell script for instance),
then you will need to use the nohup command. This prevents the
running program from receiving the HUP signal when it is sent.
nohup my_long_script.sh &
If the job requires user input then it will hang. This applies to any
job that is running in the background.
If the job generates out output that is not already redirected it will be
written to a file called nohup.out in the directory you started the
process from.
23 December 2016, Page 138
139. PROCESS PRIORITY
Every process that runs has a priority. Priorities range from 0 – 39,
although some UNIX and Linux systems use -20 to +19. Whatever
the numbering scheme, the lower the number the higher the priority.
The higher the priority the more system resources that process will
get.
Only the superuser can increase the priority of a process. Users
can only reduce the priority of their processes.
The commands nice and renice are used for this. However they
are only mentioned here for awareness.
23 December 2016, Page 139
140. EXERCISE 6
1. Start a sleep process in the background:
sleep 100000 &
List all the processes running under your ID and find the sleep
process. Once you’ve identified it, kill it cleanly.
2. Start another sleep process but do not place it in the
background.
sleep 200000
Suspend the process and vi a new file. Suspend the vi
session and start a sleep process in the background:
sleep 300000 &
23 December 2016, Page 140
141. EXERCISE 6 (CONTINUED)
3. List all the processes that you have either running in the
background or suspended. Resume your vi session and exit it.
List all your jobs again. You should have one running and one
stopped.
4. Resume the running job in the foreground. Suspend the job again
and run the other job in the foreground.
5. Put both jobs in the background and stop them. Kill both the jobs
using their job numbers.
23 December 2016, Page 141
144. SHELL ENVIRONMENT
Everything you have done so far has been done within a shell. The
shell is a program like everything else in UNIX but it provides the
environment that you work in. Understanding the different elements
of the shell and how they can be configured will enable you to
exhibit better control over your UNIX session.
There are three main areas that you can customise within the shell:
Variables: Named data stored in memory. Variables are
important as they control how your environment is set up.
Aliases: Shortcuts to commands that can also modify it’s
actions.
Functions: Shell scripts that are held in memory.
23 December 2016, Page 144
145. VARIABLES
There are two types of variable in the shell environment.
Shell defined variables are set during login to the system and
include things like:
• Home directory location
• Username
• The terminal type
• Command line prompt
User defined variables are set by the user and can relate to
anything. There are two types of user defined variable:
• Local that are only available in the current shell
• Exported that are available in the current shell and also to children
23 December 2016, Page 145
146. SETTING VARIABLES
A variable is just a name for a location of memory holding a value.
As the name implies that value can be changed. To create a local
shell variable the syntax is:
<name>=<value>NB: there is no space around the =.
database=icepw01
Creates the variable database, if it does not already exist and
assigns it the value icepw01.
23 December 2016, Page 146
147. SETTING VARIABLES
A variable can be made up of alpha, numeric or underscore
characters, however, it must begin with a letter.
Variables names are case sensitive so DATABASE is not the same
as database. By convention shell variables are normally in upper
case.
By default a new variable will we be set as a local variable.
To view the variable use echo:
echo $database
To view all the environment variables in your shell run:
env
23 December 2016, Page 147
148. SETTING VARIABLES
The environment is split into two separate areas: a local area and an
exported area. When a child process is started from your shell (for
instance you run a new program) it will only inherit variables from
the exported area. Newly created local variables will not be
available to the program.
23 December 2016, Page 148
Local Area
Exported Area
New Shell
New Local Area
Current Shell
Copy of Exported
Area
149. SETTING VARIABLES
To make variables available to the new program you have to export
them.
database=icepw01
export database
Assigns the value icepw01 to the variable database and exports
that variable so that it is available to child processes. This can also
be achieved in a single command:
export database=icepw01
Anything that you create in your new shell will cease to exist once
that shell exits.
23 December 2016, Page 149
150. ENVIRONMENT VARIABLES
The following is a list of common environment variables found in
both the Korn and Bash shells.
LOGNAME The user’s login name as found in
/etc/passwd
HOME The user’s home directory as found in
/etc/passwd
SHELL The user’s login shell as found in
/etc/passwd
PATH A colon-separated list of directories that are
searched when a user issues a command
EDITOR The editor invoked on editing the command
line
PWD The user’s current directory
PS1 The command prompt
23 December 2016, Page 150
151. COMMAND ALIASES
A command alias is a shortcut to a command or script that you want
to run. Like all commands they are entered on the command line.
Aliases are created using the alias command which can also be
run at the command line or included in your startup scripts (profiles
are covered later).
alias ll=‘ls –l’
Creates an alias called ll that executes ls –l in the current
directory .
alias rm=‘rm –i’
Creates an alias called rm that executes rm –i when rm is called
on the command line.
To list all current aliases call alias with no arguments.
23 December 2016, Page 151
152. COMMAND ALIASES
unalias ll
Removes the ll alias from the shell.
Where you have an alias that is the same name as an existing
command, if you want to call the command then you proceed the
call with a .
rm file1
Removes file1 by calling rm directly.
23 December 2016, Page 152
153. YOUR PROFILE
It is possible to have your environment set up when you logon. This
is done through the use of profile files. There are several that
control the setup of your shell, some of which you will not be able to
change.
/etc/passwd holds the users name, home directory and shell as
well as some other user related information.
/etc/profile sets the global environment variables for all users
that log on.
.profile (Korn shell) and .bash_profile can exist in the users
home directory. They are used to set up user specific environment
variables or overwrite global values that have been set in
/etc/profile.
23 December 2016, Page 153
154. YOUR PROFILE
.kshrc (Korn shell) and .bashrc in the users home directory are
used to make changes to new shells as they are started.
The difference between the profile and the rc files is simple: the
.*profile’s get run at logon while the .*rc‘s get run when a new
shell is created.
If you need to run commands at startup they can be placed in the rc
files. While they should work if they are in the profile, if you are
using sessions such as CDE or Java Desktop then the commands
might not execute correctly.
23 December 2016, Page 154
155. .PROFILE
The .profile in your home directory can be edited to include any
environment customisations that you need. This can include
creating new shell variables, amending global ones or new aliases.
PATH=$PATH:$HOME:$HOME/bin
Adds your home directory and the bin directory in your home
directory to the PATH variable.
EDITOR=vi
Sets the command line EDITOR to be vi.
ORACLE_SID=icepw01
Sets ORACLE_SID to be icepw01.
23 December 2016, Page 155
156. .PROFILE
You can change the command prompt that you see when you logon
by changing the PS1 variable
PS1=‘$PWD > ‘
Sets the command prompt to read as the current directory. As you
change directory the prompt will change too.
This works in Linux too but it is more convenient to use bash shell
special characters. However I’ll leave that for another day (but feel
free to look them up!)
If you change your profile it is best to re-execute it by logging off and
back on again. However you can if you prefer run:
. ./.profile
23 December 2016, Page 156
157. EXERCISE 7
Using vi edit your profile:
• Change the command prompt to include the name of the UNIX box and your current working directory.
• Add your home directory to the PATH variable.
• Create an alias so that you can list the 10 most recent files in a directory.
Now log off and test your changes.
23 December 2016, Page 157
158. EXERCISE 7 ANSWER
There is no right or wrong answer...
23 December 2016, Page 158
160. SETTING YOUR ORACLE ENVIRONMENT
If you want to connect to Oracle from a UNIX session you will need
to ensure that your UNIX environment is set up correctly. Adding
the following command to your .profile will set up the appropriate
environment based on the databases that exist on that UNIX box. If
there are multiple instances then you will be able to choose the
appropriate one.
. /usr/local/oracle/user.profile
You may find it useful to have the name of the database you are
working on in your command prompt. Once the ORACLE_SID is set
above you can then use:
PS1=‘$ORACLE_SID $PWD > ‘
23 December 2016, Page 160
161. USEFUL PROGRAMS
There are many other commands and utilities available for UNIX.
Here is but a scant collection of some that you may find useful in
your day to day hacking.
All these programs have a plethora of options available. Check out
their man pages for more details.
grep <pattern> <file-list>
grep (short for Global Regular Expression Printer) searches
through the file list for <pattern> and returns all the rows that
match.
find /app/ice1 -name “file1”
Finds a file called file1, starting in the directory /app/ice1 and
searching recursively through the directory tree.
23 December 2016, Page 161
162. USEFUL PROGRAMS
sort <file>
Sorts a file into alphabetical order. DO NOT redirect the output to
your input file as the file will be overwritten! Check out the –o
option.
cut –f1 –f3 <file>
Takes input and cuts it into fields and outputs fields 1 and 3. By
default fields are delimited by whitespace, however you can change
that using –d.
finger [-m] <user-id>
Looks up a user in the /etc/passwd file. Specifying –m means
that only exact matches will be printed.
23 December 2016, Page 162
163. OTHER STUFF
set –o noclobber
Prevents the shell from overwriting existing files when using
redirection. If the file already exists the command running with
redirection will fail.
~
The shell variable ~ points to your home directory. It is the same as
using $HOME (unless you have changed it, which I wouldn’t advise!)
cp /tmp/file1 ~
Copies file1 from /tmp to your home directory.
23 December 2016, Page 163
Hinweis der Redaktion
The forerunner to UNIX, called Multics was first developed in the 1960’s by a consortium made up of Massachusetts Institute of Technology (MIT), AT&T Bell Labs, and General Electric.
AT & T’s Bell labs pulled out of the group and deployed it’s resource elsewhere. However one Bell Labs employee, Ken Thompson, continued to develop on the GE-645 mainframe and wrote a computer game called “Space Travel”. Running the game was expensive on the mainframe at $75 per instruction!
With help from Dennis Ritchie, who was at the time working on the C Programming Language, they rewrote Space Travel in assembly language for Digital Equipment Corporation's PDP-7.
This and his experience on the Multics project lead him to write a new operating system for the PDP-7.
In the 1970s the project was named Unics, and eventually could support two simultaneous users. Brian Kernighan, also of C fame, invented this name as a contrast to Multics; the spelling was later changed to Unix.
In 1973 UNIX was completely rewritten in C. This resulted in much more portable code and subsequently UNIX was ported to a number of different hardware architectures.
At this point AT & T made UNIX available to Universities and commercial firms under licence.
In the 1980s AT & T released a commercial version of UNIX that would later be know as System V. This included many enhancements that had been made by users of the original free distribution.
Due to the restrictive licensing of the new version of UNIX, University of California, Berkeley, who had contributed largely to the System V release continued to work on their own version of UNIX. This was to become BSD UNIX.
Development continued through the 1980s and 1990s of various flavours of UNIX from numerous commercial enterprises.
In 1991 a University of Helsinki student called Linus Torvolds was using an operating system called Minix. He thought he could do better and so wrote what became Linux.
The dot-com crash in the early 2000s led to a considerable consolidation of UNIX operating systems. All the UNIX variations available now are based either on System V, BSD or Linux.
This is a simplified diagram of the UNIX filesystem. On most systems there will be other OS specific directories.
You don’t have to specify a full path to a directory. If you are already in /usr then you can just cd local.
Cut & Paste: Show the candidates how cut and paste works.
If there is a failure in your command then you will normally get an error message.
Pipes, FIFOs and sockets are all used for inter-process communications.
Some commands such as lp or write don’t write to screen
Also check out /bin/dos* for examples of hard linked files.
At I’ve found that I can do all I need to at startup by just adding commands to my .profile.
There’s always a section at the end where stuff goes that didn’t quite fit anywhere else. This is that section..