Você está na página 1de 40

UNIX TRAINING I

BASICS
Tejas Networks Confidential
6 September, 2000
2
What is Unix?
Manages computer resources (for example, CPU
time, memory etc.,)
Invented at AT&T Bell Labs in early 70s.
Some workstation variants
SunOS and Solaris (From Sun Microsystems), HP-UX
(From Hewlett Packard), AIX (from IBM)
Some PC & Mac variants
NetBSD, OpenBSD, Linux
Tejas Networks Confidential
6 September, 2000
3
Why Unix?
Multiple users can use it at the same time (unlike
windows where one has to logout before someone
else can use it!)
Resources are used effectively
Secure
Available on many systems from PCs to
supercomputers
Usage transparency - basic commands are same
Tejas Networks Confidential
6 September, 2000
4
Using Unix
Every user has
A User Name
Password (a secret code) dont share this!!!
A working area called home directory
One or more users are clubbed into a group. For
example, people working on the same project.
A user avails Unix services by logging in. This
creates a login session.
Enter your login name
System prompts for the password
Enter your password
Tejas Networks Confidential
6 September, 2000
5
Using Unix (contd)
After logging in Unix runs a program for you
called a shell and you land up in your home
directory.
The shell displays a shell prompt where you can
type in the commands.
A user can stop using unix by logging out. This
closes the login session.
Type logout on the screen OR
Type exit on the screen
Tejas Networks Confidential
6 September, 2000
6
What is Shell?
Shell is a program that reads the commands
that you type in, executes them and display
the results for you.
Shell sets up your working environment.
Different shells are available on Unix. For
example, csh and bash.
Tejas Networks Confidential
6 September, 2000
7
Directories and Files
UNIX computer systems store data on disk drives
using a structure called a filesystem.
There are many different types of filesystems
User accesses directories and files in the same way
irrespective of the filesystem type.
UNIX filesystems store information in a hierarchy
of directories and files.
A filesystem is created when a disk is formatted
with the proper UNIX utilities/commands.
Tejas Networks Confidential
6 September, 2000
8
Directories and Files (contd) - Example
Unix Root Directory
/
bin dev etc home lib proc sbin tmp usr var
modules
bin lib
tmp
Tejas Networks Confidential
6 September, 2000
9
Directories and Files (contd)
Directories are like folders in the filesystem.
Users can group files together inside the
directories.
Directories are represented as files with
special properties.
The top level of the UNIX filesystem is
symbolized by the "/" character. This
directory has a special name - it is called the
"root" directory.
Tejas Networks Confidential
6 September, 2000
10
Unix Directories and Files
The directories contain data in the form of
files.
Files have a name associated with them.
They are CASE SENSITIVE! (Contrast to
Windows)
In Unix all the commands are stored as files
in the directory system.
Tejas Networks Confidential
6 September, 2000
11
Get a Feel for Unix Commands
At the shell prompt type cd /. This changes the
working area to the root directory of the
filesystem.
At the shell prompt type in the word ls. This
displays the list of directories and files contained
in the root directory.
Type cd. This takes you right back to the home
directory. Type ls again. What do you see? (This
is an exercise for you)
Type echo $HOME at your shell prompt. This
prints your home directory name.
Tejas Networks Confidential
6 September, 2000
12
How do I identify Unix files and directories?
Any file (note that directory is also a file) is identified a
path name.
Path name basically is a list of directories separated by /
to the point where the file exists in the filesystem.
Absolute path describes the path from the root of the file system.
Example: the absolute path of your home directory is
/home/<username>.
Relative path describes the path with respect to the current
directory. Example: relative path of the file example.txt in your bin
directory in your home directory is (assuming it exists),
bin/example.txt
Tejas Networks Confidential
6 September, 2000
13
Gee! I dont know how to use this command!

If you know that a command exists in Unix for
some purpose and dont know how to use it,
DONOT PANIC! Unix has an Online help system.
How to use Unix help system.
Type man commandname at the shell prompt.
Type man man to get help on the help system.
Exercise:
Type man man and find out what it says
Type man cd and find out what it says
Tejas Networks Confidential
6 September, 2000
14
Working with files Basic File Commands
ls (list) list files and
directories
cat (concatenate)
print one or more files
to the screen
mv (move) renames
a file
cp (copy) copies the
contents of a file to a
new one.
rm (remove) delete a
file.
less look at the
contents of a file one
screen at t time.
tail look at the end
of the file
diff report the
differences between
files
Tejas Networks Confidential
6 September, 2000
15
Working with Directories Basic Commands
mkdir (make directory) create a directory
if it doesnt exist.
rmdir (remove directory) remove a
directory (if its empty)
pwd (present working directory) display
the directory youre in.

