You are on page 1of 72

UNIX/Linux Overview

1
Objectives
➲ What is Unix?

➲ Why do I need to know Unix?

➲ Unix history.

➲ Architecture of Unix/Linux.

➲ Important files and commands.

➲ How is Unix/Linux booted?


2
What is Unix?
What is an operating system?
The low-level (first layer loaded) software
which handles the interface to system hardware
(input/output devices, memory, file system, etc),
schedules tasks, and provides common core
services such as a basic user interface.
Colloquially,all of the software that comes
with a system before applications are installed.

3
What is Unix?
A portable, multi-tasking and
multi-user operating system
➲ Portable: runs on many different hardware
architectures (Intel x86 and IA-64, Alpha,
MIPS, HP PA-RISC, PowerPC, IBM S/390,
SPARC, Motorola 680x0, etc.).
➲ Preemptive multi-tasking: several programs
can run at the same time (time slices,
interrupts, and task switching).
➲ Multi-user: many users can share the
computer system at the same time.
4
What is Unix?
Other Features
➲ Uses a simple, uniform file model which includes
devices and access to other services in a
flexible, hierarchical file system.
➲ Written in a high-level language (“C”) making it
easy to read, understand, change and port.
➲ The command prompt is a simple user process,
the Unix shell, which is also a convenient job
programming language.
➲ Includes support for regular expressions which
are convenient for complex searching.
5
Is LINUX really UNIX?


Well, yes and no:

Yes, because it has essentially the same look and
feel like any UNIX operating System.

Yes, because it offers the ability to run nearly any
program that runs on UNIX systems (through API
conventions such as POSIX, etc..).

No because the heart of the system (kernel) has a
lot of new features that go beyond the classical
design philosophy of UNIX kernels.
Unix History

➲ 1964 joint project between AT&T Bell Labs,


GE, and MIT to develop a new OS.

➲ Goal : develop an OS that could provide


computational power, data storage and the
ability to share data among multiple users.

➲ Result: Multiplexed Information & Computer


Service - MULTICS.

7
Unix History
➲ 1969 Bell Labs withdraws from group.

➲ Two Bell Lab scientists, Ken Thompson and


Dennis Ritchie, continue research. They
were still left without a “Convenient
interactive computing service”*.

* Ritchie, D.M. “The Evolution of the Unix Time-sharing System”, AT&T Bell Laboratories

Technical Journal, Oct. 1984, Vol 63, No.8, Part 2, pp. 1577-1594.

8
Unix History
➲ At the same time Ken
Thompson wrote a game
“space travel” in Fortran to
run on GECOS OS
(Honeywell 635).
➲ The spaceship was hard
to control and it was
expensive to run. He was
told to get the game off his
work computer.

9
Unix History
➲ Thompson ported the game to a little
used PDP-7 computer.
➲ Unics (later Unix) was born as a pun
on Multics.

10
Unix History
PDP-7

11
Unix History

➲ Dennis Ritchie
developed “B” . Then
wrote “C” a compiled
language.

➲ In 1973 entire OS
ported to “C”.

12
Linus Torvalds

 1991 Linux 0.02 is first released to the public.


 1994 Linux 1.0 is released.

14
Three Definitions of Linux
➲Linux Kernel: The very low-level software that manages
your computer hardware and provides a library (POSIX)
interface for user-level software. The Linux kernel runs on
many platforms (Intel x86 and IA-64, Alpha, MIPS, HP PA-
RISC, PowerPC, IBM S/390, SPARC, Motorola 680x0, etc.).

➲GNU/Linux OS: The Linux kernel plus utility software to


provide a useful working environment.

➲Linux Distributions: The packaging of the Linux Kernel, the


GNU/Linux OS and lots of other software to make Linux easy
to install, configure, and use (at least for the target audience).
Tux, the Linux Mascot
The “Free” Software Movement
The GNU Project:
www.gnu.org
The Four Freedoms
➲ The freedom to run the program, for any purpose.
➲ The freedom to study how the program works,
and adapt it to your needs.
➲ The freedom to redistribute copies so you can
help your neighbor.
➲ The freedom to improve the program, and
release your improvements to the public, so that
the whole community benefits.
What is GNU?

GNU stands for “GNU's Not Unix”. The three letter
abbreviation is not a joke. It emphasizes a major projec
of the Free Software Foundation (FSF) that really
created the LINUX operating system with many of its
popular tools.

Richard Stallman created FSF, in order to encourage
the development and use of freely redistributable code.

Freely means the freedom of redistributing your code
under certain conditions. It does NOT mean zero
financial cost!

