UNIX AND SHELL PROGRAMMING
1005203133 3 CREDITS
UNIT 1 –
Introduction to Unix – Brief history – what is Unix- Unix components – Using Unix commands
in Unix- some basic commands- command substitution –giving multiple commands. The file
system- The basics of files- what is in a file- Directories and file names permission-Inodes- the
directory hierarchy
Unix
Unix is an operating system which has a set of programs which connects the computer and the
user.
Operating system -
An operating system is a software that manages the computer’s hardware and provides a
convenient and safe environment for running programs. It acts as an interface between programs
and the hardware resources that these programs access (like memory hard disk and printer). It is
loaded into memory when a computer is booted and remains active as long as the machine is up.
Job of an operating system: Computer's operating system (OS) manages all of the software and
hardware on the computer. Most of the time, there are several different computer programs running
at the same time, and they all need to access your computer's central processing unit (CPU),
memory, and storage. The operating system coordinates all of this to make sure each program gets
what it needs.
Key feathers of an OS
● Allocates memory and loads the program to the allocated memory.
● Loads the CPU register – register maintain the memory location
● Keeps track of the instructions
● After the program execution, it cleans up the memory and registers and make them
available for next program.
Examples: Windows, Mac, Linux
History of Operating system -
In 1960‘s definition of an operating system is software that controls the hardware. However, today,
due to microcode we need a better definition. The operating system is viewed as the programs that
make the hardware usable. In brief, an operating system is the set of programs that controls a
computer. An Operating system is software that creates a relation between the User, Software and
Hardware. It is an interface between them all. All the computers need basic software known as an
Operating System (OS) to function. The OS acts as an interface between the User, Application
Programs, Hardware and the System Peripherals. The OS is the first software to be loaded when a
computer starts up. The entire application programs are loaded after the OS.
Types of Operating System (Based on No. of user)
● Single User: If the single user Operating System is loaded in the computer's memory; the
computer would be able to handle one user at a time.
Ex: MS-DOS, MS-Win 95-98, Win-ME
● Multi user: If the multi-user Operating System is loaded in the computer's memory; the
computer would be able to handle more than one user at a time.
Ex: UNIX, Linux, XENIX
● Network: If the network Operating System is loaded in the computer's memory; the
computer would be able to handle more than one computer at time.
Ex: Novel Netware, Win-NT, Win-2000-2003
What Is UNIX -
UNIX is an operating system which was first developed in the 1960s, and has been under constant
development ever since. It is a stable, multi-user, multi-tasking system for servers, desktops and
laptops. UNIX systems also have a graphical user interface (GUI) like Microsoft Windows which
provides an easy-to-use environment. Knowledge of UNIX is required for operations which aren't
covered by a graphical program, or for when there is no windows interface available, for example,
in a telnet session. Telnet is a network protocol used to virtually access a computer and provide a
two-way, collaborative and text-based communication channel between two machines
Types of UNIX
• There are many different versions of UNIX, although they share common similarities. The
most popular varieties of UNIX are Sun Solaris, GNU/Linux, and MacOS X.
• The UNIX operating system is made up of three parts; the kernel, the shell and the
programs.
History of UNIX -
● It started as a MULTICS project (Multiplexed Operating and Computing System) in Bell
Laboratories
● It was developed by AT&T Corporation’s Bell Laboratories in the late 1960s as a result of
efforts to create a time-sharing computer system.
● Time-sharing is a computing technique that lets several users or processes share one
computer's resources, such as the CPU and memory, at the same time. Each user or process
works as if they have a dedicated computer, even though they share it with others.
● Ken Thompson involved in the project liked the potential in MULTICS.
● In 1969 he wrote the first version of Unix, called UNICS (Uniplexed operating and
computing system).
● In 1969 a team led by computer scientists Ken Thompson and Dennis Ritchie wrote the
first C compiler.
● The Full Form of UNIX (also referred to as UNICS) is UNiplexed Information
Computing System. A highly popular and multitasking Operating system.
This operating system was
● Simple and elegant.
● Written in the C programming language instead of in assembly code.
● Able to recycle code
➢ Until then, all commercially available computer systems were written in a code specifically
developed for one system.
➢ UNIX on the other hand needed only a small piece of that special code, which is now
commonly named the kernel.
➢ Using this new technique, it was much easier to develop an operating system that could run on
many different types of hardware.
➢ The software vendors were quick to adapt, since they could sell ten times more software almost
effortlessly.
➢ Several versions of UNIX built around this kernel and written in a higher programming
language, C.
➢ In the late 20th century UNIX was widely used for Internet servers, workstations, and
mainframe computers.
Features of UNIX OS
● Multi-user capability: UNIX is a multiuser system. Users can view different programs,
send messages among themselves or to the user outside the system. They can share the
database, one user can generate reports at the same time, users can also share the resources
such as printers, tapes and discs.
● Time sharing: Unix operates on the principle of time sharing. Each user program is loaded
into memory from the disc and the scheduler provides a time slice for each user in a cycle.
● Command interpreter: command interpreter is also known as a shell. Bridge between the
user and the computer. Provides a command line user interface and helps manage files and
applications.
● Portability: Ease of implementation of UNIX on different computers is known by the term
portability. The easy portability made the UNIX a more successful operating system.
● Multitasking and background processing: It has the ability to divide a processor’s time
between tasks so quickly that it appears as if everything is running simultaneously.
background processing any princess that doesn't interact with the user.
● Hierarchical file system: Unix provides an excellent multiple director file system. The
directory in UNIX can have subdirectories and files co-existing. In UNIX hierarchically
one below the other, subdirectories for specific groups of files can be made.
● Simple command structure: UNIX system commands are user-friendly and consist of a
simple command structure for most of the user commands. Commands are entered in lower
case, and are easy to remember.
● File security and protection: UNIX provides protection to user’s files and directories
from accidental or damage by other users in the system. Access permission to one's file can
be precisely defined and fixed, for reading, writing or executing or alternatively denied to
others. The owner of the files can change these permissions.
● Communication: UNIX has a variety of communication software bundled. UNIX
provides communication through telephone line, microwave links, ethernet connectivity
and satellite communication.
● DOS-Unix interface: Unix supports DOS merge utility. Users can switch from UNIX to
DOS in his terminal and execute programs under DOS. It Provides seamless integration
between MS-DOS/UNIX.
● System administration and job accounting: Provides excellent features for monitoring
and controlling the multiuser system. It controls, coordinates and maintains the system.
● Tools and Utilities: There are hundreds of tools available in UNIX and the numbers are
increasing. Some of the tools are termed as utilities such as commands used for printing
text, detecting differences between files, scanning files, sorting files.
● Shell and shell programming: The command interpreter of UNIX is called a shell. there
are two types of popular shells available in UNIX. One is the Bourne shell, and the other
is the C shell. Commands can be executed immediately or can be stored as a set. The
sequence of shell commands constitutes a shell program.
● Online documentation: Unix provides an online help to understand the proper syntax or
expression of system commands, tools and utilities with their description, with examples.
These are covered by the “man” command.
● Text processing: One of the users of UNIX is for text processing and type setting
applications. Many tools are available for text creation, text formatting, and spell checks.
● Graphics: Powerful graphic workstations used for engineering design and 3D graphics
prefer UNIX.
Applications of UNIX
● UNIX is useful everywhere such as Network management, telco, internet, banking,
shipping, security, military, factory automation.
● UNIX is also available as Your android phone, router, Apple mac, Flat TV , digital camera,
GPS.
● 90% servers use Linux including the giants like Facebook and Google.
● Big players, such as Oracle (company) and SAP (company), run on Unix. While they can
also run on Windows servers.
● Banks have long running batch programs routinely with millions of records of data getting
updated for critical operations such as interest calculation.
● UNIX OS are not prone to virus, trojans, malware, ransomware, thus few banks may run
on UNIX platform.
● Most of the software nowadays is deployed in the cloud or is a mobile app. Most of the
cloud applications run on a UNIX stack. Most mobile apps run on iOS or Android.
UNIX Components (Architecture)
There are four major layers in Unix Architecture such as follows:
Layer-1: Hardware – It consists of all hardware related information.
Layer-2: Kernel –It interacts with hardware and most of the tasks like memory management, task
scheduling, and management are done by the kernel.
Layer-3: Shell commands – Shell is the utility that processes your requests. When you type in a
command at the terminal, the shell interprets the command and calls the program that you want.
Layer-4: Application Layer – It is the outermost layer that executes the given external applications.
Kernel and operations performed by kernel-
Kernel core of the operating system. It interacts with the machine's hardware. It is loaded into
memory when the system is booted and communicates directly with the hardware. User programs
(the applications) that need to access the hardware use the services of the kernel, which performs
the job on the user's behalf. These programs access the kernel through a set of functions called
system calls. Apart from providing support to the user's program, the kernel also manages the
system's memory, schedules processes, and decides their priorities. The kernel has to run even if
no user program is running. The kernel is also called the operating system - a program's gateway
to the computer's resources.
Shell and operations performed by shell-
Shell is a command interpreter, translating commands to the computers. Shell acts as an interface
between the user and the kernel. There could be several shells running one for each user logged in
but there is only one kernel running on the system. The shell accepts commands from the user, if
required rebuilds a user command, and finally communicates with the kernel to see that the
command is executed.
Command structure in UNIX
Unix commands take the following general form
< form: verb [options] [arguments]>
The entire line is referred to as the command line. The command line is only executed only after
pressing enter. Every command has a fixed set of options. An option changes a command’s default
behavior.
Where verb is the command name which takes a set of optional options, which can be one or more
optional arguments. Commands, options and arguments are separated by spaces or tabs to enable
the shell to interpret them as words. A contiguous string of spaces together is called whitespace.
The shell compresses multiple occurrences of whitespace into single whitespace.
An Options is preceded by a minus sign (-) to distinguish it from filenames. Example: $ -l where
–l is an option to list all the attributes of the file note. There must not be any white space between
– and l.
Options can be normally combined with only one –sign.
Such as $ ls –l –a –t is same as $ ls –lat
Because UNIX was developed by people who had their own ideas as to what options should look
like there will be variations in the options like some commands use + as an option prefix instead
of -.
File Arguments
Many UNIX commands use a filename as an argument so that the command can take input from
the file. If a command uses a filename as an argument it will usually be the last argument after all
the options.
Example: ls –lat chap01 chap02 chap03 rm file1 file2 file3
Some basic commands
echo command -
command used to display a message on the terminal. it is commonly used in shell scripts and bash
files to display output status at the command line.
Example: $ echo “welcome to UNIX \n”
welcome to UNIX
Where \n – New line
printf – display a message on the terminal.
$ printf “welcome to UNIX \n”
welcome to UNIX
Listing files (ls) – It shows the list of files /directories in your current directory.
You can use 'ls -R' to show all the files not only in directories but also subdirectories.
Listing files (ls)
'ls -al' gives detailed information about the files.
Hidden items in UNIX/Linux begin with a period symbol. To show hidden file -a is used
Options in Listing files (ls)
< form: ls [options] [arguments]>
–l Lists the files in the long format
–t Lists in the order of last modification time
–d Lists directory instead of contents
-u Lists in order of last access time
Creating & Viewing files (cat)
The 'cat' command is used to display text files. It can also be used for copying, combining and
creating new text files.
To create a new file, use the command
1. cat > filename
2. Add content
3. Press 'ctrl + d'
to return to command prompt
The syntax to combine 2 files is - cat file1 file2 > newfilename
'cat' command
❑ To create a new file cat > file1.txt
This command creates a new file1.txt. After typing into the file press control+d, simultaneously tp
end the file.
❑ To append data into the file: cat >> file1.txt
To append data into the same file use append operator >> to write into the file, else the file will be
overwritten.
❑ To display a file cat file.txt
This command displays the data in the file
'cat' command
❑ To combine various files in to one file and display cat file.txt file2.txt
Some times the output may not fit the monitor screen in such cases use less command
cat file1.txt file2.txt | less
❑ To merge files and to transfer the output to another file.
cat file1.txt file2.txt > file3.txt
< form: cat [options] [file]>
-a show all
-b Omit lines numbers in the output
-e $ sign will printed at the end of each line
-n line numbers for all the output lines
-s If output has multiple empty lines it replaces with one
empty line
-t non-printing characters are printed visibly
Copy command < form: cap [options] [file]>
-a show all
-b Omit lines numbers in the output
-e $ sign will printed at the end of each line
-n line numbers for all the output lines
-s If output has multiple empty lines it replaces with one
empty line
-t non-printing characters are printed visibly
Move command < form: mv [options] [file]>
Move files or rename files
Syntax: mv [OPTION]source destination
Example: Move files1 to file2 called ‗file1‘ and ‗file2‘
$ mv file1 file2
Process command < form: ps [options] [file]>
-a list information about all the processes most frequently requested
-d List information about all processes except session leader
-e List information about every processes now running
Deleting files (rm)
The 'rm' command removes files from the system without confirmation.
Syntax: rm filename
Deleting files (rm) < form: rm [options] [arguments]>
options
-f Remove all files in a directory without prompting the user
-i asks for confirmation before removing
-r Recursively remove directories and subdirectories in the argument
list. Prompted for removal of any write-protected files.
rm –ir tmp
Creating/ Make Directories (mk)
Directories can be created on a Linux operating system using the following command.
Syntax: mkdir directoryname
Example:
Removing Directories
: To remove a directory, use the command-
Syntax: rmdir directoryname
Renaming Directory
The 'mv' (move) command (covered earlier) can also be used for
renaming directories.
Syntax: mv directoryname newdirectoryname
Change directory
Syntax: cd [OPTION] directory
Example: Change working directory to dir1
$ cd dir1
Print Directory: pwd
Print the present working directory
Syntax: pwd [OPTION]
Example: Print ‗dir1‘if a current working directory is dir1
$ pwd
Man command
Man stands for manual which is a reference book of a Linux operating system.
Syntax: $man man
Example:
History Command
History command shows all the commands that you have used in the past for the current terminal
session.
Example:
Clear command
This command clears all the clutter on the terminal and gives you a clean window
Syntax: $ Clear
Date Command: The date command displays the current day, date, time, and year. Syntax: $date
Df Command: This command reports file system disk usage. Syntax: $df
Du Command: This command reports disk usage (that is, the amount of space taken up by a group
of files). Syntax: $du
Pwd Command: This command reports the current directory path. Syntax: $pwd
Command Substitution
❑ Command substitution allows you to capture the output of a command and use it as an
argument in another command. It lets you execute a command, and the shell replaces the
command with its output.
❑ Command substitution occurs when a command is enclosed as follows:
Syntax: $(command) or `command`
❑ When you place a command line within backquotes (``), the shell first runs the command or
commands and then replaces the entire expression, including the backquotes, with the output.
Multiple commands (Command chaining)
Combining two or more commands on the command line is also known as “command chaining”.
We can combine commands on the command line in different ways such as
The semicolon operator
The logical AND operator
The logical OR operator
The semicolon operator
The semicolon (;) operator allows you to execute multiple commands in succession, regardless of
whether each previous command succeeds.
Example, type the following three commands on one line, separated by semicolons, and
press Enter. Syntax: ls ; pwd ; whoami
The logical AND operator
If you want the second command to only run if the first command is successful, separate the
commands with the logical AND operator, which is two ampersands ( && ).
Syntax: mkdir MyFolder && cd MyFolder
The folder was successfully created, so the cd command was executed and we are now in the new
folder.
The logical OR operator
Sometimes you might want to execute a second command only if the first command does not
succeed. To do this, we use the logical OR operator, or two vertical bars ( || ).
Syntax: [ -d ~/MyFolder ] || mkdir ~/MyFolder
The MyFolder directory does not exist, so the second command creates the directory.
Unix file system
Basics of files:
A file is a smallest unit in which the information is stored.
All data in Unix is organized into files.
All files are organized into directories.
These directories are organized into a tree-like structure called the file system.
❑ / : The slash / character alone denotes the root of the file system tree.
❑ /bin : Stands for “binaries” and contains certain fundamental utilities, such as ls or cp.
❑ /boot : Contains all the files that are required for successful booting process.
❑ /dev : Stands for “devices”. Contains file representations of peripheral devices and pseudo-
devices.
❑ /etc : Contains system-wide configuration files and system databases.
❑ /home : Contains the home directories for the users.
❑ /lib : Contains system libraries, and some critical files such as kernel modules or device
drivers.
❑ /mnt : Stands for “mount”. Contains file system mount points. These are used for hard disk
partitions, remote (network) file systems, CD-ROM/DVD drives.
❑ /proc : procfs virtual file system showing information about processes as files.
❑ /root : The home directory for the super user “root” – that is, the system administrator.
❑ /tmp : A place for temporary files.
❑ /usr : Originally the directory holding user home directories.
❑ /usr/bin : This directory stores all binary programs distributed with the operating system not
residing in /bin, /sbin or (rarely) /etc.
File attributes
File attributes are a type of metadata that describe and may modify how files and/or directories in
a filesystem behave. Typical file attributes are
❑ File type (i.e. what kind of data is in the file)
❑ File name (which may or may not include an extension)
❑ Physical file size
❑ File owner
❑ File protection/privacy capability
❑ File time stamp (time and date created/modified)
Types of UNIX files
The seven standard Unix file types are ordinary files, directories, special files, pipes, sockets and
symbolic links.
❑ Ordinary files
An ordinary file is a file on the system that contains data, text, or program instructions. In long-
format output of ls -l, this type of file is specified by the “-” symbol.
❑ Directories
Directories store both special and ordinary files. A directory file contains an entry for every file
and subdirectory. Each entry has two components. The Filename, A unique identification number
for the file or directory (called the inode number).In long- format output of ls –l , this type of file
is specified by the “d” symbol.
❑ Special Files
Used to represent a real physical device such as a printer, tape drive or terminal, used for
Input/output (I/O) operations. There are two flavors of special files for each device, character
special files and block special files. In long-format output of ls -l, character special files are marked
by the “c” symbol. In long-format output of ls -l, block special files are marked by the “b” symbol.
❑ Pipes
UNIX allows you to link commands together using a pipe. The pipe acts a temporary file which
only exists to hold data from one command until it is read by another. Example: who | wc -l.
❑ Sockets
A Unix socket (or Inter-process communication socket) is a special file which allows for advanced
inter-process communication. A Unix Socket is used in a client-server application framework. In
long-format output of ls -l, Unix sockets are marked by “s” symbol.
❑ Symbolic Link
Symbolic link is used for referencing some other file of the file system. Symbolic link is also
known as Soft link. In long-format output of ls –l , Symbolic link are marked by the “l” symbol.
Directories
A Directory stores the filename and inode number. The size of the directory is determined by the
number of files it contains and not by the size of the files. Directories are special files that may
contain other files. Below the root level directory are several subdirectories most of which contain
system files. Below this can exit system files, application files or user data files. All files on a Unix
system are related to one another, all files share a common parental link. Following system files
(i.e directories) are present in most Unix file systems.
❑ bin – short for binaries, this is the directory where many commonly used executable
commands reside
❑ dev - contains device specific files
❑ etc – contains system configuration files
❑ home – contains user directories and files
❑ mnt – contains device files related to mounted devices
❑ proc – contains files related to system processes
❑ lib – contains all library files
❑ root – the root user home directory
❑ sbin – system binary files reside here. If there is no sbin directory on your system, these files
most likely reside in etc.
❑ tmp - storage for temporary files which are periodically removed from the file system
❑ usr – also contains executable commands.
File names
UNIX permits file names to use most characters but avoid spaces, tabs and characters that have a
special meaning to the shell such as:
&;()|?\‘“[]{}<>$-!/
Case sensitivity: these are three different files
NOVEMBER November november
Length can be upto 256 characters
Extensions may be used to identify types of files
Hidden files have names that begin with a dot (.)
.cshrc .login .mailrc
Uniqueness – as children in a family no files with the same parent directory can have the same
name. Files located in separate directories can have identical names.
Reserved filenames:
File permissions
Every file and directory in your UNIX system has the following 3 permissions.
❑ Read: Gives the authority to open and read a file.
❑ Write: Gives authority to modify the contents of a file – add, remove, and rename files stored
in the directory
❑ Execute: In Unix / Linux, you cannot run a program unless the execute permission is set.
The first part of the code is 'rw-'.
This means the owner can:
Read the file
Write or edit the file
He cannot execute the file since the execute bit is set to '-'
The second part is 'rw-'. It for the user group 'Home' and group-members
The third part is for the world which means any user. 'r--'. Only read
Inodes
An Inode number is a uniquely existing number for all the files of Unix type systems. When a file
is created on a system, a file name and Inode number is assigned. An Inode is a data structure
containing metadata about the files. Following contents are stored in the Inode from a file:
• User ID of file
• Group ID of file
• Device ID
• File size
• Date of creation
• Permission
• Owner of the file
• File protection flag & Link counter to determine number of hard links
Inode Table: contains all the Inodes and is created when file system is created.
The df -i command can be used to check how many inodes are free and left unused in the file
system.
Each Inode has a unique number and the Inode number can be seen with the help of ls -li
command.
The directory hierarchy
The directory structure in Unix is a hierarchical tree-like organization of files on a computer. The
root directory is at the top of the tree and contains the entire system. Subdirectories branch out
from the root directory.