Fundamentals Unix
Fundamentals Unix
Version 2.0
Day 1
– UNIX Operating System
– UNIX Commands
– Files and Directories
– UNIX Utilities
– Process
Day 2
– Shell Programming
– Lab Exercises
Version 2.0 2
Module 1: UNIX Operating System
Version 2.0 3
Operating System (OS)
Version 2.0 4
Operating System (Contd.).
Version 2.0 5
History of UNIX
Version 2.0 6
What is Linux?
Version 2.0 7
Features of UNIX
Version 2.0 8
Layered Architecture
... banner
ls
cp kernel sort
comp
sh
shell hardware
as
who
ld a.out
vi date
ed wc
grep
Version 2.0 9
UNIX System Architecture
Version 2.0 10
Kernel
Version 2.0 11
The Shell
Version 2.0 12
Process Management
Version 2.0 13
CPU Scheduling
Version 2.0 14
Memory Management
Virtual memory
– Swap area
– Demand paging
Version 2.0 15
File Management
Version 2.0 16
File System
spool
inittab bin src
sh
console ls
passwd
user1
lp0
user2
Version 2.0 17
File System (Contd.).
Version 2.0 18
File System Structure
Super Block :
: ipl
e
Tr irect
Ind
Inode Block
Data Block Address of datablock
Address of datablock
:
:
Version 2.0 19
Common UNIX Flavours
Version 2.0 20
Types of UNIX Users
Version 2.0 21
Working With UNIX
Version 2.0 22
Summary
Version 2.0 23
Module 2: UNIX Commands
Version 2.0 24
Simple Commands
pwd
– Displays the current working directory.
date
– Displays the current date and time
Version 2.0 25
Simple Commands (Contd.).
who
– Displays the names of all the users who have currently logged
in
who am i
– Displays the name of the current user.
Version 2.0 26
Meta Characters
ls
Syntax : ls [options] [file….]
options: -l list in long format
-a list all files including those beginning
with a
dot
-i list inode no of file in first column
-s reports disk blocks occupied by file
-R recursively list all sub directories
-F mark type of each file
-C display files in columns
Version 2.0 28
Listing the Directory Contents (Contd.).
$ ls –l
total 6
-rwxr-xr-x 1 user1 projA 12373 Dec 15 14:45 a.out
drwxr-xr-x 2 user2 projD 4096 Dec 22 14:00 awkpro
-rw-r--r-- 1 user1 projA 12831 Dec 12 13:59 c
-rw------- 1 user1 projA 61440 Dec 15 11:16 core
-rw-r--r-- 1 user3 projC 255 Dec 20 14:29 cs
File type File access User id File size Date & time of File name
permissions in bytes modification
Link count Group id
Version 2.0 29
Getting Help on Commands
Syntax:
man [options] command_name
– Common Options
-k keyword list command synopsis line for all keyword
matches
-M path path to man pages
-a show all matching man pages (SVR4)
info command_name - help for commands
help -–command_name– gives command synatx
Version 2.0 30
Summary
Version 2.0 31
Module 3: Files and Directories
Version 2.0 32
File Access Permissions
Version 2.0 33
File Access Permissions (Contd.).
Version 2.0 34
File Access Permissions (Contd.).
Version 2.0 35
Changing Permissions - chmod
Version 2.0 36
Directory Creation
Command Syntax
mkdir [OPTION] DIRECTORY
$ mkdir <path>/<directory>
$ mkdir –m <directory>
$ mkdir –p <directory1>/<directory2>/<directory3>
Example:
$ mkdir project1
Version 2.0 37
Directory Removal
Version 2.0 38
Command - cd
Version 2.0 39
File – related Commands
Copying a file cp
Moving a file mv
Removing a file rm
Version 2.0 40
Command - cp
Example:
cp file1 file2
Version 2.0 41
Command – cp (Contd.).
Options to cp
-p
– Copies the file and preserves the following attributes
• owner id
• group id
• permissions
• last modification time
-r
– recursive copy; copy subdirectories under the directory if any
-i
– interactive; prompts for confirmation before overwriting the
target file, if it already exists
Version 2.0 42
Command – mv
Version 2.0 43
Command - rm
Version 2.0 44
Command – chown & chgrp
$ ls –l
-rwxr-xr-x 1 user1 training 12373 Dec 15 14:45 a.out
-rwxr-xr-x 3 user1 faculty 4096 Dec 24 11:56 awkpro
$ls –l
-rwxr-xr-x 1 user2 training 12373 Dec 15 14:45 a.out
-rwxr-xr-x 3 user1 faculty 4096 Dec 24 11:56 awkpro
$ls –l
-rwxr-xr-x 1 user2 training 12373 Dec 15 14:45 a.out
-rwxr-xr-x 3 user1 training 4096 Dec 24 11:56 awkpro
Version 2.0 45
Command - umask
Version 2.0 46
Command - ln
Linking files:
Hard Link (in the same filesystem)
– $ ln /usr/bin/clear /usr/bin/cls
– Hard link uses the same inode number
Soft Link (in different filesystems also used to link
directories)
– $ ln –s /usr/bin/clear /home/user1/cls
Version 2.0 47
Special Permission Bits
Version 2.0 48
Special Permission Bits (Contd.).
Version 2.0 49
Special Permission Bits (Contd.).
Version 2.0 50
Vi Editor
Version 2.0 51
Navigation
Backspace Space
h j k l
w w w $
the quick brown fox the quick brown fox
2 w ^
the quick brown fox
b b b
Version 2.0 52
Editing Commands
Version 2.0 53
Editing Commands (Contd.).
Deletion
x - to delete character at cursor position
3x - to delete 3 characters at cursor position
dw - to delete word
2dw - to delete 2 word
dd - to delete a line
2dd - to delete 2 lines
Version 2.0 54
Editing Commands (Contd.).
Yanking
– Y - copy line into buffer
– 3Y - copy 3 lines into buffer
– p - copy buffer below cursor
– P - copy buffer above cursor
Save and quit
– :w - to save
– :w! - to name a file (:w! filename -> save as)
– :x - save and quit
– :q - cancel changes
– :q! - cancel and quit
Version 2.0 55
Search & Replace Commands
Version 2.0 56
Summary
Version 2.0 57
Module 4: UNIX Utilities
Version 2.0 58
cat
cat command takes the input from the keyboard, and sends
the output to the monitor.
We can redirect the input and output using the redirection
operators.
Version 2.0 59
touch
Syntax:
touch [options] file
Options:
– -a to change the access time
– -m to change the modification time
– -c no create if not exists
touch <file> will change the time of change of the file if
the file exists.
If the file does not exist, it will create a file of zero byte
size.
Version 2.0 60
echo & read
x=10
echo $x
10
read command allows to read input from user and assign it
to the variable specified.
read x
Version 2.0 61
General Purpose Utilities
more
– Allows user to view one page-full of information at a time.
file
– Used to display the type of the file
tty
– Prints the terminal’s name
Version 2.0 62
General Purpose Utilities (Contd.).
wc
– A filter used to count the number of lines, words, and
characters in a disk file or from the standard input.
– -l - displays the number of lines
– -w - displays the number of words
– -c - displays the number of characters
Version 2.0 63
find
Version 2.0 64
find (Contd.).
Version 2.0 65
find (Contd.).
Version 2.0 66
find (Contd.).
Version 2.0 67
Standard Files
Version 2.0 68
I/O Redirection
Version 2.0 69
Filters
Filters are programs that takes its input from the standard
input file, process it, and sends it to the standard output
file.
Commonly used filter commands
– sort
– grep
– cut
– head
– tail
– paste
Version 2.0 70
sort
Sorts the contents of the given file based on the first char of
each line.
-n numeric sort (comparison made
according to strings numeric value)
-r reverse sort
-t specify delimiter for fields
+num specify sorting field numbers
+num [-num] to specify the range
Version 2.0 71
grep
Version 2.0 72
grep Options
Version 2.0 73
Patterns
Version 2.0 74
Filter Command - head
$ head -3 file1
Version 2.0 75
Filter Command - tail
$ tail -3 file1
Can also specify the line number from which the data has to
be
displayed till the end of file.
$ tail +5 file1
Version 2.0 76
Filter Command - tr
Example :
tr [a-z] [A-Z] < filename
Version 2.0 77
Filter Command – tr (Contd.).
Version 2.0 78
Command Piping
Version 2.0 79
Filter Command – tee
Version 2.0 80
Filter Command – cut
Option remark
-c used to extract characters
-d Delimiter for fields
-f Field no.
Examples:
$ cut -c2-5 file1
$ cut -d “|” -f2,3 file1
Version 2.0 81
ftp
Version 2.0 82
ftp - commands
Version 2.0 83
ftp – commands (Contd.).
Version 2.0 84
ftp – commands (Contd.).
Version 2.0 85
Tape Archive - tar
Version 2.0 86
Tape Archive – tar (Contd.).
Examples:
Create a new tar file containing all .dat files (assuming
a.dat, b.dat and c.dat exist)
$ tar –cf mytar *.dat
Version 2.0 87
Compression Utilities
gzip,
Usage is very similar to compress and pack utilities in
Unix:
– gzip [-vc] filename
• where -v displays the compression ratio.
• -c sends the compressed output to standard output and leaves the
original file intact.
gunzip
– gunzip can uncompress files originally compressed with
compress.
Version 2.0 88
Summary
Version 2.0 89
Module 5: Process
Version 2.0 90
What is a Process?
Version 2.0 91
Login shell
Version 2.0 92
ps
Version 2.0 93
Background Process
Version 2.0 94
Controlling Background Process
jobs
– List the background process
fg % <job id>
– Runs a process in the foreground
bg %<job id>
– Runs a process in the background
Version 2.0 95
The kill Command
Version 2.0 96
Process Priority
nice
– Used to reduce the priority of jobs
renice
– Used to change the priority of running process
Version 2.0 97
nohup Command
nohup
– Lets processes to continue to run even after logout
– The output of the command is sent to nohup.out if not
redirected
$ nohup command args
– $ nohup sort emp.lst &
– [1] 21356
– nohup: appending output to `nohup.out'
Version 2.0 98
wait Command
wait command
– can be used when a process has to wait for the output of a
background process
– The wait command, can be used to let the shell wait for all
background processes terminate.
– $ wait
– It is possible to wait for completion of one specific process as
well.
Version 2.0 99
Summary
– Bourne shell sh
– C shell csh
alias newname=command
$ ls a*
$ ls ??
Allows
– Defining and referencing variables
– Logic control structures such as if, for, while, case
– Input and output
Once variables are defined, one can use the echo command
to display the value of each variable:
– echo $x
– echo $textline_1
– echo $textline_2
– echo $allusers
– echo $usercount
PATH
BASH_ENV
HOME
PWD
SHELL
TERM
MAIL
USER
LOGNAME
PS1
PS2
#! /bin/bash
#
# The above line has a special meaning. It must be the
# first line of the script. It says that the commands in
# this shell script should be executed by the bash
# shell (/bin/bash).
# ---------------------------------------------------------------
echo “Hello $USER….”
echo “Welcome to programming shell scripts..”
# ---------------------------------------------------------------
----------------------script2.sh--------------------------
echo “Total parameters entered: $#”
echo “First parameter is : $1”
echo “The parameters are: $*”
shift
echo “First parameter is : $1”
------------------------------------------------------------
test <expression>
Please note that you can only compare two integers using
test.
General syntax
– test [expression]
– test integer1 operator integer2
OR
– [ integer1 operator integer2 ]
test ! expression _1
OR
[ ! expression _1 ]
if condition
then
command
fi
if condition_1
then
command
elif condition_2
then
command
else
command
fi
----------------------script.sh--------------------------
#! /bin/sh
usernames=`who | cut –d “ “ –f1`
echo “Total users logged in = $#usernames”
#
for user in ${usernames}
do
echo $user
done
------------------------------------------------------------
The Bash shell provides a while loop. The syntax of this loop
is:
while condition
do
command
…
command
done
#!/bin/bash
n=$1
if [ $n -eq 0 ]; then
fact=0
else
fact=1
while [ $n –ne 0 ]
do
fact=`expr $fact \* $n`
n=`expr $n – 1`
done
fi
echo $fact
#!/bin/bash
echo enter a choice
read choice
case $choice in
1) echo enter 2 nos
read num1
read num2
res=`expr $num1 + $num2`
echo result is $res;;
2) who;;
*) echo invalid choice;;
esac
#!/bin/bash
read number
case $number
1) echo 1st;;
2) echo 2nd;;
3) echo 3rd;;
*) echo ${number}th;;
esac
toupper()
{
tr a-z A-Z
}
$ bash –v script-file
$ bash –x script-file
Comparison between:
A solution in C
A shell solution written like a C program
A proper “shell/unix” solution
e.g:
The problem is to count the no of lines in a file ( the file is
called
the_file)
$ wc –l the_file
Websites:
http://knet.wipro.com