The Gnu Public License (GPL) defines the terms and
conditions of redistributing the LINUX kernel and other
tools that make it usable, forming a LINUX distribution.
Architecture of Unix
➲ Kernel: Schedules programs,
Manages data/file access and storage,
Enforces security,
Performs all hardware access.

➲ Init: First program run by kernel on booting.

➲ Shell: Presents each user a prompt,


Interprets commands typed by user,
Executes users commands,
Provides user/programming environment.

19
Architecture of Unix
Architecture of Unix
Unix file system
root (/)

bin usr
dev etc
var
home tmp bin

spool log X11R6


(users’ accounts) mail

(users’ mail)
21
Typical Directories
➲ /: Root of the tree. Where it starts.

➲ bin, sbin, usr/bin: software for the shells and


most common Unix commands.

➲ dev: short for devices, holds the files necessary


to operate peripherals such as printers and
terminals.

➲ home: contains the home directories of users


(/export/home on sun computers).
22
Typical Directories
➲ tmp: holds temporary files.
➲ var: contains files that vary in size;
(Mail directories, printer spool files,
logs, etc.)

➲ etc: administrative files such as lists of


user names and passwords.

23
Typical Directories
➲ usr: Contains application programs
➲ lib: Contains libraries for programs
➲ proc: a pseudo-filesystem used as an
interface to kernel data structures.

24
File and Directory Name Rules
➲ Valid names can be made up of:
● Uppercase letters (A to Z).
● Lower case letters (a to z). Case sensitive!!!
● Numbers (0 to 9).
● Period (.), underscore (_), commas (,).
➲ Should not contain spaces or the following:
● & * \ | [ ] { } $ < > ( ) # ? ‘ “ ; ^ ! ~ %. Never /.
● You should avoid naming files or directories with
Unix commands.

25
File System Structure
➲ Unix stores a file's administrative
information (its physical location on
disk, permissions including ownership
and modification times) in an inode (i-
node or Index Node).
➲ The file name (link) is stored in the
contents of a directory entry. Deleting
a file consists of removing a link to the
inode (the inode itself is not deleted).

26
File System Structure
Data Recovery:
➲ When a file is deleted the number of
links to the inode is reduced by one.
➲ Note: an inode may have more than
one link (or name) --- see ln(1).
➲ If the number of links becomes zero,
the kernel may reuse the disk space
making recovery difficult. Magnetic
Force Microscopy (MFM) can recover
most data unless wipe(1) is used.
27
The K Desktop Environment (KDE)
GNOME
OpenLook Windows Manager (olwm)

30
Common Desktop Environment (CDE)

31
How Do You Boot Unix?
Multi-boot Programs
➲ Windows NT OS loader- select the OS of
choice

➲ Linux LILO (Linux loader)- the tab key will


show
Selections. Type the name of the OS to load

➲ Grub – Newer versions of Linux (i.e., 9.0) use this loader


to choose the OS

➲ Other methods to boot- Norton system


commander, boot disc, Boot Magic…

32
Shell

➲ Program that interacts with kernel


➲ Bridge between kernel and the user
➲ Command interpreter
➲ User can type command and the
command is conveyed to the kernel and it
will be executed
Types of Shell
➲ Sh – simple shell
➲ BASH – Bourne Again Shell
➲ KSH – Korne Shell
➲ CSH – C Shell
➲ SSH – Secure Shell
➲ To use a particular shell type the shell name at the command
prompt.
● Eg $csh – will switch the current shell to c shell

➲ To view the available shells in the system, type cat /etc/shells at


the command prompt
➲ To view the current shell that is being used, type echo $SHELL at
the command prompt
UNIX COMMANDS
UNIX Commands
A command is a program which interacts with the
kernel to provide the environment and perform the
functions called for by the user.

A command can be: a built-in shell command; an


executable shell file, known as a shell script; or a source
compiled, object code file.

The shell is a command line interpreter. The user


interacts with the kernel through the shell. You can
write ASCII (text) scripts to be acted upon by a shell.
Linux Command Basics

➲ To execute a command, type its name


and arguments at the command line

ls -l /etc

Command name Arguments


Options
(flags)
Standard Files

➲ UNIX concept of “standard files”


● standard input (where a command gets its
input) - default is the terminal
● standard output (where a command writes it
output) - default is the terminal
● standard error (where a command writes
error messages) - default is the terminal
Redirecting Output

➲ The output of a command may be sent


(piped) to a file:
ls -l >output
“>” is used to specify
the output file
Redirecting Input

➲ The input of a command may come (be


piped) from a file:
wc <input
“<” is used to specify
the input file
Common Commands

➲ pwd - print (display) the working directory