Tejas Networks Confidential
6 September, 2000
16
File Access Permissions
Since Unixs are used by multiple users, permissions are
associated with files and directories to protect them against
modification/deletion by others.
Types of permissions
For reading a file
For writing a file
For executing a file
Level of permissions
Owner/Creator of the files (u = user)
Group in which the owner of the file belongs to (g = group)
For the rest of the world (o = others)
Everyone (a = all)
Tejas Networks Confidential
6 September, 2000
17
File Access Permissions (contd)
Unix command to change the file access
permissions at various levels: chmod
(change mode)
Usage: chmod level+permission filename
Level (u = user/owner, g=group, o=others, a=all)
Permission type (r=read, w=write, x=execute)
Example, chmod g+w file.txt gives the write
permission to the people belonging to file.txts
owners group.
Tejas Networks Confidential
6 September, 2000
18
File Access Permissions (contd) Changing
Owner
chown (Change Owner)
Usage: chown username filename
Example: chown trainee file.txt changes the
owner of the file.txt to trainee. Note that the
user ID should be created in the system for the
command to succeed.
Tejas Networks Confidential
6 September, 2000
19
File Archiving/Compressing Commands
Archiving putting
together multiple files
into a single one.
Tar read/write data
from/to tape OR multi-
file archive on disk.
Cpio read/write data
from/to tape OR multi-
file archive on disk
Compressing shrinking
the file size.
compress compression
utility (compressed files
with suffix .Z)
gzip/gunzip GNU
compression/uncompressio
n utility. Compressed file
suffix is .gz
zip/unzip public domain
software compatible with
DOS zip.
Tejas Networks Confidential
6 September, 2000
20
Unix Processes
Unix lets multiple users run commands at
the same time.
Unix lets an individual users run commands
at the same time.
When a command is executing in the
computers memory, its called a process.
Each process has a unique integer to
identify it, called PID (Process ID).
Tejas Networks Confidential
6 September, 2000
21
Whos running now?? The ps command!
Check the status of the processes: ps
(process status)
ps options
Type ps ef or ps ax to display all processes in the
Unix system
Type ps fu trainee to display all processes associated
with the user trainee
Tejas Networks Confidential
6 September, 2000
22
ps command continued!
How to check if the process is running?
Check the process state
S Sleeping
R Runnable
< - high-priority
D doing IO
All the above are inside the Unix!
Z (Zombie a.k.a living dead) Dead and
waiting for Unix to cleanup!
Tejas Networks Confidential
6 September, 2000
23
View the processes interactively!
Top look at the process activity in real
time
Run the command top and see what it shows
The information shown on the screen is
configurable.
Exercise: View only selected fields.
Exercise: View only selected processes.

Tejas Networks Confidential
6 September, 2000
24
How to terminate processes?
If you want a process to terminate, you can
use the command kill.
Usage: kill [optional signal name] PID
Unix sends a signal to process.
Process dies when it receives a signal (default
behavior).The default may be overridden.
How do you ensure that a process is killed?
Run the command kill KILL PID
Tejas Networks Confidential
6 September, 2000
25
Jobs and Job control
You will have to wait till the command completes
before you can execute the next command.
Such commands are called foreground OR
interactive commands.
How to save time due to foreground commands?
Detach the running command from the terminal and run
it in the background.
Background commands are called jobs.
Jobs can be
Suspended/resumed
Brought to foreground
Tejas Networks Confidential
6 September, 2000
26
Know Thy Shell!
Shell gives you the working environment.
Environment is a set of initializations. The
initializations are done in:
/etc/profile (DO NOT EDIT! Common for all users)
~/.profile
~/.bashrc
What can you initialize?
Shell variables
Shell functions
Command aliases
Tejas Networks Confidential
6 September, 2000
27
Know thy Shell Variables!
Strings of characters that hold values. Example, $HOME
stands for your home directory.
To see all your built-in shell variables type set at your
shell prompt.
Setting the value of a shell variable:
At the prompt type, variablename=value
Displaying the value of a shell variable:
At the prompt, type echo $variablename
Example:
Type MYVAR=simple string at the shell prompt.
Type echo $MYVAR at the shell prompt. What do you see?
Tejas Networks Confidential
6 September, 2000
28
Know Shell Variables Of Your Environment
Some environment variables are special:
$HOME (your home directory)
$PATH (directories which are to be searched
for commands)
Exercise: Adding current directory to your search path
$TERM (your terminal type)
$SHELL (name of your shell)
$PWD (current working directory)
$OLDPWD (previous working directory)
Tejas Networks Confidential
6 September, 2000
29
Special Characters of Shell
Shell uses some special characters
~ denotes your home directory.
Example: cd ~harish will take you to the home
directory of the user harish.
Recap: Also denoted by $HOME.
. Denotes the current directory
.. Denotes the directory that contains the
current directory (also known as parent
directory).
Tejas Networks Confidential
6 September, 2000
30
Special Characters (contd) Filename
substitution
*
substitute any character(s) at the position.
Example: Type ls *.c. Displays all the C
source files in the working directory.
?
substitute any character at a given position.
Example: Type ls file?.c. Displays files with
names file1.c file2.c . filex.c etc.,
Exercises
Tejas Networks Confidential
6 September, 2000
31
Filename Substitution (contd)
How to specify a range of characters at a
position? Enclose the range in [ and ].
Example: [A-Z], [a-z], [0-9].
Exercise: Putting things together. What would
the command ls [1-3]?abcd*.* do?