➲ cd <dir> - change the current working
directory to dir
➲ ls - list the files in the current working directory
➲ ls -l - list the files in the current working
directory in long format
File Commands

➲ cp <fromfile> <tofile>
● Copy from the <fromfile> to the <tofile>
➲ mv <fromfile> <tofile>
● Move/rename the <fromfile> to the <tofile>
➲ rm <file>
● Remove the file named <file>
➲ mkdir <newdir>
● Make a new directory called <newdir>
➲ rmdir <dir>
● Remove an (empty) directory
More Commands

➲ who
● List who is currently logged on to the system
➲ whoami
● Report what user you are logged on as
➲ ps
● List your processes on the system
➲ ps aux
● List all the processes on the system
➲ echo “A string to be echoed”
● Echo a string (or list of arguments) to the terminal
More Commands

➲ alias - used to tailor commands:


● alias erase=rm
● alias grep=”grep -i”
➲ ar - Maintain archive libraries: a
collection of files (usually object files
which may be linked to a program, like a
CMS TXTLIB)
More Commands

➲ awk - a file processing language that is


well suited to data manipulation and
retrieval of information from text files
➲ chown - sets the user ID (UID) to owner
for the files and directories named by
pathname arguments. This command is
useful when from test to production
More Commands

➲ diff - attempts to determine the


minimal set of changes needed to
convert a file specified by the first
argument into the file specified by the
second argument
➲ find - Searches a given file hierarchy
specified by path, finding files that match
the criteria given by expression
More Commands
➲ grep - Searches files for one or more
pattern arguments. It does plain string,
basic regular expression, and extended
regular expression searching
More Commands

➲ kill - sends a signal to a process or


process group
➲ You can only kill your own processes
unless you are root
UID PID PPID C STIME TTY TIME CMD
root 6715 6692 2 14:34 ttyp0 00:00:00 sleep 10h
root 6716 6692 0 14:34 ttyp0 00:00:00 ps -ef
[root@penguinvm log]# kill 6715
[1]+ Terminated sleep 10h
More Commands

➲ make - helps you manage projects


containing a set of interdependent files
(e.g. a program with many source and
object files; a document built from source
files; macro files)
➲ make keeps all such files up to date with
one another: If one file changes, make
updates all the other files that depend on
the changed file
➲ Roughly the equivalent of VMFBLD
More Commands

➲ sed - applies a set of editing


subcommands contained in a script to each
argument input file
find ./ -name "*.c,v" | sed ’s/,v//g’ | xargs grep "PATH"

This finds all files in the current and subsequent directories with an extension of c,v. sed then
strips the ,v off the results of the find command. xargs then uses the results of sed and builds a
grep command which searches for occurrences of the word PATH in the C source files.
Pipes
An important early development in Unix was the
invention of "pipes," a way to pass the output of
one tool to the input of another.
eg. $ who | wc −l
By combining these two tools, giving the wc
command the output of who, you can build a new
command to list the number of users currently on
the system
Basic Unix commands (vs. DOS)
➲ cat file Concatenate or type out a file,
cat file1 file2 ... Type out a number of files
➲ cd directory1 Change current directory to directory1
cd /usr/bin Change current directory to /usr/bin
cd Change back to your home directory
➲ clear Clear the current screen
➲ cp file1 file2 Copy file1 to file2
cp file1 file2 ... dirCopy a number of files to a directory
➲ ls List the files in the current directory
ls /usr/bin List the files in the /usr/bin directory
➲ lpr file1 Print file1 out , lpr file1 file2 .. Print a number of files out
➲ more file Look at the content of a file with paging, use ‘q’ to get out
Basic Unix commands (vs. DOS)

➲ mkdir directory Create a directory


➲ mv file1 file2 Move file1 to file2, like rename.
mv file1 file2 ... dirMove a number of files into a directory
mv dir1 dir2 Move or rename a directory

➲ rm file Remove a file


rm file1 file2 ... Remove a number of files
rm -r directory Remove a directory include the sub-directory
➲ rmdir directory Remove a directory
Basic Unix commands (vs. DOS)
DOS Command Unix Descriptions
Command
CD cd Change directory
CHKDSK du Disk usage
CLS clear Clear the current screen
COPY cp Copying files
DEL rm Removing files or directorie
DIR ls File listing of directories
MD mkdir Create a directory
MORE more Type out a file with paging
PRINT lpr Print out a file
RD rmdir Remove a directory
RENAME mv Moving files around
TYPE cat Type out files
Who, When, Why, What and Where?

man cp Display on-line manual for the “cp” command


man -k keyword Display manual help file related to the keyword
passwd Change your login password
pwd Display the path name of where you are
uptime Tell you how long the machine has been up and running
users Tell you who is logging in
who Tell you who is logging-in in detail
w Tell you who is logging in and doing what!
whoami Show you the owner of this account

finger user Find out the personal information of a user


finger name Try to find the person’s info. by his/her name
finger email-address Try to find the person’s info across the network
write user Write a message on somebody’s screen
talk user Talk to the person logging in the same system with you
talk email-address Talk to somebody logging in the network
date Display today’s time and date
cal year Display the calendar of the specified year (e.g. 1997)
Shell Scripting

➲ Shell scripting is the most useful and


powerful feature in Linux
● Minimizes typing of repetitive command
● Can schedule jobs to run in the system
● Can initiate back up activities for system
administration
● Similar to batch files in DOS, but more
powerful than Batch files
Working with shell script

➲ Open a file with extension .sh using vi


editor
➲ We can type any number of commands
that we use to type at command prompt
➲ Save the file
➲ Execute the file
● sh file.sh
● ./file.sh (if the file has execution permission)
VI Editor

➲ Popular text editor


➲ Just type vi <<filename>> at the prompt
and hit the enter key.
➲ A new file will be opened
➲ Type the contents needed and save
➲ To save, press the Esc Key and then
press : (colon) w q and then enter
➲ To quit with out saving Esc + : + q and
then enter
Vi editor

➲ Navigation
● Left - h
● Down -j
● Up - k
● Right -l
● Top of the screen – H (shift + h) //caps lock will not work
● Middle of the screen – M (shift + m)
● Bottom of the screen – L (shift + l)
● $ - End Key, 0 – Home Key
➲ Edit Commands
● Cut – X, x
● Copy – yy, yw
● Paste – P, p
Vi editor

X – cuts the letter after cursor position


x – cuts the letter before cursor position

yy – Copies a line at which cursor is currently positioned


yw – Copies a word at which cursor is currently positioned

P – Pastes the copied content after cursor position


p – Pastes the copied content before cursor position

dd – Deletes a line
Shell Scripts

➲ To Print a line
● echo “Hello World” (Prints Hello World in the screen)
➲ To read a line
● read n (Stores the content entered by user in variable
n
➲ To Comment a line
● # This is a comment
● Only single line comment is available. For multi line
comment, we need to use # symbol in lines which we
want to comment.
Conditions
➲ We can have if, if else, if elif else and case statements (Nested if
statements are also possible
1. if [ condn ]
then
fi

2. if [ condn ]
then
else
fi

3. if [ condn ]
then
elif [ condn ]
then
else
fi
Conditions (Case)
case expr in
Option1) stmt ;;
Option2) stmt ;;
*) stmt ;;
esac
➲ Every option should be terminated with a
double semicolon.
➲ Denotes default case
➲ Case should be termniated with esac
Loops
➲ For loop
for i in 1 2 3 4 5 //Loops 5 times
do
Body of the loop
done

for (( i=0; i<5; i++ ))


do
Body of the loop
done
While Loop

while [ condn ]
do
body of the loop
done

We need to ensure that the while loop


condition is terminated in a finite way
Comparison
➲ For integer comparison we have the
following
-eq : equal to
-ne : not equal to
-lt : less than
-gt : greater than
-le : less than or equal to
-ge : greater than or equal to
Comparison

➲ For string comparison we have


- = : equal to
- ~= : not equal to

➲ For logical operators


-a : AND
-o : OR
Arrays
➲ Initialising an array
● A[0] = 10
● A[1] = Hi
➲ Using an array
● ${A[0]} : retrieves 10

➲ Here arrays can contain data belonging to


different data types
➲ Uninitialised index in arrays will have null value
by default
Functions

➲ Local Function
● Written at the command prompt
● Lasts for the current session alone
➲ Global Function
● Written in .bashrc file
● Available as long as the definition is there
in .bashrc file
➲ Function in shell script
● Available with in the file alone
Parameters

➲ Sample function
Functionname()
{
echo $1
}

Calling function:
Functionname Ram
Result:
Ram
We can pass any number of parameters like this

$0 – File name
$1,$2… - first, second parameters and so on
$? – stores the status (success or failure status of the last command that was
executed)
$# - stores the number of parameters passed through the command line

#pwd
/home
#echo $?
0 // As the last command is success
#abc
Error:command not found
#echo $?
127 // as the last command didn’t execute successfully
C Program in Linux
➲ Open a file with extension .c from the command prompt using vi
editor
● vi hello.c

➲ Type the contents and save (Esc : wq!)


➲ Compile the file
● gcc hello.c

➲ Run the executable


● ./a.out

➲ Compile file with output option


● gcc –o hello hello.c

➲ Run the executable


● ./hello

You might also like