Tejas Networks Confidential
6 September, 2000
32
Commands - Redirecting Input and Output
Shell lets you send the output of a command to a file of
your choice by using
> for creating/overwriting or
>> appending
between the command and the output filename.
Type ls l / > /tmp/out.txt at your shell prompt. The directory
contents of the root directory is now in the file out.txt in the /tmp
directory.
Shell lets you specify the input to a command using <
character between the command and the input filename.
Type cat < /etc/passwd at your shell prompt. It prints out the
contents of your system password file on the screen.
How to specify more than one input will be covered later.
Tejas Networks Confidential
6 September, 2000
33
I/O Redirection - Pipes
How to create?
command1 | command2
What does it do?
To send the output from command1 as the input to
command2.
What is it used for?
To build complex shell commands.
Example:
Type ps aux|less. What will it do? You can see the
output of the ps command one screenful at a time.
Tejas Networks Confidential
6 September, 2000
34
Programming Shell! - Scripts
Shell script A collection of Unix
commands which are executed together.
Whats the use?
Combine the individual commands to perform
specialized and complex tasks.
How does the shell let me combine the
command?
Provides a simple programming language!
Tejas Networks Confidential
6 September, 2000
35
Quick Diversion Editing your programs
with Vi!
Vi
Screen oriented editor
Extremely powerful
Mapping keyboard strokes to commands
Pattern substitution
Cutting and pasting text
And much more!
Addictive : Youll never leave it once you know it!
Two modes
Command Mode (controlling the editing)
Editing Mode (enter the text)
Tejas Networks Confidential
6 September, 2000
36
Editing with Vi (contd) Entering and
Quitting!
Entering Vi
Type vi OR vi filename.
By default the Vi is in command mode when you enter.
Quitting In the quit command you can optionally
provide the filename
Type :wq [optional_filename] to save and quit
Type :q to quit (the edited file should be already
saved)
Type :q! to quit without saving
Tejas Networks Confidential
6 September, 2000
37
Editing with Vi (contd) - Command Mode
How to enter? Type ESCAPE key on the key
board
Enter editing mode :
Press i to insert new characters at a position
Press a to append characters after current position
Save the edited text : Type :w filename
Find and replace text : Type %s/oldpattern/newpattern/
Navigate through lines and words
Press h: one character left
Press l: one character right
Press k: one line up
Press j : one line down
Tejas Networks Confidential
6 September, 2000
38
Editing with Vi (Contd)
More info
http://www.thomer.com/thomer/vi/vi.html
http://www.vim.org/

Tejas Networks Confidential
6 September, 2000
39
Shell Programming Your First Program
Type vi example-1.sh at your shell prompt.
Enter editing mode
Type in echo Hello world in the vi screen.
Save and quit
Type chmod a+x example.sh at your command
prompt. Remember: You need execute permission
to execute anything in Unix.
Type ./example-1.sh. What do you see?
Tejas Networks Confidential
6 September, 2000
40
Your second Shell Program!
Using Vi edit example-2.sh
Enter the following lines:
`cat /etc/passwd | grep trainee | awk {print $1}` >
/tmp/example.output
cat /tmp/example.output
Save, quit and give execute permission to the file.
Type ./example-2.sh at shell prompt
What do you see? What did you learn?

Você também pode gostar