0% found this document useful (0 votes)
12 views97 pages

1736411761-Linux 5 Units Notes

The document provides an overview of the Linux operating system, detailing its functions, types, and components. It explains key concepts such as memory management, processor management, device management, and file management, along with the roles of the kernel and shell. Additionally, it highlights the various types of operating systems and their applications, emphasizing Linux's features like portability, open-source nature, multi-user capabilities, and security.

Uploaded by

supravat.p2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views97 pages

1736411761-Linux 5 Units Notes

The document provides an overview of the Linux operating system, detailing its functions, types, and components. It explains key concepts such as memory management, processor management, device management, and file management, along with the roles of the kernel and shell. Additionally, it highlights the various types of operating systems and their applications, emphasizing Linux's features like portability, open-source nature, multi-user capabilities, and security.

Uploaded by

supravat.p2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 97

II B.C.A.

-‘C’ (2016-2019) KGCAS

LINUX AND SHELL PROGRAMMING


UNIT – I
Introduction to LINUX Operating System: Introduction - The LINUX Operating System.

Linux Operating system:


Introduction:
Operating System - Overview
An Operating System (OS) is an interface between a computer user and computer hardware. An operating
system is a software which performs all the basic tasks like file management, memory management, process
management, handling input and output, and controlling peripheral devices such as disk drives and printers.
Some popular Operating Systems include Linux, Windows, OS X, VMS, OS/400, AIX, z/OS, etc.
Definition
An operating system is a program that acts as an interface between the user and the computer hardware and
controls the execution of all kinds of programs.

Following are some of important functions of an operating System.


 Memory Management
 Processor Management
 Device Management
 File Management
 Security
 Control over system performance
 Job accounting
 Error detecting aids
 Coordination between other software and users
Memory Management
Memory management refers to management of Primary Memory or Main Memory. Main memory is a large
array of words or bytes where each word or byte has its own address.
Main memory provides a fast storage that can be accessed directly by the CPU. For a program to be
executed, it must in the main memory. An Operating System does the following activities for memory
management −
 Keeps tracks of primary memory, i.e., what part of it are in use by whom, what part are not in use.
 In multiprogramming, the OS decides which process will get memory when and how much.
 Allocates the memory when a process requests it to do so.
 De-allocates the memory when a process no longer needs it or has been terminated.

1
II B.C.A.-‘C’ (2016-2019) KGCAS
Processor Management
In multiprogramming environment, the OS decides which process gets the processor when and for how
much time. This function is called process scheduling. An Operating System does the following activities
for processor management −
 Keeps tracks of processor and status of process. The program responsible for this task is known as
traffic controller.
 Allocates the processor (CPU) to a process.
 De-allocates processor when a process is no longer required.
Device Management
An Operating System manages device communication via their respective drivers. It does the following
activities for device management −
 Keeps tracks of all devices. Program responsible for this task is known as the I/O controller.
 Decides which process gets the device when and for how much time.
 Allocates the device in the efficient way.
 De-allocates devices.
File Management
A file system is normally organized into directories for easy navigation and usage. These directories may
contain files and other directions.
An Operating System does the following activities for file management −
 Keeps track of information, location, uses, status etc. The collective facilities are often known as file
system.
 Decides who gets the resources.
 Allocates the resources.
 De-allocates the resources.
Other Important Activities
Following are some of the important activities that an Operating System performs −
 Security − By means of password and similar other techniques, it prevents unauthorized access to
programs and data.
 Control over system performance − Recording delays between request for a service and response
from the system.
 Job accounting − Keeping track of time and resources used by various jobs and users.
 Error detecting aids − Production of dumps, traces, error messages, and other debugging and error
detecting aids.
 Coordination between other softwares and users − Coordination and assignment of compilers,
interpreters, assemblers and other software to the various users of the computer systems.

Different types of Operating System


Key difference: Operating Systems, shortly OS is an intermediate between the computer hardware and
users. It is a collection of software which manages the computer hardware resources and provides common
services for computer programs. Today, there are different types of standard operating systems which are
developed and used as middleware.

Operating systems (OS) are known to simplify and provide the human computer interactions. These are
responsible for connecting the user-defined application programs with the hardware; due to which it
2
II B.C.A.-‘C’ (2016-2019) KGCAS
becomes easy to access over the computers. They host several applications which run on computers and
hence handle the operations related to their functioning with that of the computer. Hence, Operating Systems
are essential component of computer system software; as all the application programs usually function and
run on these operating systems.
According to the developing Computer and Information technology, there has been a tremendous growth in
the development sector. Today, there are different types of Operating system, which acts as a middleware
between the hardware and the user, thereby providing easy and understanding interactions.
Comparison of various Operating Systems:
Real-Time Operating System:
Real-time Operating System also known as RTOS; are used for managing the control machinery, scientific
instruments and industrial systems. It manages the computer resources and handles the operation in a precise
way by providing equal amount of time for every operation. It has very little user-interface capability, and
no end-user utilities.
There are two types of real-time operating systems.
Hard Real-Time Systems- the system guarantee the completion of the critical task on time. In this system,
the secondary storage is limited or missing with data stored in ROM and the virtual memory is also almost
never found.
Soft Real-Time Systems- these are less restrictive and hence these give the first priority to the critical tasks
than the other tasks. They retain the priority until the task gets completed. These have limited utility than
hard real-time systems. For example, Multimedia, Virtual Reality, Advanced Scientific Projects like
undersea exploration and planetary rovers etc.

Single-User, Single Task Operating System:


From the names itself it gets clear that these operating systems work on single task and single user at a time.
Here, a single tasks or operation can be carried out efficiently and thoroughly by a single user. The Palm OS
for Palm handheld computers is a good example of a modern single-user, single-task operating system.

Single-User, Multi-Task Operating System:


These operating systems works on more than one task and process them concurrently at a time. The
processors of this OS divides the time among the several executed tasks. This division of time is called as
Time Sharing systems. Here, the processors switch rapidly between the processes. For example, the user

3
II B.C.A.-‘C’ (2016-2019) KGCAS
can listen to music on the computer while writing an article using word processor software. Users can also
switch between the applications and transfer the data between them; Windows 95 and all later versions of
Windows are examples of multitasking OS.

Multiuser Operating System:


In these OS, multiple users are allowed to access the same data or information at a time via a network. The
users can also interact among each-other. Some examples of this type of Os are: Linux, UNIX, and
Windows 7.

Multiprocessing Operating System:


Here, a single process runs on two or more processors. All the processing and their management takes place
in a parallel way, hence this OS are also called as Parallel Processing. Each processor works on different
parts of the same task, or, on two or more different tasks. As their execution works in parallel, these are
applicable for high speed execution, and also to increase the power of computer. For example: Linux, UNIX
and Windows 7 are examples of multiprocessing OS.

Embedded Operating System:


These are embedded in a device, which is located in ROM. These are applicable and developed only for the
needed resources and accordingly developed. These OS are less resource intensive. Mainly, applicable in
appliances like microwaves, washing machines, traffic control systems etc.

4
II B.C.A.-‘C’ (2016-2019) KGCAS

Distributed Operating System:


In these OS, the computers work in co-operation with each other. As this OS manages a group of
independent computers and makes them appear to be a single computer is known as a distributed operating
system.

Along with these OS, the other developed OS are Mobile Operating Systems, which are now
famous in all types of latest and developed mobiles; among them the most renowned and famous are the
Android OS with all multitasking and application features. In Batch Processing and Interactive Systems,
the execution of programs takes place in batches. The Online and Offline Processing Systems offers the
processing of data in online mode.
Some of the most used Oerating systems in todays market are listed below:
Linux / Ubuntu, MacOS, MS-DOS, IBM OS/2 Warp, Unix / Variants, Windows CE, Windows 3.x,
Windows 95, Windows 98, Windows 98 SE, Windows ME, Windows NT, Windows 2000, Windows
XP, Windows Vista, Windows 7

What Is Linux?
In the simple language Linux is an operating system (OS). We all are familiar with other operating systems
like Microsoft windows, Apple Mac OS, iOS, Google android, etc, just like them linux is also an operating
system. An operating system is a software that enables communication between computer hardware and
software. It conveys input to get processed by the processor and brings output to the hardware to display it.
This is the basic function of an operating system. Although, it performs many other important tasks, let's not
talk about that. Linux is around us since mid 90s. It can be used from wristwatches to supercomputers. It is
everywhere in our phones, laptops, PCs, cars and even in refrigerators. It is very much famous among the
developers and normal computer users.
Who created Linux?

5
II B.C.A.-‘C’ (2016-2019) KGCAS
Linux is an operating system or a kernel which germinated as an idea in the mind of young and bright Linus
Torvalds when he was a computer science student. He used to work on the UNIX OS (proprietary
software) and thought that it needed improvements.

However, when his suggestions were rejected by the designers of UNIX, he thought of launching an OS
which will be receptive to changes, modifications suggested by its users.

Where I can use Linux?


You can use Linux as Server Os or as stand alone Os on your PC. (But it is best suited for Server.) As a
server Os it provides different services/network resources to client. Server Os must be:
 Stable
 Robust
 Secure
 High Performance
Linux offers all of the above characteristics plus its Open Source and Free OS. So Linux can be used as:
(1) On stand alone workstation/PC for word processing, graphics, software development, internet, e-mail,
chatting, small personal database management system etc.
(2) In network environment as:
(A) File and Print or Application Server
Share the data, Connect the expensive device like printer and share it, e-mail within the LAN/intranet etc are
some of the application.

Linux Server with different Client Os


(B) Linux sever cab be connected to Internet, So that PC's on intranet can share the internet/e-mail etc. You
can put your web sever that run your web site or transmit the information on the internet.

Linux Server can act as Proxy/Mail/WWW/Router Server etc.


So you can use Linux for:
 Personal Work
 Web Server
 Software Development Workstation
 Workgroup Server
 In Data Center for various server activities such as FTP, Telnet, SSH, Web, Mail, Proxy, Proxy
Cache Appliance etc

What Kernel Is?


Kernel is hart of Linux Os.

6
II B.C.A.-‘C’ (2016-2019) KGCAS
It manages resource of Linux Os. Resources means facilities available in Linux. For e.g. Facility to store
data, print data on printer, memory, file management etc .
Kernel decides who will use this resource, for how long and when. It runs your programs (or set up to
execute binary files).
The kernel acts as an intermediary between the computer hardware and various programs/application/shell.

It's Memory resident portion of Linux. It performance following task :-


 I/O management
 Process management
 Device management
 File management
 Memory management

What is Linux Shell ?


Computer understand the language of 0's and 1's called binary language.
In early days of computing, instruction are provided using binary language, which is difficult for all of us, to
read and write. So in Os there is special program called Shell. Shell accepts your instruction or commands in
English (mostly) and if its a valid command, it is pass to kernel.
Shell is a user program or it's environment provided for user interaction. Shell is an command language
interpreter that executes commands read from the standard input device (keyboard) or from a file.
Shell is not part of system kernel, but uses the system kernel to execute programs, create files etc.
Several shell available with Linux including:
Shell Name Developed by Where Remark
BASH ( Bourne-Again Brian Fox and Chet Free Software Foundation Most common shell in
SHell ) Ramey Linux. It's Freeware
shell.
CSH (C SHell) Bill Joy University of California The C shell's syntax and
(For BSD) usage are very similar to
the C programming
language.
KSH (Korn SHell) David Korn AT & T Bell Labs --
TCSH See the man page. -- TCSH is an enhanced but
Type $ man tcsh completely compatible
version of the Berkeley
UNIX C shell (CSH).
Tip: To find all available shells in your system type following command:
$ cat /etc/shells
7
II B.C.A.-‘C’ (2016-2019) KGCAS
Note that each shell does the same job, but each understand a different command syntax and provides
different built-in functions.
In MS-DOS, Shell name is COMMAND.COM which is also used for same purpose, but it's not as powerful
as our Linux Shells are!
Any of the above shell reads command from user (via Keyboard or Mouse) and tells Linux Os what users
want. If we are giving commands from keyboard it is called command line interface ( Usually in-front of $
prompt, This prompt is depend upon your shell and Environment that you set or by your System
Administrator, therefore you may get different prompt ).
Tip: To find your current shell type following command
$ echo $SHELL

Components of Linux System


Linux Operating System has primarily three components
 Kernel − Kernel is the core part of Linux. It is responsible for all major activities of this operating
system. It consists of various modules and it interacts directly with the underlying hardware. Kernel
provides the required abstraction to hide low level hardware details to system or application
programs.
 System Library − System libraries are special functions or programs using which application
programs or system utilities accesses Kernel's features. These libraries implement most of the
functionalities of the operating system and do not requires kernel module's code access rights.
 System Utility − System Utility programs are responsible to do specialized, individual level tasks.

Kernel Mode vs User Mode


Kernel component code executes in a special privileged mode called kernel mode with full access to all
resources of the computer. This code represents a single process, executes in single address space and do not
require any context switch and hence is very efficient and fast. Kernel runs each processes and provides
system services to processes, provides protected access to hardware to processes.
Support code which is not required to run in kernel mode is in System Library. User programs and other
system programs works in User Mode which has no access to system hardware and kernel code. User
programs/ utilities use System libraries to access Kernel functions to get system's low level tasks.
Basic Features
Following are some of the important features of Linux Operating System.
 Portable − Portability means software can works on different types of hardware in same way. Linux
kernel and application programs supports their installation on any kind of hardware platform.

8
II B.C.A.-‘C’ (2016-2019) KGCAS
 Open Source − Linux source code is freely available and it is community based development
project. Multiple teams work in collaboration to enhance the capability of Linux operating system
and it is continuously evolving.
 Multi-User − Linux is a multiuser system means multiple users can access system resources like
memory/ ram/ application programs at same time.
 Multiprogramming − Linux is a multiprogramming system means multiple applications can run at
same time.
 Hierarchical File System − Linux provides a standard file structure in which system files/ user files
are arranged.
 Shell − Linux provides a special interpreter program which can be used to execute commands of the
operating system. It can be used to do various types of operations, call application programs. etc.
 Security − Linux provides user security using authentication features like password protection/
controlled access to specific files/ encryption of data.
Architecture
The following illustration shows the architecture of a Linux system −

The architecture of a Linux System consists of the following layers −


 Hardware layer − Hardware consists of all peripheral devices (RAM/ HDD/ CPU etc).
 Kernel − It is the core component of Operating System, interacts directly with hardware, provides
low level services to upper layer components.
 Shell − An interface to kernel, hiding complexity of kernel's functions from users. The shell takes
commands from the user and executes kernel's functions.
 Utilities − Utility programs that provide the user most of the functionalities of an operating systems.

Linux History
Evolution of Computer
In earlier days, computers were as big as houses or parks. So you can imagine how difficult it was to operate
them. Moreover, every computer has a different operating system which made it completely worse to
operate on them. Every software was designed for a specific purpose and was unable to operate on other
computer. It was extremely costly and normal people neither can afford it nor can understand it.
Evolution of Unix
In 1969, a team of developers of Bell Labs started a project to make a common software for all the
computers and named it as 'Unix'. It was simple and elegant, used 'C' language instead of assembly language
9
II B.C.A.-‘C’ (2016-2019) KGCAS
and its code was recyclable. As it was recyclable, a part of its code now commonly called 'kernel' was used
to develop the operating system and other functions and could be used on different systems. Also its source
code was open source.
Initially, Unix was only found in large organizations like government, university, or larger financial
corporations with mainframes and minicomputers (PC is a microcomputer).
Unix Expansion
In eighties, many organizations like IBM, HP and dozen other companies started creating their own Unix. It
result in a mess of Unix dialects. Then in 1983, Richard Stallman developed GNU project with the goal to
make it freely available Unix like operating system and to be used by everyone. But his project failed in
gaining popularity. Many other Unix like operating system came into existence but none of them was able to
gain popularity.
Evolution of Linux
In 1991, Linus Torvalds a student at the university of Helsinki, Finland, thought to have a freely available
academic version of Unix started writing its own code. Later this project became the Linux kernel. He wrote
this program specially for his own PC as he wanted to use Unix 386 Intel computer but couldn't afford it. He
did it on MINIX using GNU C compiler. GNU C compiler is still the main choice to compile Linux code but
other compilers are also used like Intel C compiler.
He started it just for fun but ended up with such a large project. Firstly he wanted to name it as 'Freax' but
later it became 'Linux'.
He published the Linux kernel under his own license and was restricted to use as commercially. Linux uses
most of its tools from GNU software and are under GNU copyright. In 1992, he released the kernel under
GNU General Public License.

Linux Today
Today, supercomputers, smart phones, desktop, web servers, tablet, laptops and home appliances like
washing machines, DVD players, routers, modems, cars, refrigerators, etc use Linux OS.

Linux Features
 Multiuser capability: Multiple users can access the same system resources like memory, hard disk,
etc. But they have to use different terminals to operate.
 Multitasking: More than one function can be performed simultaneously by dividing the CPU time
intelligently.
 Portability: Portability doesn't mean it is smaller in file size or can be carried in pen drives or
memory cards. It means that it support different types of hardware.
 Security: It provides security in three ways namely authenticating (by assigning password and login
ID), authorization (by assigning permission to read, write and execute) and encryption (converts file
into an unreadable format).
 Live CD/USB: Almost all Linux distros provide live CD/USB so that users can run/try it without
installing it.
 Graphical User Interface (X Window system): Linux is command line based OS but it can be
converted to GUI based by installing packages.
 Support's customized keyboard: As it is used worldwide, hence supports different languages
keyboards.
 Application support: It has its own software repository from where users can download and install
many applications.
 File System: Provides hierarchical file system in which files and directories are arranged.
 Open Source: Linux code is freely available to all and is a community based development project.

10
II B.C.A.-‘C’ (2016-2019) KGCAS
Why Use Linux
Linux is completely different from other operating systems in many ways.
 It is an open source OS which gives a great advantage to the programmers as they can design their
own custom operating systems.
 It gives you a lot of option of programs having some different features so you can choose according
to your need.
 A global development community look at different ways to enhance its security, hence it is highly
secured and robust so you don't need an anti virus to scan it regularly. Companies like Google,
Amazon and Facebook use linux in order to protect their servers as it is highly reliable and stable.
 Above all you don't have to pay for software and server licensing to install Linux, its absolutely free
and you can install it on as many computers as you want.
 Its completely trouble free operating system and don't have an issue with viruses, malware and
slowing down your computer.

Linux Distributions (Distros)


Other operating systems like Microsoft combine each bit of codes internally and release it as a single
package. You have to choose from one of the version they offer.
But Linux is different from them. Different parts of Linux are developed by different organizations.
Different parts include kernel, shell utilities, X server, system environment, graphical programs, etc. If you
want you can access the codes of all these parts and assemble them yourself. But its not an easy task seeking
a lot of time and all the parts has to be assembled correctly in order to work properly.
From here on distribution (also called as distros) comes into the picture. They assemble all these parts for us
and give us a compiled operating system of Linux to install and use.

Linux Distributions List


There are on an average six hundred Linux distributors providing different features. Here, we'll discuss
about some of the popular Linux distros today.
1) Ubuntu
It came into existence in 2004 by Canonical and quickly became popular. Canonical wants Ubuntu to be
used as easy graphical Linux desktop without the use of command line. It is the most well known Linux
distribution. Ubuntu is a next version of Debian and easy to use for newbies. It comes with a lots of pre-
installed apps and easy to use repositories libraries.
Earlier, Ubuntu uses GNOME2 desktop environment but now it has developed its own unity desktop
environment. It releases every six months and currently working to expand to run on tablets and
smartphones.
2) Linux Mint
Mint is based on Ubuntu and uses its repository software so some packages are common in both.
Earlier it was an alternative of Ubuntu because media codecs and proprietary software are included in mint
but was absent in Ubuntu. But now it has its own popularity and it uses cinnamon and mate desktop instead
of Ubuntu's unity desktop environment.
3) Debian
Debian has its existence since 1993 and releases its versions much slowly then Ubuntu and mint.
This makes it one of the most stable Linux distributor.
Ubuntu is based on Debian and was founded to improve the core bits of Debian more quickly and make it
more user friendly. Every release name of Debian is based on the name of the movie Toy Story.

11
II B.C.A.-‘C’ (2016-2019) KGCAS
4) Red Hat Enterprise / CentOS
Red hat is a commercial Linux distributor. There products are red hat enterprise Linux (RHEL) and Fedora
which are freely available. RHEL is well tested before release and supported till seven years after the
release, whereas, fedora provides faster update and without any support.
Red hat uses trademark law to prevent their software from being redistributed. CentOS is a community
project that uses red hat enterprise Linux code but removes all its trademark and make it freely available. In
other words, it is a free version of RHEL and provide a stable platform for a long time.
5) Fedora
It is a project that mainly focuses on free software and provides latest version of software. It doesn't make its
own desktop environment but used 'upstream' software. By default it has GNOME3 desktop environment. It
is less stable but provides the latest stuff.

Choosing a Linux Distro


Distribution Why To Use
UBuntu It works like Mac OS and easy to use.
Linux mint It works like windows and should be use by new comers.
Debian It provides stability but not recommended to a new user.
Fedora If you want to use red hat and latest software.
Red hat enterprise To be used commercially.
CentOS If you want to use red hat but without its trademark.
OpenSUSE It works same as Fedora but slightly older and more stable.
Arch Linux It is not for the beginners because every package has to be installed by yourself.

Unix Vs Linux
Unix OS
Unix is a proprietary software operating system. As said above, it was developed by Bell Labs for its own
use but over the years was licensed to other tech companies. The Unix OS works primarily on Command
Line Interface, though, recently, there have been developments for GUI on Unix systems.
Unix is not free. Different flavors of Unix have different cost structures according to vendors.
Unix is not as flexible as Linux. It has less compatibility with different types of hardware. Unix installation
requires a strict and well-defined hardware machinery and works only on specific CPU machines.
Since Unix OS is not a freeware and therefore its source code is not available. You cannot modify and sell
your version of Unix therefore.
Unix installation is comparatively much costlier than Linux since it requires special hardware and can be run
only on specific CPU processors. It is mostly used in big data servers around the world.
Unix comparatively supports very fewer File systems. It supports the following File systems:
ZFS, JS, HFX, GPS, XFS, GPS, XFS, VXFS
The Unix Operating System is not portable. There are comparatively less distributions or versions of Unix
operating systems.
Different versions of Unix are as follows:
 AIS (IBM)
 BSD
 HP – UX
 Solaris
 Iris

12
II B.C.A.-‘C’ (2016-2019) KGCAS
The Unix OS is mainly used on large server systems, mainframes, expensive and high-end computer
systems at big MNCs and institutions. Unix is being developed, maintained and updated by AT&T
developers. They don‘t encourage open source developments.

Linux operating system


Most gadgets that you can think of except your laptop or PC run on some Linux distro. Linux is an open
source operating system based on Unix. Linux is basically the name of the Kernel and was developed in
1991. The Linux kernel is developed by the community and Linus Torvalds oversees things. In addition to
the command line interface, Linux has a graphical user interface like Windows operating system and
applications to make it a complete operating system.
The Linux OS has both Graphical User Interface (GUI) as well as Command Line Interface (CLI). Linux
comes with KDE and Gnome as its GUI environment. The Command Line Interface is optional in Linux.
Linux can be freely distributed, downloaded freely, distributed through magazines, Books etc. There are
priced distros for Linux like Red Hat Linux also, but they are normally cheaper than Windows.
Linux is a very flexible operating system and is compatible with most hardware systems. Linux OS can be
installed and executed on almost anything that has a processor. Since it is so fluid and agile, Linux can be
installed on a wide variety of computer hardware, ranging from mobile phones, tablets, and video game
consoles, to mainframes and supercomputers.
Linux OS being an open source OS, its source code is available for free. You can, therefore, read, modify
and implement it on your machine. However, you cannot sell it as it comes under the GNU GPL License.
Linux is highly scalable and supports a really large set of file systems. Linux installation is generally very
economical as compared to Unix since it does not require special hardware for it to run and the operating
system itself is either free or very inexpensive.
The File systems supported by Linux are as follows:
XFS, RAMFS, NFS, VFAT, CRAMFSM EXT3, EXT4, EXT2, EXT1, UFS, AUTOFS, DEVPTS, NTFS AND
MANY MORE.
There are comparatively more distributions or versions of Linux operating systems. The distributions
available for Linux are as follows:
 Redhat
 Kali
 Slackware
 Debian
 ArchLinux
 Solaris
 Ubuntu
 CentOS
 Fedora
The Linux Kernel was created by Linux Torvalds in 1990s. However, the complete OS with GNU GPL
license also had a huge contribution by Richard Stallman. Inspired by MINIX (a Unix-like system) and
eventually after adding many features of GUI, Drivers etc, Linus Torvalds developed the framework of the
OS that became LINUX in 1992. The LINUX kernel was released on 17th September, 1991.
The Linux OS mainly focused on home-based PCs and laptops. However, due to its popularity and stability,
it started being used in offices and high-end systems including mobile phones, servers, and even embedded
systems.
The developments in Linux are contributed by a community of developers worldwide without any cost.
However, the authority to implement the development lies with the founder of Linux – Linus Torvalds who
has been releasing updates to its Kernels.

13
II B.C.A.-‘C’ (2016-2019) KGCAS
BASH (Bourne Again SHell) is the Linux default shell. It can support multiple command interpreters. Linux
is considered to be very secure. Linux has had about 60-100 viruses listed till date, however, none of them
actively spreading nowadays.
Linux was originally developed for Intel‘s x86 hardware. However, now the ports are available for over two
dozen CPU types including ARM.

Unix vs Linux – Difference between Unix and Linux


The Source Code of Linux is freely available to its The Source Code of Unix is not available for
Users. the general public.
Linux primarily uses Graphical User Interface with an
Unix primarily uses Command Line Interface.
optional Command Line Interface.
Linux OS is portable and can be executed in different
Unix is not portable.
Hard Drives.
Unix has a rigid requirement of the Hardware.
Linux is very flexible and can be installed on most of the
Hence, cannot be installed on every other
Home Based Pcs.
machine.
Linux is mainly used in Home Based PC, Mobile Unix is mainly used in Server Systems,
Phones, Desktops, etc. Mainframes and High End Computers.
Different Versions of Linux are: Ubuntu, Debian, Different Versions of Unix are: AIS, HP-UX,
OpenSuse, Redhat, Solaris, etc. BSD, Iris, etc.
Linux Installation is economical and doesn‘t require Unix Installation is comparatively costlier as
much specific and high end hardware. it requires more specific hardware circuitry.
The Filesystems supported by Linux are as follows: xfs,
The Filesystems supported by Unix are as
ramfs, nfs, vfat, cramfsm ext3, ext4, ext2, ext1, ufs,
follows: zfs, js, hfx, gps, xfs, gps, xfs, vxfs.
autofs, devpts, ntfs
Linux is development by an active Linux Community
Unix is developed by AT&T Developers.
worldwide.

Example of Unix : Solaris

Now that you have a good idea of the general differences between Linux and Unix, let‘s take a look at some
more specific examples. First, let‘s compare Solaris, a Unix distro made by Oracle (formerly made by Sun
Microsystems), with Linux. Linux is more portable, meaning that it can run on more system architectures
(think x86 and ARM) than Solaris can. Solaris is known for better stability and hardware integration, but
Linux has a vast arrray distros to manage even the complex hardware. Linux also has a much faster rate of
development than Solaris.
There are also several other differences between them, but this can occur even among different Linux
distributions. For example, they use different package managers, different default file systems, and more.
There are also various differences in the respective kernels on how they deal with things such as I/O and
network, but those differences are extremely technical.
14
II B.C.A.-‘C’ (2016-2019) KGCAS
Usage of Linux and Unix
Linux OS is great for small- to medium-sized operations, and today it is also used in large enterprises where
UNIX was considered previously as the only option. A few years back, Linux was considered as an
interesting academic project, but most big enterprises where networking and multiple user computing are the
main concerns; people didn‘t consider Linux as an option. But today, with major software vendors porting
their applications to Linux, and as it can be freely distributed, the OS has entered the mainstream as a viable
option for Web serving and office applications.
But there are some circumstances where UNIX is the obvious choice, or used to be. If an enterprise used
massive symmetric multiprocessing systems, or systems with more than eight CPUs, they needed to run
UNIX in the past. UNIX was far more capable in handling all the processes more effectively than Linux.
However since 2004 more of the world‘s biggest supercomputers now run Linux than unix. Since 2011
Linux powers over 90% of the top 500 servers. It runs also on the biggest (as of 2011): RIKEN Advanced
Institute for Computational Science Cores: 705024 Power: 12659.89 kW Memory: 1410048 GB

Linux vs Windows
Linux Operating System
It is an operating system assembled under the model of open source software development and mostly used
for server purpose. It supports a dozen of programming languages like C,C++,JAVA,PHP and many more.

Advantages:
 Low cost: You need not spend much money to obtain license as its softwares come from GNU General
Public License. You can also download high quality software whenever you want, free of cost and you
need not worry that your program may stop due to trail version. You can also install it in many
computers without paying.
 Performance: Linux provides high performance on workstations and on networks. It also helps in
making old computers sufficient and usable again and also can handle many users at a time.
 Stability: You don‘t have to reboot periodically to maintain performance. It can handle large number of
users and does not hang up or slow down due to memory issues. Continuous up time upto a year or so is
common.
 Flexibility: It is used for high performance applications, desktop applications and embedded
applications. You can save disk space by installing components required for a particular use. You can
restrict specific computers instead of all computers.
 Security: The security aspect of the linux is very strong as it is very secure and it is less prominent to
viruses, even if there is an attack there would be immediate step taken by the developers all over the
world to resolve it.
 Choice: Choice is one of the greatest advantage of Linux. It gives the power to control every aspect of
the operating system. Main features that you have control is look and feel of desktop by Windows
Manager and kernel.
Disadvantages:
 Understanding: To become familiar with Linux you need to have a lot of patience and desire to read
and explore about it.
 Software: Linux has a limited selection of available softwares.
15
II B.C.A.-‘C’ (2016-2019) KGCAS
 Ease: Even though Linux has improved a lot in ease of use but windows is much easier.
 Hardware: Linux doesnot support many hardware devices.

Windows Operating System:

It is family of operating system from Microsoft. If programming languages such as Visual Basic.net,
ASP.net, Visual C#, Visual C++ are used then it is better to opt windows hosting.
Advantages:
 Ease: Microsoft Windows has made much advancement and changes which made it easy to use the
operating system. Even though it is not the easiest, it is easier than linux.
 Software: Since there are more number of Microsoft users there are more software programs, games
and utilities for windows. All most all games are compatible to windows, some CPU intensive and
graphic intensive games are also supported.
 Hardware: All hardware manufacturers will support Microsoft windows. Due to large number of
Microsoft users and broader driver, all the hardware devices are supported.
 Front Page Extension: When using a popular web design program having windows hosting makes it
lot more easier. You don‘t have to worry if it supported or not.
 Development: If you plan to develop windows based applications then windows platform is most
suggested as linux does not support windows applications.

Disadvantage:
 Price: Microsoft windows is costly compared to Linux as each license costs between $50.00-$100.00.
 Security: When compared to linux it is much more prone to viruses and other attacks.
 Reliability: It needs to be rebooted periodically else there is a possibility of hang up of the system.
 Software Cost: Even though the windows have softwares,games for free most of the programs will cost
more than $200.
Conclusion: Both Windows and Linux Hosting have advantages and disadvantages. Based on your
requirements you need to opt which hosting suits better.

UNIT – II
Managing Files and Directories: Introduction Directory Commands in LINUX File Commands in
LINUX.
A directory is a file the solo job of which is to store the file names and the related information. All
the files, whether ordinary, special, or directory, are contained in directories.
Unix uses a hierarchical structure for organizing files and directories. This structure is often referred to as a
directory tree. The tree has a single root node, the slash character (/), and all other directories are contained
below it.
Home Directory
The directory in which you find yourself when you first login is called your home directory.

16
II B.C.A.-‘C’ (2016-2019) KGCAS

You will be doing much of your work in your home directory and subdirectories that you'll be creating to
organize your files.
You can go in your home directory anytime using the following command −
$cd ~
$
Here ~ indicates the home directory. Suppose you have to go in any other user's home directory, use the
following command −
$cd ~username
$
To go in your last directory, you can use the following command −
$cd -
$

Absolute/Relative Pathnames
Directories are arranged in a hierarchy with root (/) at the top. The position of any file within the hierarchy
is described by its pathname.
Elements of a pathname are separated by a /. A pathname is absolute, if it is described in relation to root,
thus absolute pathnames always begin with a /.
Following are some examples of absolute filenames.
/etc/passwd
/users/sjones/chem/notes
/dev/rdsk/Os3
A pathname can also be relative to your current working directory. Relative pathnames never begin with /.
Relative to user amrood's home directory, some pathnames might look like this −
chem/notes
personal/res
To determine where you are within the filesystem hierarchy at any time, enter the command pwd to print
the current working directory −
$pwd
/user0/home/amrood

Listing Directories
To list the files in a directory, you can use the following syntax −
$ls dirname
Following is the example to list all the files contained in /usr/local directory −
$ls /usr/local

X11 bin gimp jikes sbin


ace doc include lib share

17
II B.C.A.-‘C’ (2016-2019) KGCAS

atalk etc info man ami

Creating Directories
We will now understand how to create directories. Directories are created by the following command −
$mkdir dirname
Here, directory is the absolute or relative pathname of the directory you want to create. For example, the
command −
$mkdir mydir
$
Creates the directory mydir in the current directory. Here is another example −
$mkdir /tmp/test-dir
$
This command creates the directory test-dir in the /tmp directory. The mkdircommand produces no output
if it successfully creates the requested directory.
If you give more than one directory on the command line, mkdir creates each of the directories. For
example, −
$mkdir docs pub
$
Creates the directories docs and pub under the current directory.
Creating Parent Directories
We will now understand how to create parent directories. Sometimes when you want to create a directory,
its parent directory or directories might not exist. In this case, mkdir issues an error message as follows −
$mkdir /tmp/amrood/test
mkdir: Failed to make directory "/tmp/amrood/test";
No such file or directory
$
In such cases, you can specify the -p option to the mkdir command. It creates all the necessary directories
for you. For example −
$mkdir -p /tmp/amrood/test
$
The above command creates all the required parent directories.
Removing Directories
Directories can be deleted using the rmdir command as follows −
$rmdir dirname
$
Note − To remove a directory, make sure it is empty which means there should not be any file or sub-
directory inside this directory.
You can remove multiple directories at a time as follows −
$rmdir dirname1 dirname2 dirname3

18
II B.C.A.-‘C’ (2016-2019) KGCAS

$
The above command removes the directories dirname1, dirname2, and dirname3, if they are empty.
The rmdir command produces no output if it is successful.
Changing Directories
You can use the cd command to do more than just change to a home directory. You can use it to change to
any directory by specifying a valid absolute or relative path. The syntax is as given below −
$cd dirname
$
Here, dirname is the name of the directory that you want to change to. For example, the command −
$cd /usr/local/bin
$
Changes to the directory /usr/local/bin. From this directory, you can cd to the
directory /usr/home/amrood using the following relative path −
$cd ../../home/amrood
$

Renaming Directories
The mv (move) command can also be used to rename a directory. The syntax is as follows −
$mv olddir newdir
$
You can rename a directory mydir to yourdir as follows −
$mv mydir yourdir
$

The directories . (dot) and .. (dot dot)


The filename . (dot) represents the current working directory; and thefilename .. (dot dot) represents the
directory one level above the current working directory, often referred to as the parent directory.
If we enter the command to show a listing of the current working directories/files and use the -a option to
list all the files and the -l option to provide the long listing, we will receive the following result.
$ls -la
drwxrwxr-x 4 teacher class 2048 Jul 16 17.56 .
drwxr-xr-x 60 root 1536 Jul 13 14:18 ..
---------- 1 teacher class 4210 May 1 08:27 .profile
-rwxr-xr-x 1 teacher class 1948 May 12 13:42 memo

Directory Related Commands


Command Summary Use

cd Use cd to change directories. Type cd followed by the name of a directory


to access that directory. Keep in mind that you are always in a directory
and can navigate to directories hierarchically above or below. Ex:
cd games

19
II B.C.A.-‘C’ (2016-2019) KGCAS
If the directory games is not located hierarchically below the current
directory, then the complete path must be
written out. Ex:
cd /usr/games
To move up one directory, use the shortcut command. Ex:
cd ..
Use cp -r to copy a directory and all of its contents
Type cp -r followed by the name of an existing directory and the name of
the new directory. Ex:
cp -r testing newdir
You must include the -r or you‘ll see the following message:
cp: testing is a directory and -r not specified.
This command saves you time if you need to make a mirror image of a
directory packed with files.
mkdir Use mkdir to make/create a brand new directory
Type mkdir followed by the name of a directory. Ex:
mkdir testdir
mv Use mv to change the name of a directory
Type mv followed by the current name of a directory and the new name of
the directory. Ex:
mv testdir newnamedir
Trying to find out where on your Linux server you currently are located?
pwd The pwd (print working directory) command will show you the full path to
the directory you are currently in. This is very handy to use, especially
when performing some of the other commands on this page!
rmdir Use rmdir to remove an existing directory (assuming you have permissions
set to allow this).
Type rmdir followed by a directory's name to remove it. Ex:
rmdir testdir
You CAN'T remove a directory that contains files with this command.
Instead to delete non-empty directories in Linux, use the more useful
command:rm -r
This command removes directories and files within the directories. Read
more details about this in Commands for Beginning Admins
The rmdir command is used mostly to remove empty directories. If you
have a desire to use this command then you'll need to delete or move the
files before attempting to remove a full directory. For more help please
read the mv command and also File Related Commands.

File Commands in Linux


Listing files (ls)
If you want to see the list of files on your UNIX or Linux system, use the 'ls' command.
It shows the files /directories in your current directory.

Note:
 Directories are denoted in blue color.
20
II B.C.A.-‘C’ (2016-2019) KGCAS
 Files are denoted in white.
 You will find similar color schemes in different flavors of Linux.

Suppose, your "Music" folder has following sub-directories and files.

You can use 'ls -R' to shows all the files not only in directories but also subdirectories

NOTE: The command is case-sensitive. If you enter, "ls - r" you will get an error.
'ls -al' gives detailed information of the files. The command provides information in a columnar format. The
columns contain the following information:
1st Column File type and access permissions

2nd Column # of HardLinks to the File

3rd Column Owner and the creator of the file

4th Column Group of the owner

5th Column File size in Bytes

6th Column Date and Time

7th Column Directory or File name


Let's see an example -

21
II B.C.A.-‘C’ (2016-2019) KGCAS
Listing Hidden Files
Hidden items in UNIX/Linux begin with - at the start, of the file or
directory.

Any Directory/file starting with a '.' will not be seen unless you request for it. To view hidden files, use the
command.

ls -a

Creating & Viewing Files


The 'cat' command is used to display text files. It can also be used for copying, combining and creating new
text files. Let's see how it works.

To create a new file, use the command


1. cat > filename
2. Add content
3. Press 'ctrl + d' to return to command prompt.

To view a file, use the command -


cat filename
Let's see the file we just created -

Let's see another file sample2

The syntax to combine 2 files is -


cat file1 file2 > newfilename
Let's combine sample 1 and sample 2.

As soon as you insert this command and hit enter, the files are concatenated, but you do not see a result. This
is because Bash Shell (Terminal) is silent type. It will never give you a confirmation message like "OK"
or "Command Successfully Executed". It will only show a message when something goes wrong or when an
error has occurred.
22
II B.C.A.-‘C’ (2016-2019) KGCAS
To view the new combo file "sample" use the command
cat sample

Note: Only text files can be displayed and combined using this command.

Deleting Files
The 'rm' command removes files from the system without confirmation.
To remove a file use syntax -
rm filename

Moving and Re-naming files


To move a file, use the command.
mv filename new_file_location
Suppose we want to move the file "sample2" to location /home/guru99/Documents. Executing the command
mv sample2 /home/guru99/Documents

mv command needs super user permission. Currently, we are executing the command as a standard user.
Hence we get the above error. To overcome the error use command.
sudo command_you_want_to_execute
Sudo program allows regular users to run programs with the security privileges of the superuser or root.
Sudo command will ask for password authentication. Though, you do not need to know the root password.
You can supply your own password. After authentication, the system will invoke the requested command.
Sudo maintains a log of each command run. System administrators can trackback the person responsible for
undesirable changes in the system.

For renaming file:


mv filename newfilename

NOTE: By default, the password you entered for sudo is retained for 15 minutes per terminal. This
eliminates the need of entering the password time and again.
You only need root/sudo privileges, only if the command involves files or directories not owned by the user
or group running the commands

23
II B.C.A.-‘C’ (2016-2019) KGCAS
Directory Manipulations

Enough with File manipulations! Let's learn some directory commands.


Creating Directories
Directories can be created on a Linux operating system using the following command
mkdir directoryname
This command will create a subdirectory in your present working directory, which is usually your "Home
Directory".

For example,
mkdir mydirectory

If you want to create a directory in a different location other than 'Home directory', you could use the
following command -
mkdir
For example:
mkdir /tmp/MUSIC
will create a directory 'Music' under '/tmp' directory

You can also create more than one directory at a time.

Removing Directories
To remove a directory, use the command -
rmdir directoryname
Example
rmdir mydirectory
will delete the directory mydirectory

Tip: Ensure that there is no file / sub-directory under the directory that you want to delete. Delete the
files/sub-directory first before deleting the parent directory.

24
II B.C.A.-‘C’ (2016-2019) KGCAS
Renaming Directory
The 'mv' (move) command (covered earlier) can also be used for renaming directories. Use the below-given
format:
mv directoryname newdirectoryname
Let us try it:

Other Important Commands


The 'Man' command
Man stands for manual which is a reference book of a Linux operating system. It is similar to HELP file
found in popular software.

To get help on any command that you do not understand, you can type
man
The terminal would open the manual page for that command.
For an example, if we type man man and hit enter; terminal would give us information on man command

The History Command


History command shows all the commands that you have used in the past for the current terminal session.
This can help you refer to the old commands you have entered and re-used them in your operations again.

The clear command


This command clears all the clutter on the terminal and gives you a clean window to work on, just like when
you launch the terminal.

25
II B.C.A.-‘C’ (2016-2019) KGCAS

Pasting commands into the terminal


Many times you would have to type in long commands on the Terminal. Well, it can be annoying at times,
and if you want to avoid such a situation then copy, pasting the commands can come to rescue.
For copying, the text from a source, you would use Ctrl + c, but for pasting it on the Terminal, you need to
use Ctrl + Shift + p. You can also try Shift + Insert or select Edit>Paste on the menu
NOTE: With Linux upgrades, these shortcuts keep changing. You can set your preferred shortcuts via
Terminal> Edit> Keyboard Shortcuts.

Printing in Unix/Linux

Let's try out some easy commands which can print files in a format you want. What more, your original file
does not get affected at all by the formatting that you do. Let us learn about these commands and their use.

'pr' command
This command helps in formatting the file for printing on the terminal. There are many options available
with this command which help in making desired format changes on file. The most used 'pr' options are
listed below.

Option Function

-x Divides the data into 'x' columns

-h "header" Assigns "header" value as the report header

-t Does not print the header and top/bottom margins

-d Double spaces the output file

-n Denotes all line with numbers

-l page length Defines the lines (page length) in a page. Default is 56

26
II B.C.A.-‘C’ (2016-2019) KGCAS

Option Function

-o margin Formats the page by the margin number


Let us try some of the options and study their effects.
Dividing data into columns
'Tools' is a file (shown below).

We want its content to be arranged in three columns. The syntax for the same would be:
pr -x Filename
The '-x' option with the 'pr' command divides the data into x columns.

Assigning a header
The syntax is:
pr -h "Header" Filename
The '-h' options assigns "header" value as the report header.

As shown above, we have arranged the file in 3 columns and assigned a header

Denoting all lines with numbers


The syntax is:
pr -n Filename
This command denotes all the lines in the file with numbers.

These are some of the 'pr' command options that you can use to modify the file format.
27
II B.C.A.-‘C’ (2016-2019) KGCAS
Printing a file
Once you are done with the formatting, and it is time for you to get a hard copy of the file, you need to
use the following command:
lp Filename
or
lpr Filename
In case you want to print multiple copies of the file, you can use the number modifier.

In case you have multiple printers configured, you can specify a particular printer using the Printer modifier

Cheat Sheet
Below is a Cheat Sheet of Linux commands we have learned in this tutorial
Command Description

ls Lists all files and directories in the present working directory

ls - R Lists files in sub-directories as well

ls - a Lists hidden files as well

ls - al Lists files and directories with detailed information like permissions, size,
owner, etc.

cat > filename Creates a new file

cat filename Displays the file content

cat file file2 > file3 Joins two files (file1, file2) and stores the output in a new file (file3)

mv file "new file path" Moves the files to the new location

mv filename new_file_name Renames the file to a new filename

sudo Allows regular users to run programs with the security privileges of the
superuser or root

rm Deletes a file

man Gives help information on a command

history Gives a list of all past commands typed in the current terminal session

clear Clears the terminal


28
II B.C.A.-‘C’ (2016-2019) KGCAS

mkdir Creates a new directory in the present working directory

mkdir Create a new directory at the specified path

rmdir Deletes a directory

mv Renames a directory

pr -x Divides the file into x columns

pr -h Assigns a header to the file

pr -n Denotes the file with Line Numbers

lp -nc Prints "c" copies of the File


lpr c

lp -d lp -P Specifies name of the printer

apt-get Command used to install and update packages

mail -s 'subject' -c 'cc-address' - Command to send email


b 'bcc-address' 'to-address'

Mail -s "Subject" to-address < Command to send email with attachment


Filename

UNIT III
Creating files using the vi editor: Text editors The vi editor. Managing Documents: Locating files in
LINUX Standard files Redirection Filters Pipes.

Editing files using the screen-oriented text editor vi is one of the best ways. This editor enables you
to edit lines in context with other lines in the file.
An improved version of the vi editor which is called the VIM has also been made available now. Here,
VIM stands for Vi IMproved.
vi is generally considered the de facto standard in Unix editors because −
 It's usually available on all the flavors of Unix system.
 Its implementations are very similar across the board.
 It requires very few resources.
 It is more user-friendly than other editors such as the ed or the ex.
You can use the vi editor to edit an existing file or to create a new file from scratch. You can also use this
editor to just read a text file.
Starting the vi Editor
The following table lists out the basic commands to use the vi editor −
S.No. Command & Description

1 vi filename
Creates a new file if it already does not exist, otherwise opens an existing file.

2 vi -R filename
Opens an existing file in the read-only mode.

3 view filename
Opens an existing file in the read-only mode.
29
II B.C.A.-‘C’ (2016-2019) KGCAS
Following is an example to create a new file testfile if it already does not exist in the current working
directory −
$vi testfile
The above command will generate the following output −
|
~
~
~
~
~
~
~
~
~
~
~
~
"testfile" [New File]
You will notice a tilde (~) on each line following the cursor. A tilde represents an unused line. If a line
does not begin with a tilde and appears to be blank, there is a space, tab, newline, or some other non-
viewable character present.
You now have one open file to start working on. Before proceeding further, let us understand a few
important concepts.
Operation Modes
While working with the vi editor, we usually come across the following two modes −
 Command mode − This mode enables you to perform administrative tasks such as saving the files,
executing the commands, moving the cursor, cutting (yanking) and pasting the lines or words, as
well as finding and replacing. In this mode, whatever you type is interpreted as a command.
 Insert mode − This mode enables you to insert text into the file. Everything that's typed in this
mode is interpreted as input and placed in the file.
vi always starts in the command mode. To enter text, you must be in the insert mode for which simply
type i. To come out of the insert mode, press theEsc key, which will take you back to the command mode.
Hint − If you are not sure which mode you are in, press the Esc key twice; this will take you to the
command mode. You open a file using the vi editor. Start by typing some characters and then come to the
command mode to understand the difference.
Getting Out of vi
The command to quit out of vi is :q. Once in the command mode, type colon, and 'q', followed by return. If
your file has been modified in any way, the editor will warn you of this, and not let you quit. To ignore this
message, the command to quit out of vi without saving is :q!. This lets you exit vi without saving any of the
changes.
The command to save the contents of the editor is :w. You can combine the above command with the quit
command, or use :wq and return.
The easiest way to save your changes and exit vi is with the ZZ command. When you are in the command
mode, type ZZ. The ZZ command works the same way as the :wq command.
If you want to specify/state any particular name for the file, you can do so by specifying it after the :w. For
example, if you wanted to save the file you were working on as another filename called filename2, you
would type :w filename2 and return.
Moving within a File
To move around within a file without affecting your text, you must be in the command mode (press Esc
twice). The following table lists out a few commands you can use to move around one character at a time −
S.No. Command & Description

30
II B.C.A.-‘C’ (2016-2019) KGCAS

1 k
Moves the cursor up one line

2 j
Moves the cursor down one line

3 h
Moves the cursor to the left one character position

4 l
Moves the cursor to the right one character position
The following points need to be considered to move within a file −
 vi is case-sensitive. You need to pay attention to capitalization when using the commands.
 Most commands in vi can be prefaced by the number of times you want the action to occur. For
example, 2j moves the cursor two lines down the cursor location.
There are many other ways to move within a file in vi. Remember that you must be in the command mode
(press Esc twice). The following table lists out a few commands to move around the file −
Given below is the list of commands to move around the file.
Control Commands
The following commands can be used with the Control Key to performs functions as given in the table
below −
Given below is the list of control commands.
Editing Files
To edit the file, you need to be in the insert mode. There are many ways to enter the insert mode from the
command mode −
S.No. Command & Description

1 i
Inserts text before the current cursor location

2 I
Inserts text at the beginning of the current line

3 a
Inserts text after the current cursor location

4 A
Inserts text at the end of the current line

5 o
Creates a new line for text entry below the cursor location

6 O
Creates a new line for text entry above the cursor location

Deleting Characters
Here is a list of important commands, which can be used to delete characters and lines in an open file −
S.No. Command & Description

1 x
Deletes the character under the cursor location

2 X

31
II B.C.A.-‘C’ (2016-2019) KGCAS

Deletes the character before the cursor location

3 dw
Deletes from the current cursor location to the next word

4 d^
Deletes from the current cursor position to the beginning of the line

5 d$
Deletes from the current cursor position to the end of the line

6 D
Deletes from the cursor position to the end of the current line

7 dd
Deletes the line the cursor is on
As mentioned above, most commands in vi can be prefaced by the number of times you want the action to
occur. For example, 2x deletes two characters under the cursor location and 2dd deletes two lines the
cursor is on.
It is recommended that the commands are practiced before we proceed further.
Change Commands
You also have the capability to change characters, words, or lines in vi without deleting them. Here are the
relevant commands −
S.No. Command & Description

1 cc
Removes the contents of the line, leaving you in insert mode.

2 cw
Changes the word the cursor is on from the cursor to the lowercase wend of the
word.

3 r
Replaces the character under the cursor. vi returns to the command mode after the
replacement is entered.

4 R
Overwrites multiple characters beginning with the character currently under the
cursor. You must use Esc to stop the overwriting.

5 s
Replaces the current character with the character you type. Afterward, you are left
in the insert mode.

6 S
Deletes the line the cursor is on and replaces it with the new text. After the new
text is entered, vi remains in the insert mode.

Copy and Paste Commands


You can copy lines or words from one place and then you can paste them at another place using the
following commands −
S.No. Command & Description

32
II B.C.A.-‘C’ (2016-2019) KGCAS

1 yy
Copies the current line.

2 yw
Copies the current word from the character the lowercase w cursor is on, until the
end of the word.

3 p
Puts the copied text after the cursor.

4 P
Puts the yanked text before the cursor.

Advanced Commands
There are some advanced commands that simplify day-to-day editing and allow for more efficient use of vi

Given below is the list advanced commands.
Word and Character Searching
The vi editor has two kinds of searches: string and character. For a string search, the / and ? commands
are used. When you start these commands, the command just typed will be shown on the last line of the
screen, where you type the particular string to look for.
These two commands differ only in the direction where the search takes place −
 The / command searches forwards (downwards) in the file.
 The ? command searches backwards (upwards) in the file.
The n and N commands repeat the previous search command in the same or the opposite direction,
respectively. Some characters have special meanings. These characters must be preceded by a backslash (\)
to be included as part of the search expression.
S.No. Character &Description

1 ^
Searches at the beginning of the line (Use at the beginning of a search
expression).

2 .
Matches a single character.

3 *
Matches zero or more of the previous character.

4 $
End of the line (Use at the end of the search expression).

5 [
Starts a set of matching or non-matching expressions.

6 <
This is put in an expression escaped with the backslash to find the ending or the
beginning of a word.

7 >
This helps see the '<' character description above.
The character search searches within one line to find a character entered after the command.
The f and F commands search for a character on the current line only. f searches forwards and F searches
backwards and the cursor moves to the position of the found character.
33
II B.C.A.-‘C’ (2016-2019) KGCAS
The t and T commands search for a character on the current line only, but for t, the cursor moves to the
position before the character, and T searches the line backwards to the position after the character.
Set Commands
You can change the look and feel of your vi screen using the following :setcommands. Once you are in the
command mode, type :set followed by any of the following commands.
S.No. Command & Description

1 :set ic
Ignores the case when searching

2 :set ai
Sets autoindent

3 :set noai
Unsets autoindent

4 :set nu
Displays lines with line numbers on the left side

5 :set sw
Sets the width of a software tabstop. For example, you would set a shift width of 4
with this command — :set sw = 4

6 :set ws
If wrapscan is set, and the word is not found at the bottom of the file, it will try
searching for it at the beginning

7 :set wm
If this option has a value greater than zero, the editor will automatically "word
wrap". For example, to set the wrap margin to two characters, you would type
this: :set wm = 2

8 :set ro
Changes file type to "read only"

9 :set term
Prints terminal type

10 :set bf
Discards control characters from input

Running Commands
The vi has the capability to run commands from within the editor. To run a command, you only need to go
to the command mode and type :! command.
For example, if you want to check whether a file exists before you try to save your file with that filename,
you can type :! ls and you will see the output of lson the screen.
You can press any key (or the command's escape sequence) to return to your vi session.
Replacing Text
The substitution command (:s/) enables you to quickly replace words or groups of words within your files.
Following is the syntax to replace text −
:s/search/replace/g
The g stands for globally. The result of this command is that all occurrences on the cursor's line are
changed.

34
II B.C.A.-‘C’ (2016-2019) KGCAS
Important Points to Note
The following points will add to your success with vi −
 You must be in command mode to use the commands. (Press Esc twice at any time to ensure that
you are in command mode.)
 You must be careful with the commands. These are case-sensitive.
 You must be in insert mode to enter text.

Text Editors
A text editor is a must have application for any operating system. We have no dearth of best
modern editors for Linux. But those are GUI based editors.
As you know, the real power of Linux lies in the command line. And when you are working in
command line, you would need a text editor that could work right inside the terminal.
For that purpose, today we are going to make a list of best command line text editors for
Linux.
Vim
If you‘re on Linux for quite some time, you must have heard about Vim. Vim is an extensively
configurable, cross-platform and highly efficient text editor.
Almost every Linux distribution comes with Vim pre -installed. It is extremely popular for
its wide range of features.
Vim can be quite agonizing for first-time users. I remember the first time I tried to edit a text
file with Vim, I was completely puzzled. I couldn‘t type a single letter on it and the funny part
is, I couldn‘t even figure out how to close this thing. If you are going to use Vim, you have to be
determined for climbing up a very steep learning curve.
But after you have gone through all that, combed through some documentations, remembered its
commands and shortcuts you will find that the hassle was worth it. You can bend Vim to your
will – customizing its interface however it seems fit to you, give your workflow a boost by using
various user scripts, plugins and so on. Vim supports syntax highlighting, macro
recording and action history.
GNU Emacs
GNU Emacs is undoubtedly one of the most powerful text editor out there . If you have heard
about both Vim and Emacs, you should know that both of these editors have a very loyal fan -
base and often they are very serious about their text editor of choice. And you can find lots of
humor and stuff on the internet about it:
Emacs is cross-platform and has both command-line and graphical user interface. It is also very
rich with various features and, most importantly, extensible.
Just as Vim, Emacs too comes with a steep learning curve. But once you master it, you can
completely leverage its power. Emacs can handle just about any types of text files. The interface
is customizable to suit your workflow. It supports macro recording and shortcuts.
The unique power of Emacs is that it can be transformed into something completely differen t
from a text editor. There is a large collection of modules that can transform the application for
using in completely different scenarios, like – calendar, news reader, word processor etc. You
can even play games in Emacs!
Nano
When it comes to simplicity, Nano is the one. Unlike Vim or Emacs, the learning curve for nano
is almost flat.
If you want to simply create & edit a text file and get on with your life, look no further
than Nano.
The shortcuts available on Nano are displayed at the bottom of the user interface. Nano includes
only the basic functions of a text editor.
It is minimal and perfectly suitable for editing system & configuration files. For those who
doesn‘t need advanced features from a command-line text editor, Nano is the perfect match.

Vi Text Editor

35
II B.C.A.-‘C’ (2016-2019) KGCAS
What is the VI editor?
The VI editor is the most popular and classic text editor in the Linux family. Below, are some reasons which
make it a widely used editor –
 It is available in almost all Linux Distributions
 It works the same across different platforms and Distributions
 It is user-friendly. Hence, millions of Linux users love it and use it for their editing needs
Nowadays, there are advanced versions of the vi editor available, and the most popular one is VIMwhich
is Vi Improved. Some of the other ones are Elvis, Nvi, Nano, and Vile. It is wise to learn vi because it is
feature-rich and offers endless possibilities to edit a file.
To work on VI editor, you need to understand its operation modes. They can be divided into two main
parts.
Command mode:
 The vi editor opens in this mode, and it only understands commands
 In this mode, you can, move the cursor and cut, copy, paste the text
 This mode also saves the changes you have made to the file
 Commands are case sensitive. You should use the right letter case.
Insert mode:
 This mode is for inserting text in the file.
 You can switch to the Insert mode from the command mode by pressing 'i' on the keyboard
 Once you are in Insert mode, any key would be taken as an input for the file on which you are
currently working.
 To return to the command mode and save the changes you have made you need to press the Esc key

Starting the vi editor


To launch the VI Editor -Open the Terminal (CLI) and type
vi <filename_NEW> or <filename_EXISTING>
&If you specify an existing file, then the editor would open it for you to edit. Else, you can create a new file.

vi Editing commands
Note: You should be in the "command mode" to execute these commands. VI editor is case-sensitive so
make sure you type the commands in the right letter-case.
Keystrokes Action

i Insert at cursor (goes into insert mode)

a Write after cursor (goes into insert mode)


36
II B.C.A.-‘C’ (2016-2019) KGCAS

A Write at the end of line (goes into insert mode)

ESC Terminate insert mode

u Undo last change

U Undo all changes to the entire line

o Open a new line (goes into insert mode)

dd Delete line
3dd Delete 3 lines.

D Delete contents of line after the cursor

C Delete contents of a line after the cursor and insert new text. Press ESC key to end insertion.

dw Delete word
4dw Delete 4 words

cw Change word

x Delete character at the cursor

r Replace character

R Overwrite characters from cursor onward

s Substitute one character under cursor continue to insert

S Substitute entire line and begin to insert at the beginning of the line

~ Change case of individual character


Make sure you press the right command otherwise you will end up making undesirable changes to the file.
You can also enter the insert mode by pressing a, A, o, as required.
Moving within a file
You need to be in the command mode to move within a file. The default keys for navigation are mentioned
below else; You can also use the arrow keys on the keyboard.
Keystroke Use

k Move cursor up

j Move cursor down

h Move cursor left

l Move cursor right


Saving and Closing the file
You should be in the command mode to exit the editor and save changes to the file.
Keystroke Use

Shift+zz Save the file and quit

37
II B.C.A.-‘C’ (2016-2019) KGCAS

:w Save the file but keep it open

:q Quit without saving

:wq Save the file and quit

Locating Files in Linux

Most people use a graphical file manager to find files in Linux, such as Nautilus in Gnome, Dolphin in KDE,
and Thunar in Xfce. However, there are several ways to use the command line to find files in Linux, no
matter what desktop manager you use.

Using the Find Command


The ―find‖ command allows you to search for files for which you know the approximate filenames. The
simplest form of the command searches for files in the current directory and recursively through its
subdirectories that match the supplied search criteria. You can search for files by name, owner, group, type,
permissions, date, and other criteria.
Typing the following command at the prompt lists all files found in the current directory.

find .

The dot after ―find‖ indicates the current directory.

To find files that match a specific pattern, use the -name argument. You can use filename metacharacters
(such as * ), but you should either put an escape character ( \ ) in front of each of them or enclose them in
quotes.
38
II B.C.A.-‘C’ (2016-2019) KGCAS

For example, if we want to find all the files that start with ―pro‖ in the Documents directory, we would use
the cd Documents/ command to change to the Documents directory, and then type the following command:

find . -name pro\*

All files in the current directory starting with ―pro‖ are listed.
NOTE: The find command defaults to being case sensitive. If you want the search for a word or phrase to be
case insensitive, use the -iname option with the find command. It is the case insensitive version of the -
name command.

If find doesn‘t locate any files matching your criteria, it produces no output.
The find command has a lot of options available for refining the search. For more information about the find
command, run man find in a Terminal window and press Enter.
Using the Locate Command
The locate command is faster than the find command because it uses a previously built database, whereas
the find command searches in the real system, through all the actual directories and files. The locate
command returns a list of all path names containing the specified group of characters.
The database is updated periodically from cron, but you can also update it yourself at any time so you can
obtain up-to-the-minute results. To do this, type the following command at the prompt:

sudo updatedb

Enter your password when prompted.

The basic form of the locate command finds all the files on the file system, starting at the root, that contain
all or any part of the search criteria.

locate mydata

For example, the above command found two files containing ―mydata‖ and one file containing ―data.‖

If you want to find all files or directories that contain exactly and only your search criteria, use the -b option
with the locate command, as follows.

39
II B.C.A.-‘C’ (2016-2019) KGCAS

locate -b ‗\mydata‘

The backslash in the above command is a globbing character, which provides a way of expanding wildcard
characters in a non-specific file name into a set of specific filenames. A wildcard is a symbol that can be
replaced by one or more characters when the expression is evaluated. The most common wildcard symbols
are the question mark ( ? ), which stands for a single character and the asterisk ( * ), which stands for a
contiguous string of characters. In the above example, the backslash disables the implicit replacement of
―mydata‖ by ―*mydata*‖ so you end up with only results containing ―mydata.‖

The mlocate command is a new implementation of locate. It indexes the entire file system, but the search
results only include files to which the current user has access. When you update the mlocate database, it
keeps timestamp information in the database. This allows mlocate to know if the contents of a directory
changed without reading the contents again and makes updates to the database faster and less demanding on
your hard drive.
When you install mlocate, the /usr/bin/locate binary file changes to point to mlocate. To install mlocate, if
it‘s not already included in your Linux distribution, type the following command at the prompt.

sudo apt-get install mlocate

NOTE: We will show you a command later in this article that allows you to determine where the executable
for a command is located, if it exists.

The mlocate command does not use the same database file as the standard locate command. Therefore, you
may want to create the database manually by typing the following command at the prompt:

sudo /etc/cron.daily/mlocate

The mlocate command will not work until the database is created either manually or when the script is run
from cron.

For more information about either the locate or the mlocate command, type man locateor man mlocate in a
Terminal window and press Enter. The same help screen displays for both commands.
Using the Which Command
The ―which‖ command returns the absolute path of the executable that is called when a command is issued.
This is useful in finding the location of an executable for creating a shortcut to the program on the desktop,

40
II B.C.A.-‘C’ (2016-2019) KGCAS

on a panel, or other place in the desktop manager. For example, typing the command which firefox displays
the results shown in the image below.

By default, the which command only displays the first matching executable. To display all matching
executables, use the -a option with the command:

which -a firefox

You can search for multiple executables using at once, as shown in the following image. Only the paths to
executables found are displayed. In the example below, only the ―ps‖ executable was found.

NOTE: The which command only searches the current user‘s PATH variable. If you search for an
executable that is only available for the root user as a normal user, no results will display.
For more information about the which command, type ―man which‖ (without the quotes) at the command
prompt in a Terminal window and press Enter.
Using the Whereis Command
The whereis command is used to find out where the binary, source, and man page files for a command are
located. For example, typing whereis firefox at the prompt displays results as shown in the following image.

If you want only the path to the executable to display, and not the paths to the source and the man(ual)
pages, use the -b option. For example, the command whereis -b firefox will display only /usr/bin/firefox as
the result. This is handy because you will most likely search for a program‘s executable file more often than
you would search for source and man pages for that program. You can also search for only the source files
( -s ) or for only the man pages ( -m ).
For more information about the whereis command, type man whereis in a Terminal window and press Enter.

Understanding the Difference Between the Whereis Command and the Which Command
The whereis command shows you the location for the binary, source, and man pages for a command,
whereas the which command only shows you the location of the binary for the command.
The whereis command searches through a list of specific directories for the binary, source, and man files
whereas the which command searches the directories listed in the current user‘s PATH environment
variable. For the whereis command, the list of specific directories can be found in the FILES section of the
man pages for the command.
When it comes to results displayed by default, the whereis command displays everything it finds whereas
the which command only displays the first executable it finds. You can change that using the -a option,
discussed earlier, for the which command.
41
II B.C.A.-‘C’ (2016-2019) KGCAS

Because the whereis command only uses paths hard-coded into the command, you may not always find what
you are looking for. If you are searching for a program you think might be installed in a directory not listed
in the man pages for the whereis command, you might want to use the which command with the -a option to
find all occurrences of the command throughout the system.

Standard Files in Linux or Linux File System Structure


Linux filesystem structures and understand the meaning of individual high-level directories.

1. / – Root
 Every single file and directory starts from the root directory.
 Only root user has write privilege under this directory.
 Please note that /root is root user‘s home directory, which is not same as /.
2. /bin – User Binaries
 Contains binary executables.
 Common linux commands you need to use in single-user modes are located under this directory.
 Commands used by all the users of the system are located here.
 For example: ps, ls, ping, grep, cp.
3. /sbin – System Binaries
 Just like /bin, /sbin also contains binary executables.
 But, the linux commands located under this directory are used typically by system aministrator, for
system maintenance purpose.
 For example: iptables, reboot, fdisk, ifconfig, swapon
4. /etc – Configuration Files
 Contains configuration files required by all programs.
 This also contains startup and shutdown shell scripts used to start/stop individual programs.
 For example: /etc/resolv.conf, /etc/logrotate.conf

42
II B.C.A.-‘C’ (2016-2019) KGCAS
5. /dev – Device Files
 Contains device files.
 These include terminal devices, usb, or any device attached to the system.
 For example: /dev/tty1, /dev/usbmon0
6. /proc – Process Information
 Contains information about system process.
 This is a pseudo filesystem contains information about running process. For example: /proc/{pid}
directory contains information about the process with that particular pid.
 This is a virtual filesystem with text information about system resources. For example: /proc/uptime
7. /var – Variable Files
 var stands for variable files.
 Content of the files that are expected to grow can be found under this directory.
 This includes — system log files (/var/log); packages and database files (/var/lib); emails (/var/mail);
print queues (/var/spool); lock files (/var/lock); temp files needed across reboots (/var/tmp);
8. /tmp – Temporary Files
 Directory that contains temporary files created by system and users.
 Files under this directory are deleted when system is rebooted.
9. /usr – User Programs
 Contains binaries, libraries, documentation, and source-code for second level programs.
 /usr/bin contains binary files for user programs. If you can‘t find a user binary under /bin, look under
/usr/bin. For example: at, awk, cc, less, scp
 /usr/sbin contains binary files for system administrators. If you can‘t find a system binary under /sbin,
look under /usr/sbin. For example: atd, cron, sshd, useradd, userdel
 /usr/lib contains libraries for /usr/bin and /usr/sbin
 /usr/local contains users programs that you install from source. For example, when you install apache
from source, it goes under /usr/local/apache2
10. /home – Home Directories
 Home directories for all users to store their personal files.
 For example: /home/john, /home/nikita
11. /boot – Boot Loader Files
 Contains boot loader related files.
 Kernel initrd, vmlinux, grub files are located under /boot
 For example: initrd.img-2.6.32-24-generic, vmlinuz-2.6.32-24-generic
12. /lib – System Libraries
 Contains library files that supports the binaries located under /bin and /sbin
 Library filenames are either ld* or lib*.so.*
 For example: ld-2.11.1.so, libncurses.so.5.7
13. /opt – Optional add-on Applications
 opt stands for optional.
 Contains add-on applications from individual vendors.
 add-on applications should be installed under either /opt/ or /opt/ sub-directory.
43
II B.C.A.-‘C’ (2016-2019) KGCAS
14. /mnt – Mount Directory
 Temporary mount directory where sysadmins can mount filesystems.
15. /media – Removable Media Devices
 Temporary mount directory for removable devices.
 For examples, /media/cdrom for CD-ROM; /media/floppy for floppy drives; /media/cdrecorder for CD
writer
16. /srv – Service Data
 srv stands for service.
 Contains server specific services related data.
 For example, /srv/cvs contains CVS related data.

Redirection in Linux
What is Redirection?
Redirection is a feature in Linux such that when executing a command, you can change the standard
input/output devices. The basic workflow of any Linux command is that it takes an input and give an output.
 The standard input (stdin) device is the keyboard.
 The standard output (stdout) device is the screen.
With redirection, the above standard input/output can be changed.

Output Redirection
The '>' symbol is used for output (STDOUT) redirection.

Example:
ls -al > listings
Here the output of command ls -al is re-directed to file "listings" instead of your screen.

Note: Use the correct file name while redirecting command output to a file. If there is an existing file with
the same name, the redirected command will delete the contents of that file and then it may be overwritten."
If you do not want a file to be overwritten but want to add more content to an existing file, then you should
use '>>' operator.

You can redirect standard output, to not just files, but also devices!
$ cat music.mp3 > /dev/audio
The cat command reads the file music.mp3 and sends the output to /dev/audio which is the audio device. If
the sound configurations in your PC are correct, this command will play the file music.mp3

Input redirection
The '<' symbol is used for input(STDIN) redirection

Example: The mail program in Linux can help you send emails from the Terminal.
You can type the contents of the email using the standard device keyboard. But if you want to attach a File
to email you can use the input re-direction operator in the following format.
Mail -s "Subject" to-address < Filename
44
II B.C.A.-‘C’ (2016-2019) KGCAS

The above examples were simple. Let's look at some advance re-direction techniques which make use of
File Descriptors.

File Descriptors (FD)


In Linux/Unix, everything is a file. Regular file, Directories, and even Devices are files. Every File has an
associated number called File Descriptor (FD).
Your screen also has a File Descriptor. When a program is executed the output is sent to File Descriptor of
the screen, and you see program output on your monitor. If the output is sent to File Descriptor of the
printer, the program output would have been printed.

Error Redirection
Whenever you execute a program/command at the terminal, 3 files are always open, viz., standard input,
standard output, standard error.

These files are always present whenever a program is run. As explained before a file descriptor, is associated
with each of these files.
File File Descriptor

Standard Input STDIN 0

Standard Output STDOUT 1

Standard Error STDERR 2


By default, error stream is displayed on the screen. Error redirection is routing the errors to a file other than
the screen.

Why Error Redirection?


Error re-direction is one of the very popular features of Unix/Linux.
Frequent UNIX users will reckon that many commands give you massive amounts of errors.
 For instance, while searching for files, one typically gets permission denied errors. These errors
usually do not help the person searching for a particular file.
 While executing shell scripts, you often do NOT want error messages cluttering up the normal
program output.
The solution is to re-direct the error messages to a file.
Example 1
$ myprogram 2>errorsfile

Above we are executing a program names myprogram.


The file descriptor for standard error is 2.
Using "2>" we re-direct the error output to a file named "errorfile"
Thus, program output is not cluttered with errors.
Example 2
45
II B.C.A.-‘C’ (2016-2019) KGCAS
Here is another example which uses find statement -
find . -name 'my*' 2>error.log
Using the "find" command, we are searching the "." current directory for a file with "name" starting with
"my"

Example 3 Let's see a more complex example,


Server Administrators frequently, list directories and store both error and standard output into a file, which
can be processed later. Here is the command.
ls Documents ABC> dirlist 2>&1
Here,
 which writes the output from one file to the input of another file. 2>&1 means that STDERR
redirects to the target of STDOUT (which is the file dirlist)
 We are redirecting error output to standard output which in turn is being re-directed to file dirlist.
Hence, both the output is written to file dirlist

Filters in Linux
Linux Filters
Linux Filter commands accept input data from stdin (standard input) and produce output
on stdout (standard output). It transforms plain-text data into a meaningful way and can be used with pipes
to perform higher operations.
These filters are very small programs that are designed for a specific function which can be used as building
blocks.

Linux Filter Commands


Linux Cat Filters
When cat command is used inside pipes, it does nothing except moving stdin to stout.
Syntax:
1. cat <fileName> | cat or tac | cat or tac |. . .
Example:
1. cat weeks.txt | tac | cat | cat | tac

46
II B.C.A.-‘C’ (2016-2019) KGCAS

Look at the above snapshot, output of one 'cat' or 'tac' command is passing onto another as input.

Linux cut
The 'cut' command is useful in selecting a specific column of a file. After (-d), delimiter (from where you
want to separate the columns) comes. Delimiters can be a space (' '), a hyphen (-), a slash (/) or anything
else. After (-f), column number is mentioned.
Syntax:
1. cut -d(delimiter) -f(columnNumber) <fileName>
Hyphen (-) As Delimiter
Syntax:
1. cut -d- -f(columnNumber) <fileName>
Example:
1. cut -d- -f2 marks.txt
2. cut -d- -f1 marks.txt

Look at the above snapshot, our delimiter is hyphen (-), hence we have used (-) after (-d). Command "cut -
d- -f1 marks.txt"displays column 1 and command "cut -d- -f2 marks.txt" displays column 2.
Space As Delimiter
If you want to use space as a delimiter then you have to quote the space (' ').
Syntax:
1. cut -d ' ' -f(columnNumber) <fileName>
Example:
1. cut -d ' ' -f2 marks.txt
2. cut -d ' '-f5 marks.txt
3. cut -d ' '-f3 marks.txt

47
II B.C.A.-‘C’ (2016-2019) KGCAS

Look at the above snapshot, our delimiter is space, hence we have used (' ') after (-d). Command "cut -d ' ' -
f2 marks.txt"displays column 2, command "cut -d ' '-f5 marks.txt" displays column 5 and command "cut
-d ' '-f3 marks.txt" displays column 3.

Linux grep
The 'grep' command stands for "global regular expression print". grep command filters the content of a
file which makes our search easy.
grep with pipe
The 'grep' command is generally used with pipe (|).
Syntax:
1. command | grep <searchWord>
Example:
1. cat marks.txt | grep 9

Look at the above snapshot, grep command filters all the data containing '9'.

grep without pipe


It can be used without pipe also.
Syntax:
1. grep <searchWord> <file name>
Example:
1. grep 9 marks.txt

Look at the above snapshot, grep command do the same work as earlier example but without pipe.
grep options
o grep -vM: The 'grep -v' command displays lines not matching to the specified word.
Syntax:
1. grep -v <searchWord> <fileName>
Example:
48
II B.C.A.-‘C’ (2016-2019) KGCAS
1. grep -v 9 marks.txt

Look at the above snapshot, command "grep -v 9 marks.txt" displays lines hwich don't contain our
search word '9'.
o grep -i: The 'grep -i' command filters output in a case-insensitive way.
Syntax:
1. grep -i <searchWord> <fileName>
Example:
1. grep -i red exm.txt

Look at the above snapshot, command "grep -i red exm.txt" displays all lines containing 'red'
whether in upper case or lower case.
o grep -A/ grep -B/ grep -C
grep -A command is used to display the line after the result.
grep -B command is used to display the line before the result.
grep -C command is used to display the line after and line before the result.
You can use (A1, A2, A3.....)(B1, B2, B3....)(C1, C2, C3....) to display any number of lines.
Syntax:
1. grep -A<lineNumber> <searchWord> <fileName>
2. grep -B<lineNumber> <searchWord> <fileName>
3. grep -C<lineNumber> <searchWord> <fileName>
Example:
1. grep -A1 yellow exm.txt
2. grep -B1 yellow exm.txt
3. grep -C1 yellow exm.txt

Look at the above snapshot, command "grep -A1 yellow exm.txt" displays searched line with next
succeeding line, command "grep -B1 yellow exm.txt" displays searched line with one preceding
line and command "grep -C1 yellow exm.txt" displays searched line with one preceding and
succeeding line.

Linux comm
The 'comm' command compares two files or streams. By default, 'comm' will always display three columns.
First column indicates non-matching items of first file, second column indicates non-matching items of

49
II B.C.A.-‘C’ (2016-2019) KGCAS
second file, and third column indicates matching items of both the files. Both the files has to be in sorted
order for 'comm' command to be executed.
Syntax:
1. comm <file1> <file2>
Example:
1. comm file1.txt file2.txt

Look at the above snapshot,


First column displays non-matching items of first file 'file1.txt' (Sehwag, Yuvi)
Second column displays non-matching items of second file 'file2.txt' (Zadeja)
Third column indicates matching items of both the files (Dhoni, Dravid, Sachin)

To Display Single Column


If you want to output a single column, you have to specify number of the columns which are not to be
displayed.
Syntax:
comm -23 (To display first column)
comm -13 (To display second column)
comm -12 (To display third column)
Example:
comm -23 file1.txt file2.txt
comm -13 file1.txt file2.txt
comm -12 file1.txt file2.txt

Look at the above snapshot, column number which needs to be displayed are not mentioned in the 'comm'
command.

Linux sed
Command 'sed' stands for stream editor. You can use this command to edit streams (files) using regular
expressions. But this editing is not permanent. It remains only in display, but in actual, file content remains
same.
Syntax:
1. command | sed 's/<oldWord>/<newWord>/'
Example:
1. echo class7 | sed 's/class/jtp/'
2. echo class7 | sed 's/7/10/'
3. cat msg.txt | sed 's/learn/study/'

50
II B.C.A.-‘C’ (2016-2019) KGCAS

Look at the above snapshot, first we have performed 'sed' command on a string 'class7' where 'class' is
changed into 'jtp' and 7 into 10. Then we have performed 'sed' command on a stream 'msg.txt' where 'learn'
is converted into 'study'.

Global Replacement
In earlier example, all 'learn' word were not edited into 'study'. To edit every word we have to use
a global replacement 'g'. It will edit all the specified word in a file or string.
Syntax:
1. command | sed 's/<oldWord>/<newWord>/g'
Example:
1. echo class7 class9 | sed 's/class/jtp/g'
2. cat msg.txt | sed 's/learn/study/g'

Look at the above snapshot, with command "echo class7 class9 | sed 's/class/jtp/g'" all the 'class' is
converted into 'jtp' and with command "cat msg.txt | sed 's/learn/study/g'" all the 'learn' was converted
into 'study'.

Removing A Line
The 'd' option will let you to remove a complete line from a file. You only need to specify a word from that
line with 'd' option and that line will be deleted. But please note that all the lines having that same word will
be deleted.
Syntax:
1. cat <fileName> | sed '/<Word>/d'
Example:
1. cat msg.txt | sed '/jtp/d'

Look at the above snapshot, with command "cat msg.txt | sed '/jtp/d'" all lines containing word 'jtp' is
deleted.

Linux tee
51
II B.C.A.-‘C’ (2016-2019) KGCAS
The 'tee' command is similar to 'cat' command with only one difference. It puts stdin on stdout and also put
them into a file.
Syntax:
1. cat or tac <fileName> | tee <newFile> | cat or tac |.....
Example:
1. tac weeks.txt | tee new.txt | cat

Look at the above screenshot, file 'new.txt' is created with the help of'tee'command.

Linux tr
The command 'tr' stands for 'translate'. It is used to translate, like from lowercase to uppercase and vice
versa or new lines into spaces.
Syntax:
1. command | tr <'old'> <'new'>
Change Case
The 'tr' command can change case.
Syntax:
1. command | tr <'old'> <'new'>
Example:
1. cat exm.txt | tr 'prcu' 'PRCU'

Look at the above snapshot, all p,r,c,u are converted into upprecase P,R,C,U.
Remove New Lines
To write all the lines into a single line we have to translate all new lines into spaces.
Syntax:
1. command | tr <'\n'> <' '>
Example:
1. cat exm.txt | tr '\n' ' '

tr Options
o tr -s : The 'tr -s' command squeezes the occurence of multiple characters into one.

52
II B.C.A.-‘C’ (2016-2019) KGCAS
Syntax:
1. command | tr -s <'letter'>
Example:
1. cat jtp.txt | tr -s 'l'
2. cat spaces.txt | tr -s ' '
3.

Look at the above snapshot, command "cat jtp.txt | tr -s 'l'" has squeezed all the letters 'l' into one
and command "cat spaces.txt | tr -s ' '" has squeezed all the spaces into single space.
o tr rot13: This command encrypts the text. It is case-sensitive.
Example:
1. cat exm.txt | tr 'a-z' 'nopqrstuvwxyzabcdefghijklm'
2. cat exm.txt | tr 'a-z' 'n-za-m'

Look at the above snapshot, all the letters are encrypted according to the command. But letter 'A' and
'M' are not encrypted as they are in upper case.
o tr -d: The 'tr -d' command is used to delete characters.
Syntax:
1. command | tr -d <letter>
Example:
1. cat exm.txt | tr -d o

Look at the above snapshot, all the 'o' letters are deleted from the file 'exm.txt'.

Linux uniq
With the help of uniq command you can form a sorted list in which every word will occur only once.
Syntax:
1. command <fileName> | uniq
Example:
1. sort dupli.txt | uniq

53
II B.C.A.-‘C’ (2016-2019) KGCAS

Look at the above snapshot, words which are repeating, repeats only once with the 'uniq' command.

uniq -c
You can count the number of occurences of a word with 'uniq -c' command.
Syntax:
1. command <fileName> | uniq -c
Example:
1. sort dupli.txt | uniq -c

Look at the above snapshot, command "sort dupli.txt | uniq -c" counts the number of times a word is
repeating.

Linux wc
The 'wc' command helps in counting the lines, words and characters in a file.
Syntax:
1. wc <fileName> (Counts words, lines and characters)
2. wc -l <fileName> (Counts only lines)
3. wc -w <fileName> (Counts only words)
4. wc -c <fileName> (Counts only characters)
Example:
1. wc exm.txt
2. wc -l exm.txt
3. wc -w exm.txt
4. wc -c exm.txt

54
II B.C.A.-‘C’ (2016-2019) KGCAS
Look at the above snapshot, command "wc exm.txt" displays all three counts together, command wc -l
exm.txt displays line counts, command "wc -w exm.txt" displays word counts and command "wc -c
exm.txt" displays character counts.

Linux od
The 'od' term stands for octal dump. It displays content of a file in different human-readable formats like
hexadecimal, octal and ASCII characters.
Syntax:
1. od -b <fileName> (display files in octal format)
2. od -t x1 <fileName> (display files in hexadecimal bytes format)
3. od -c <fileName> (display files in ASCII (backslashed) character format)
Example:
1. od -b format.txt
2. od -t x1 format.txt
3. od -c format.txt

Look at the above snapshot, command "od -b format.txt" displays in octal format, command "od -t x1
format.txt" displays in hexadecimal format, command "od -c format.txt" displays in ASCII character
where a new line will be marked with '\n'

Linux sort
The 'sort' command sorts the file content in an alphabetical order.
Syntax:
1. sort <fileName>
Example:
1. sort weeks.txt

Look at the above snapshot, the 'sort' command has sorted the file 'weeks.txt' in alphabetical order.
To Sort A Column
If a file has more than one column, column number is used to sort a specific column.
Syntax:
1. sort -k<columnNumber> <fileName>
Example:
1. sort -k1 states.txt
2. ort -k2 states.txt

55
II B.C.A.-‘C’ (2016-2019) KGCAS

Look at the above snapshot, we have sorted column 1 and 2.


Numeric Sorting
Numeric sorting is different from alphabetical sorting. For numeric sorting option 'n' is used along with the
column number if required.
Syntax:
1. sort -n -k<columnNumber> <fileName>
Example:
1. sort -n -k2 marks.txt

Linux gzip
Gzip (GNU zip) is a compressing tool, which is used to truncate the file size. By default original file will be
replaced by the compressed file ending with extension (.gz).
To decompress a file you can use gunzip command and your original file will be back.
Syntax:
1. gzip <file1> <file2> <file3>. . .
2. gunzip <file1> <file2> <file3>. . .
3.
Example:
1. gzip file1.txt file2.txt
2. gunzip file1.txt file2.txt
3.

Look at the above snapshot, the gzip command has compressed the files 'file1.txt' and 'file2.txt'.
Compressed files are shown with the extension (.gz). While gunzip command has decompressed the same
files and extension (.gz) is removed.

56
II B.C.A.-‘C’ (2016-2019) KGCAS
gzip options
Compressing Multi Files Together
If you want to compress more than one file together, you can use 'cat' and gzip command with pipe
command.
Syntax:
1. cat <file1> <file2>. . | gzip > <newFile.gz>
Example:
1. cat file1.txt file2.txt | gzip > final.gz

Look at the above snapshot, both the files ' file1.txt' and 'file2.txt' are compressed as 'final.gz'.

gzip -l
The 'gzip -l' command tells about the compression ratio or how much the original file has compressed.
Syntax:
1. gzip -l <file1> <file2>. .
Example:
1. gzip -l final.gz jtp.txt.gz

Look at the above snapshot, command "gzip -l final.gz jtp.txt.gz" shows the ratio of both the files.

How To Compress A Directory


The gzip command will not be able to compress a directory because it can only compress a single file. To
compress a directory you have to use 'tar' command.
Hyphen (-) is not mandatory in 'tar' command.
'c' is to create,
'v' is for verbose, to display output,
'f' to mention destination of your output file,
'z' for specifying compress with gzip.
Syntax:
1. tar cf - <directory> | gzip > <directoryName>
OR
1. tar cvfz office.tar.gz office
Example:
1. tar cf - office | gzip > office.tar.gz

Look at the above snapshot, directory 'office' is compressed with 'tar' command.

UNIT – IV
Securing files in LINUX: File access permissions viewing file access permissions Changing File access
permissions. Automating Tasks using Shell Scripts: Introduction Variables- Local and Global Shell
variables Command Substitution

57
II B.C.A.-‘C’ (2016-2019) KGCAS
Understanding and Setting UNIX File Permissions
1. Introduction
This guide gives a brief introduction to UNIX file permissions, provides instructions on how to perform
simple configuration of UNIX file permissions, and shows how to find files that may have more permissive
settings than intended.
2. Theory of Operation
UNIX assigns file permissions to three broad categories:
1. The user that owns the file
2. The group that owns the file
3. Everyone else (a.k.a 'world' or 'other')
Each permission category can be assigned three permissions:
1. (r)ead
2. (w)rite
3. e(x)ecute
UNIX permissions can be represented as symbols and as octal digits. Fig. 1 shows a table of possible values:
Octal Symbol Permission
0 --- No Permissions
1 --x Execute
2 -w- Write
3 -wx Write and Execute
4 r-- Read
5 r-x Read and Execute
6 rw- Read and Write
7 rwx Read, Write, and Execute
Fig. 1: Table of Permission Combinations
The effect of these permissions varies by file type. There are numerous file types, but this guide will only
deal with the following types:
1. '-' — Regular file
2. 'd' — Directory
When applied to a regular file, UNIX permissions have the following effect:
1. Read permission allows the file to be opened
2. Write permission allows the file to be modified
3. Execute permission allows the file to be executed directly from the shell, if it is a script or binary file
When applied to a directory, UNIX permissions have the following effect:
1. Read permission allows the directory contents to be listed
2. Write permission allows files or directories to be created or deleted within the directory
3. Execute permission allows the directory contents to be accessed
UNIX permissions closest to the user take precedence. User permissions take priority over group
permissions for the user who owns a file or directory, and both user and group permissions take priority over
the 'other' permissions for the user and group who owns the file.
3. Setting UNIX File Permissions
There are three utilities used for managing UNIX permissions:
1. 'ls -l' — Displays a detailed list of directory contents
2. 'chmod' — Changes permissions
3. 'chgrp' — Changes group owners
3.1. 'ls -l'
The ls commands outputs a list of files and directories within a given directory, and the '-l' option provides a
detailed list of the directory contents (including permissions).
Fig. 2 shows the output of 'ls -l' on a test file, with each section labeled. The portions of the output related to
UNIX file system permissions are colored red:

58
II B.C.A.-‘C’ (2016-2019) KGCAS

Fig. 2: Labeled output of 'ls -l'


The output of Fig. 2 tells us the following:
1. The '-' file type indicates that this is a regular file
2. 'rwx' permissions for user indicates that the user who owns this file has read, write, and execute
permissions for the file
3. 'r-x' permissions for group indicates that the group who owns this file has read and execute
permissions for the file
4. '---' permissions for 'other' (i.e., everyone else) means that everyone other than the user or group who
owns the file has no access to the file.
5. The file is owned by user 'walbert' and group 'support'

3.2. 'chmod'
The chmod command changes the permission on a given file or directory.
chmod sets permissions in two ways.
1. Using symbols
2. Using octal values
This guide uses primarily octal values because they can be entered quickly. For more information on using
symbols, refer to the chmod man page.
When using octal values, a series of three digits defines the permissions for user, group, and other in that
order. For example, a three-digit octal value of 640 would translate to read and write permissions for the
user, read permission for the group, and no permission for others. When using octal values to define
permissions, the new octal value entered will replace the existing permissions on the file or directory.
(There is an optional leading digit which sets the setuid, setgid, and sticky bits. Usage of these permissions
bits are outside the scope of this guide.)
The following examples show common uses of chmod. After the chmod command, the output of the ls -l
command is shown to demonstrate how the file permissions should look. Refer to the table in Fig. 1 for
explanations of the octal values.
3.2.1. Access for Only Yourself
$ chmod 600 test_file
$ ls -l test_file
-rw------- 1 walbert ugrad 0 Nov 5 15:58 test_file
3.2.2. Write Access for Yourself and Read Access for Your Group
$ chmod 640 test_file
$ ls -l test_file
-rw-r----- 1 walbert ugrad 0 Nov 5 15:58 test_file
3.2.3. Write Access for Yourself and Read Access for Everyone Else
$ chmod 644 test_file
$ ls -l test_file
-rw-r--r-- 1 walbert ugrad 0 Nov 5 15:58 test_file
3.2.4. Write and Execute Access for Yourself Only
$ chmod 700 test_file
$ ls -l test_file
-rwx------ 1 walbert ugrad 0 Nov 5 15:58 test_file
3.2.5. Write and Execute Access for Yourself, and Execute Access for Everyone

59
II B.C.A.-‘C’ (2016-2019) KGCAS
$ chmod 755 test_file
$ ls -l test_file
-rwxr-xr-x 1 walbert ugrad 0 Nov 5 15:58 test_file
3.2.6. Write Access to a Directory for Yourself, and Read Access for Your Group
$ chmod 750 test_directory
$ ls -l -d test_directory
drwxr-x--- 2 walbert ugrad 2.0K Nov 5 15:59 test_directory/
3.2.7. Remove All Non-User Access to a Directory and its Sub-directories
$ chmod -R go-rwx test_directory
$ ls -l -R test_directory
test_directory/:
total 4.0K
drwx------ 3 walbert ugrad 2.0K Nov 5 16:53 foo/
-rw------- 1 walbert ugrad 0 Nov 5 16:20 test_file
test_directory/foo:
total 4.0K
drwx------ 3 walbert ugrad 2.0K Nov 5 16:53 bar/
-rw------- 1 walbert ugrad 0 Nov 5 16:53 test1
test_directory/foo/bar:
total 4.0K
drwx------ 2 walbert ugrad 2.0K Nov 5 16:53 baz/
-rw------- 1 walbert ugrad 0 Nov 5 16:53 test2
test_directory/foo/bar/baz:
total 0
-rw------- 1 walbert ugrad 0 Nov 5 16:53 test3
In this case, symbolic permission assignment was used, as octal permission assignments usually do not
produce the intended result when applied recursively. The symbolic permissions used in this example, 'go-
rwx', can be translated as ―for group and other—remove read, write, and execute permissions.‖
3.3. 'chgrp'
The chgrp command changes the group ownership on a given file or directory.
Changing group ownership may be necessary when sharing files and directories with other members of your
research group.
3.3.1. Changing the Group Ownership of a File
$ chgrp support test_file
$ ls -l test_file
-rw-rw---- 1 walbert support 0 Nov 5 15:58 test_file
3.3.2. Changing the Group Ownership of a Directory and its Contents
$ chgrp -R support test_directory
$ ls -l -R test_directory
test_directory:
total 4.0K
drwx------ 3 walbert support 2.0K Nov 5 16:53 foo/
-rw------- 1 walbert support 0 Nov 5 16:20 test_file
test_directory/foo:
total 4.0K
drwx------ 3 walbert support 2.0K Nov 5 16:53 bar/
-rw------- 1 walbert support 0 Nov 5 16:53 test1
test_directory/foo/bar:
total 4.0K
drwx------ 2 walbert support 2.0K Nov 5 16:53 baz/
-rw------- 1 walbert support 0 Nov 5 16:53 test2
test_directory/foo/bar/baz:
total 0
-rw------- 1 walbert support 0 Nov 5 16:53 test3

60
II B.C.A.-‘C’ (2016-2019) KGCAS
4. Finding Files with Unwanted Permissions
This section provides information on the use of the find command to find and display files and directories
with permissions that may be undesirable. All examples in this section will output results in the format
typically provided by ls -l.
All examples will use the following format:
find /dir/name -perm /005 -type f -print0 | xargs -0 ls -l
Explanation of these options:
/dir/name — Directory being searched
-perm — Instructs find to match files based on given permissions
-type f — Instructs find to match only regular files
-print0 — Sends output to stdout using the null character
xargs — Command that builds another command to execute based on the content of stdin
-0 — Use the null character as the item delimiter
4.1. Find All Files Accessible by 'Other' in Any Way
$ find ~ -perm /007 -type f -print0 | xargs -0 ls -l
This will display all files within your home directory that have read, write, or execute permissions for 'other.'
This is useful for getting a general idea of what others can access in your home directory.
4.2. Find All Files Readable by 'Other'
$ find . -perm /004 -type f -print0 | xargs -0 ls -l
This will display all files within the current working directory that are world-readable. Many of these files
will be 'common' files (such as shared libraries, icons, etc.) located in hidden directories, which are generally
harmless. Any private files that appear on this list (e.g., private SSH keys or files on non-hidden directories)
should be investigated.
4.3. Find All Files Writable by 'Other'
$ find /extra/research0 -perm /002 -type f -print0 | xargs -0 ls -l
This will display all files in the /extra/research0 directory that are writable by 'other.' Any files that appear in
this list should be investigated, and in most cases, permissions should be changed to something more
restrictive.
4.4. Find All Files Executable by User or Group, and Writable by 'Other'
$ find ~ -perm -102 -type f -print0 | xargs -0 ls -l # User
$ find ~ -perm -012 -type f -print0 | xargs -0 ls -l # Group
This will display all files in your home directory that are both world-writable and executable by either the
user or the group that owns the file. Files that are user-executable and world-writable are a major security
vulnerability and should be fixed immediately unless you know exactly what you are doing.
4.5. Find All Files Owned by a Specific Group
$ find ~ -group NameOfGroup -type f -print0 | xargs -0 ls -l
This will display all files in your home directory that are owned by the specified group.
4.6. Find All Files Not Owned by a Specific Group
$ find . -not -group NameOfGroup -type f -print0 | xargs -0 ls -l
This will display all files in the current working directory that are not owned by the specified group.

7 Chmod Command Examples for Beginners


Following are the symbolic representation of three different roles:
 u is for user,
 g is for group,
 and o is for others.
Following are the symbolic representation of three different permissions:
 r is for read permission,
 w is for write permission,
 x is for execute permission.
Following are few examples on how to use the symbolic representation on chmod.

61
II B.C.A.-‘C’ (2016-2019) KGCAS
1. Add single permission to a file/directory
Changing permission to a single set. + symbol means adding permission. For example, do the following to
give execute permission for the user irrespective of anything else:
$ chmod u+x filename
2. Add multiple permission to a file/directory
Use comma to separate the multiple permission sets as shown below.
$ chmod u+r,g+x filename
3. Remove permission from a file/directory
Following example removes read and write permission for the user.
$ chmod u-rx filename
4. Change permission for all roles on a file/directory
Following example assigns execute privilege to user, group and others (basically anybody can execute this
file).
$ chmod a+x filename
5. Make permission for a file same as another file (using reference)
If you want to change a file permission same as another file, use the reference option as shown below. In this
example, file2‘s permission will be set exactly same as file1‘s permission.
$ chmod --reference=file1 file2
6. Apply the permission to all the files under a directory recursively
Use option -R to change the permission recursively as shown below.
$ chmod -R 755 directory-name/
7. Change execute permission only on the directories (files are not affected)
On a particular directory if you have multiple sub-directories and files, the following command will assign
execute permission only to all the sub-directories in the current directory (not the files in the current
directory).
$ chmod u+X *
Note: If the files has execute permission already for either the group or others, the above command will
assign the execute permission to the user

UNIX / Linux: Beginners Guide to File and Directory Permissions ( umask, chmod,
read, write, execute )
Unix file and directory permission is in the form of a 3×3 structure. i.e Three permissions (read, write and
execute) available for three types of users (owner, groups and others).
In the output of ls -l command, the 9 characters from 2nd to 10th position represents the permissions for the
3 types of users.
-rw-r--r-- 1 sathiya sathiya 272 Mar 17 08:22 test.txt
In the above example:
 User (sathiya) has read and write permission
 Group has read permission
 Others have read permission
Three file permissions:
 read: permitted to read the contents of file.
 write: permitted to write to the file.
 execute: permitted to execute the file as a program/script.
Three directory permissions:
 read: permitted to read the contents of directory ( view files and sub-directories in that directory ).
 write: permitted to write in to the directory. ( create files and sub-directories in that directory )
 execute: permitted to enter into that directory.
62
II B.C.A.-‘C’ (2016-2019) KGCAS
Numeric values for the read, write and execute permissions:
 read 4
 write 2
 execute 1
To have combination of permissions, add required numbers. For example, for read and write permission, it is
4+2 = 6.
Change File and Directory Permissions Using Chmod Command
You can use either the octal representation or symbolic representation to change the permission of a file or
directory.
Octal representation for permissions:
 First number is for user
 Second number is for group
 Third number is for others
For example, give read, write ( 4+2 = 6 ) to user and read ( 4 ) to group and others.
$ chmod 644 filename
For example, give read, execute ( 4 + 1 = 5 ) to user and read (4 ) to group, and nothing ( 0 ) to others.
$ chmod 540 filename
For example, give read, write ( 4 + 2 = 6 ) to user and nothing ( 0 ) to group, and read ( 4 ) to others.
$ chmod 604 filename
Umask 022 is Responsible for the default permission of a file
The default umask value is 0022, which decides the default permission for a new file or directory. Default
permission for a directory is 0777, for files the permissions are 0666 from which the default umask value
0022 is deducted to get the newly created files or directory permission.
Final default permission for a file is calculated as shown below:
 Default file permission: 666
 Default umask : 022
 Final default file permission: 644
Final default permission for a directory is calculated as shown below:
 Default directory permission: 777
 Default umask: 022
 Final default directory permission: 755
You can change the umask value to appropriate value of what you need based upon the above calculation.
For example, if you don‘t want anybody other than the user (owner) to do anything on the file or directory
then you can give umask as 0077.
$ umask 0077
After this, if you create a file or directory, it will have permissions only for the user as shown below:
$ > testfile
$ ls -l testfile
-rw------- 1 sathiya sathiya 0 Mar 17 08:23 testfile

Linux File Permissions


In Ubuntu / Linux everything is a file, so everything will have permissions also.
File permissions define which user or system accounts have permissions to read, write, and execute specific
files.
These read, write, and execute permissions are defined for:
user the user that owns the file
group users in the files group
other every other user

63
II B.C.A.-‘C’ (2016-2019) KGCAS
There are also three other components when it comes to file mode bits, namely the setuid bit, the setgid bit,
and the sticky bit.
As you'll see later, these "special mode bits" can only be used for certain files.

How Read, Write, And Execute Permissions Are Represented


File permissions are identified through file mode bits. These bits represent what actions can be carried out
by specific user accounts.
For example, if you run the command ls -l to list the files in the current directory, you'll see something
similar to this at the beginning of each line in the results:
-rwxrwxrwx
The repeated rwx sequences represent the notion of read (r), write (w), and execute (x) permissions for
user, group, and other (in that order).
Hence the -rwxrwxrwx above indicates that user, group, and other have read, write and execute permissions
for that file or in other words: the owner of the file, anyone in the file's group, and everybody else has read,
write, and execute permissions for that file).
Note that the leading - you'll see in permissions like -rwxrwxrwx simply indicates that this is a normal file
(file type regular).

File Types
The possible file types you may see are depicted by preceding the permissions by one of these:
 - = Regular File
 d = Directory
 l = Symbolic Link
 b = Block Special Device
 c = Character Device
 s = Unix Socket (local domain socket)
 p = Named Pipe
Here are a few more examples of what you might see:
-rw-rw-r--
A regular file, readable and writeable by user and group, but only readable by everybody else.
drwxr-xr-x
Note that the d above indicates that the permissions are for a directory (i.e. the file's type is a directory).
This directory is readable, writeable, and executable by "user" whilst only readable and executable by
"group" and "other".
Also note that for directories, the execute mode bit x indicates access / searchability of that directory for a
particular category of user.
-rw-------
The above permissions show that the owner of this regular file has read and write permission but nobody
else has any permissions for that file.
If you see a file with permissions like this:
crw--w----
You'll know it refers to a "character device" (such as a tty) where the "user" has read and write permission,
the "group" has write permission, and "other" has no permissions.
To recap, the meanings of r, w, and x for each of the three categories "user", "group", and "other" are
illustrated in the image below which shows an ls -l command run in a directory which contains
filename.txt:

64
II B.C.A.-‘C’ (2016-2019) KGCAS

A File's "User" And "Group"


The user name shown in the image above is the name of the user account which owns the file (normally the
creator, but this can be changed using chown) whilst the group name is the creator's primary group (this can
be changed using chgrp).
By default in Ubuntu, the default primary group is a group with the same name as the user. This is the case
above where both the user and group are "tutonics" (for more info about user accounts and groups, please
read our post about user account and group management).
To understand how default permissions are determined, skip to section "Umask - Configuring Default File /
Directory Permissions" below.
Special Mode Bits
Normal Process Permissions
When a process runs, it takes on the effective permissions of the user who started it. This means the process
can only read / write / execute what the user has permissions for.
The same applies to the effective group id of a process, it assumes that of the user, so group permissions of
the process mirror that of the user.
This behaviour gets changed when setuid and/or setgid bits are set as you'll see next.
Setuid - Set User ID
When the setuid bit is set for a program, on execution the process's effective user ID gets set to that of the
program file itself (rather than that of the user running it).
If a file with permissions -rwxrwxrw- gets its setuid bit set, the permissions will be displayed as -
rwsrwxrw-. Note the lower case s where the x was.
If however, the file didn't have the x permissions for the user, and then had the setuid bit set, you'd see -
rwSrwxrw- instead.
So to recap, there is a difference between S and s the former indicates just the setuid bit, the latter indicates
setuid bit and execute x (for that position) in the permissions is set.
Setgid - Set Group ID
When the setgid bit is set for a program, on execution the process's effective group ID gets set to that of
the program file (rather than that of the user's primary group).
Like setuid, the setgid bit is shown as either an S or s.
If a file starts out with -rw-r--r-- (no group x) and has its setgid bit set, you'd see it being displayed as -
rw-r-Sr-- whereas if it started out as -rw-r-xr-- it would be displayed as -rw-r-sr-- once the setgid bit
is set.

65
II B.C.A.-‘C’ (2016-2019) KGCAS
Setuid For Directories
When the setuid bit is set as part of a directory's permissions in Ubuntu, it does nothing, i.e. it has no effect
(This is not the case for the setgid bit, as you'll see next).
Setgid For Directories
When the setgid bit is set for a directory, any files created in that directory will have the same group as that
directory.
Also, any directories created in that directory will also have their setgid bit set.
Sticky Bit
Nowadays (for linux) the sticky bit is used only in relation to directories.
When a directory has the sticky bit set, only root or the file's owner has permission to change files in
that directory.
The letter's T and t are used to indicate that the sticky bit is set. e.g. A directory with permissions drwxr-
xr-x having the sticky bit set, would change to drwxr-xr-t whilst a dir with drwxr-xr-- would change to
drwxr-xr-T (So t vs T depends on whether the "other" category has x permissions set or not respectively).
Permissions: Octal Representation
Sometimes, you'll see permissions referred to numerically in base 8 octal (i.e. using digits 0-7).
Permissions Symbolic Binary Octal
read, write, and execute rwx 111 7
read and write rw- 110 6
read and execute r-x 101 5
read r-- 100 4
write and execute -wx 011 3
write -w- 010 2
execute --x 001 1
no permissions --- 000 0
So for example, using the table above, we can see that the file permissions -rwxrwxrwx can be represented
in octal as 777 (because each rwx translates to an octal digit 7).
Note that the octal number refers to permissions, the file type does not matter.
So, if we wanted to represent the permissions drwxrwxrwx of a directory in octal, the same octal number 777
would also apply.
Other Octal Permission Examples:
User / Group / Other rwx Mode Symbols Octal Equivalent
-rwxr-xr-x 755
-rw-rw-r-- 664
-rw-r--r-- 644
-rw------- 600
Changing File Permissions - Chmod
The chmod command is used to change the various permission bits of a file or directory.
The command takes the general form:
chmod MODE file
There are two ways to represent the MODE:
1. Using symbolic modes (letters to indicate the categories and permission)
2. Using numeric modes (An octal (base 8) number that represents the mode)
Using the "numeric modes" way of setting these permissions is shorter than the symbolic method, but not as
flexible because you can't build on top of existing permissions which is possible when using "symbolic
modes".

66
II B.C.A.-‘C’ (2016-2019) KGCAS
Using Symbolic Modes With Chmod
In order to change the permissions of a file using symbolic permissions, use the command format:
chmod SYMBOLIC-MODE FILENAME
where SYMBOLIC-MODE is the symbolic representation of permissions (which we describe below) that
you wish to apply to FILENAME.
The letters for user, group, and other are u, g, and o respectively. The letter a is used to mean all three of
these categories.
The MODE above takes the form (as per manpage):
[ugoa...][[+-=][permissions...]...]
So, the operations available are:
 + (add the permissions to what currently exists).
 - (remove the permissions from what currently exists).
 = (set to this value only, replacing existing permissions).
When you combine the above with the permission letters r, w, and x you can run chmod commands like
those shown below.
For example, to use chmod to set permissions of file "filename" to -rwxrwxrwx you could run:
chmod a=rwx filename
Breaking this down, the a means all and rwx means set read, write, and execute.
The = means that permissions are to be set to exactly what we specify.(i.e. we overwrite the current
permissions).
In this case you can get the same result more explicitly using either:
chmod ugo=rwx filename
or
chmod ugo+=rwx filename
Regarding just the symbolic mode part of the command, here are a few more examples:
To add read permission for all:
a+r
To remove permissions for all:
a-r
To add execute permissions for all:
a+x
To remove execute permissions for all:
a-x
To assign read, write permissions only for user and group:
ug=rw
To add read, write permissions to user and group to the permissions that already exist:
ug+=rw
To remove execute permissions from group and other (i.e from all users except the file's owner):
go-x
To remove permissions to do anything from all users except the owner:
go=
Note in the examples above and in general that there are different combinations that have the same
effect.
Changing Special Modes Using Symbols
The setuid bit can be set using:
u+s
The setuid bit can be removed using:
u-s
Similarly, setgid can be set using:
g+s
and removed using:
67
II B.C.A.-‘C’ (2016-2019) KGCAS
g-s
The sticky bit can be set by using:
+t
and removed using:
-t
Using Numeric Modes With Chmod
To set the permissions of a file or directory using numeric modes, simply use the format:
chmod OCTAL-MODE FILENAME
where OCTAL-MODE is the octal form of the permissions.
For example, to set the permissions of filename to -rw-r--r-- you could run the command:
chmod 644 filename
or to change permissions to -rwxrwxrwx you could use the command:
chmod 777 filename
Be careful when setting permissions to 777 as this means every single user account can read, write, and
execute that file.
Special Mode Bits
The setuid, setgid, and sticky bit can be set using chmod where
 1 = sticky bit
 2 = setgid
 4 = setuid
For example to set the setuid bit along with permissions 766:
chmod 4766 filename
To set the setgid bit along with 776:
chmod 2776 filename
To set sticky bit along with 766:
chmod 1776 fileanme
To set both setuid(2) and setgid(4) along with 766, prepend with 6. i.e. 2+4:
chmod 6766 filename
Umask - Configuring Default File / Directory Permissions
When a user creates a file, how does the system determine that file's initial permissions?
This is done based on the user's umask value.
The umask value specifies which permissions are not to be set.
In Ubuntu, the default umask value for a normal user is 002, while the default for root is 022.
You can find out the current umask value (or set it) using the umask command.
If (as a normal user) you run the command:
umask
You'll see something like 0002 displayed, however octal numbers are preceded by a 0 (in the same way hex
would be preceded by 0x), so the umask value itself is actually 002.
This value is an octal (base 8, digits 0-7) value which is subtracted from a base value of 777 for directories,
or subtracted from a base value of 666 for files.
A umask of 002 basically means don't remove any permissions from the base value for "user" or "group",
but "other" is not allowed write permission (write permission is octal 2, or binary 010 meaning -w-).
So if we create a new file:
touch newfile.txt
The file permissions for this new file will be 666-002 = 664, i.e. rw-rw-r-- (readable and writeable by user
and group, but only readable by everyone else).
Similarly, if we create a new directory:
mkdir newDir
The file permissions for the directory newDir will be 777-002 = 775, i.e. drwxrwxr-x (readable, writeable,
executable by user and group, but only readable and executable by everyone else).

68
II B.C.A.-‘C’ (2016-2019) KGCAS
If you wish to set the umask value to something else, simply use umask command like so:
umask newvalue
where "newvalue" is an octal number representing which permissions you do not want to be set when files
are created.

Example 1: The file should have read, write and execute permissions to user, read and execute permissions
to group and read, and execute permissions to others.
read, write and execute permissions to user =7
read and execute permissions to group =5
read and execute permissions to others=5
So total permissions will be 755
chmod 755 filename
Example 2: Providing write access to a user
chmod u+w filename
Example 3: Adding write permissions to a group
chmod g+w filename
Example 4: Adding executable permissions to others
chmod o+x filename
Example 5: Adding executable and write permissions to all
chmod a+wx filename
Example 6: Replicating user permissions to a group
chmod u=g filename
Example 7: Removing execute permissions to a user
Chmod u-x filename
Example 8: Adding execute permissions to others
Chmod o+x

Linux Variables
Rules to Define Variables in Shell Scripting
 Variable Must be Start/Begin with Letter
 In Variable space are not allowed
 Instead of spaces we can make use of under_scores
 Special Characters Like @ # are not allowed
 Do not use Hyphen (-)
 Do not Start Variable with Number
 To Pass Commands to Variable use $(Command) Or `Command`
 Define Local variable using local in front of variable (.i.e. local variable)
 Variable must be defined starting for the script, If not local variable.
 Variable are case sensitive
Read-only Variables
Shell provides a way to mark variables as read-only by using the read-only command. After a variable is
marked read-only, its value cannot be changed.
For example, the following script generates an error while trying to change the value of NAME −

#!/bin/sh
NAME="Zara Ali"
readonly NAME
NAME="Qadiri"
The above script will generate the following result −
/bin/sh: NAME: This variable is read only.
Unsetting Variables
Unsetting or deleting a variable directs the shell to remove the variable from the list of variables that it
tracks. Once you unset a variable, you cannot access the stored value in the variable.
69
II B.C.A.-‘C’ (2016-2019) KGCAS
Following is the syntax to unset a defined variable using the unset command −

unset variable_name
The above command unsets the value of a defined variable. Here is a simple example that demonstrates
how the command works −

#!/bin/sh
NAME="Zara Ali"
unset NAME
echo $NAME
The above example does not print anything. You cannot use the unset command to unset variables that are
marked readonly.
Variable Types
When a shell is running, three main types of variables are present −
 Local Variables − A local variable is a variable that is present within the current instance of the
shell. It is not available to programs that are started by the shell. They are set at the command
prompt.
 Environment Variables − An environment variable is available to any child process of the shell.
Some programs need environment variables in order to function correctly. Usually, a shell script
defines only those environment variables that are needed by the programs that it runs.
 Shell Variables − A shell variable is a special variable that is set by the shell and is required by the
shell in order to function correctly. Some of these variables are environment variables whereas
others are local variables.

What are Environment variables?


Environment variables are dynamic values which affect the processes or programs on a computer. They exist
in every operating system, but types may vary. Environment variables can be created, edited, saved, and
deleted and give information about the system behavior.
Environment variables can change the way a software/programs behave.
E.g. $LANG environment variable stores the value of the language that the user understands. This value is
read by an application such that a Chinese user is shown a Mandarin interface while an American user is
shown an English interface.
Let's study some common environment variables -

Variable Description

PATH This variable contains a colon (:)-separated list of directories in which your system looks for
executable files. When you enter a command on terminal, the shell looks for the command in
different directories mentioned in the $PATH variable. If the command is found, it executes.
Otherwise, it returns with an error 'command not found'.

USER The username

HOME Default path to the user's home directory

EDITOR Path to the program which edits the content of files

70
II B.C.A.-‘C’ (2016-2019) KGCAS

UID User's unique ID

TERM Default terminal emulator

SHELL Shell being used by the user

Accessing Variable values


In order to determine value of a variable, use the command
echo $VARIABLE
Variables are- Case Sensitive. Make sure that you type the variable name in the right letter case otherwise
you may not get the desired results.

The 'env' command displays all the environment variables.

Set New Environment Variables


You can create your own user defined variable, with syntax
VARIABLE_NAME= variable_value
Again, bear in mind that variables are case-sensitive and usually they are created in upper case.

Deleting Variables
The following syntax can be used to remove a Variable from the system.
unset variablename

71
II B.C.A.-‘C’ (2016-2019) KGCAS

This would remove the Variable and its value permanently.


Summary:
 Environment variables govern the behavior of programs in your Operating System.

Command Description

echo $VARIABLE To display value of a variable

env Displays all environment variables

VARIABLE_NAME= variable_value Create a new variable

unset Remove a variable

export Variable=value To set value of an environment variable

Let’s see few Examples about Variables Global and Local Variables
Below are few different types of variables defined as per above rules, Let‘s see what happens when we
execute below variable script.

#!/bin/bash
##Purpose: Define and execute Variable. Few Examples.
##Date: 19th Oct 2016
##Author: Ankam Ravi Kumar
##WebSite: https://arkit.co.in
##Start
## GLOBAL VARIABLE
A=10
B=22
HOST=$(hostname)
HOST1=`hostname`
1value=222
false@var=false
hyphen-a=wrong
echo "Variable A Value: $A"
echo "Variable B Vaule: $B"
echo "Variable HOST value: $HOST"
echo "Variable HOST1 value: $HOST1"
echo "Wrong Variable 1value $1value"
echo 'False @ Variable' $false@var
echo "hyphen-a Variable Value: $hyphen-a"
##END

ravi@TechTutorials:/script$ sh variables.sh
variables.sh: 14: variables.sh: 1value=222: not found

72
II B.C.A.-‘C’ (2016-2019) KGCAS

variables.sh: 15: variables.sh: false@var=false: not found


variables.sh: 16: variables.sh: hyphen-a=wrong: not found
Variable A Value: 10
Variable B Vaule: 22
Variable HOST value: TechTutorials
Variable HOST1 value: TechTutorials
Wrong Variable 1value value
False @ Variable @var
hyphen-a Variable Value: -a

If you can see above shell script executed, It is showing an errors where variable rules are not matching.

14 1value=222
15 false@var=false
16 hyphen-a=wrong

Rule Says do to start variable with numbers, Do not use special symbols in variable and do not use hyphen -.
So they are giving an errors.
Case sensitive Variable Examples
Whenever we are writing Variable we have to be careful if you write upper case and lower case each and
every character will differ because it is case sensitive.
NOTE: Best practice is to write shell script variable in UPPER case.

#!/bin/bash
## Variable is Case sensitive
## Author: Ankam Ravi Kumar
## WebSite: https://arkit.co.in
## START
NAME=Ravi
NaMe=Kumar
nAmE=arkit
echo "NAME Variable: $NAME"
echo "NaMe Variable: $NaMe"
echo "nAmE Variable $nAmE"
## END

Executing script

ravi@TechTutorials:/script$ sh var1.sh
NAME Variable: Ravi
NaMe Variable: Kumar
nAmE Variable arkit

Shell Scripting Tutorial : Defining Local Variables


Local Variable will work only within function or within condition statement only. If your calling local
variable from outside of function or outside of statement it will not work. Let‘s see the example here.

ravi@TechTutorials:/script$ cat localvar.sh


#!/bin/bash
## Local Variable Example
## Date: 19th Oct 2016
## Author: Ankam Ravi Kumar
## Global Variable
VAR1=456
## Start
ravi ()
{
73
II B.C.A.-‘C’ (2016-2019) KGCAS

local VAR=123
echo "Local Var: $VAR"
}
ravi
echo "VAR Value $VAR"
echo "Global Variable: $VAR1"
## END

As per the above shell script code, i have defined one global variable called VAR1. Local Variable called
VAR.
I am calling local and global variable after the function let‘s see the output of it. Shell Scripting Tutorial

ravi@TechTutorials:/script$ sh localvar.sh
Local Var: 123
VAR Value
Global Variable: 456

Above script output does not printed variable VAR, because local variable can‘t be called wherever we
want.

Unix / Linux - Special Variables


In this chapter, we will discuss in detail about special variable in Unix. In one of our previous chapters, we
understood how to be careful when we use certain nonalphanumeric characters in variable names. This is
because those characters are used in the names of special Unix variables. These variables are reserved for
specific functions.
For example, the $ character represents the process ID number, or PID, of the current shell −

$echo $$

The above command writes the PID of the current shell −

29949
The following table shows a number of special variables that you can use in your shell scripts −

S.No. Variable & Description

1 $0
The filename of the current script.

2 $n
These variables correspond to the arguments with which a script was invoked.
Here n is a positive decimal number corresponding to the position of an argument
(the first argument is $1, the second argument is $2, and so on).

3 $#
The number of arguments supplied to a script.

4 $*
All the arguments are double quoted. If a script receives two arguments, $* is
equivalent to $1 $2.

5 $@
All the arguments are individually double quoted. If a script receives two
arguments, $@ is equivalent to $1 $2.

74
II B.C.A.-‘C’ (2016-2019) KGCAS

6 $?
The exit status of the last command executed.

7 $$
The process number of the current shell. For shell scripts, this is the process ID
under which they are executing.

8 $!
The process number of the last background command.

Command-Line Arguments
The command-line arguments $1, $2, $3, ...$9 are positional parameters, with $0 pointing to the actual
command, program, shell script, or function and $1, $2, $3, ...$9 as the arguments to the command.
Following script uses various special variables related to the command line −

#!/bin/sh
echo "File Name: $0"
echo "First Parameter : $1"
echo "Second Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"
Here is a sample run for the above script −
$./test.sh Zara Ali
File Name : ./test.sh
First Parameter : Zara
Second Parameter : Ali
Quoted Values: Zara Ali
Quoted Values: Zara Ali
Total Number of Parameters : 2
Special Parameters $* and $@
There are special parameters that allow accessing all the command-line arguments at once. $* and $@ both
will act the same unless they are enclosed in double quotes, "".
Both the parameters specify the command-line arguments. However, the "$*" special parameter takes the
entire list as one argument with spaces between and the "$@" special parameter takes the entire list and
separates it into separate arguments.
We can write the shell script as shown below to process an unknown number of commandline arguments
with either the $* or $@ special parameters −

#!/bin/sh
for TOKEN in $*
do
echo $TOKEN
done
Here is a sample run for the above script −
$./test.sh Zara Ali 10 Years Old
Zara
Ali
10
Years
Old
75
II B.C.A.-‘C’ (2016-2019) KGCAS
Note − Here do...done is a kind of loop that will be covered in a subsequent tutorial.
Exit Status
The $? variable represents the exit status of the previous command.
Exit status is a numerical value returned by every command upon its completion. As a rule, most
commands return an exit status of 0 if they were successful, and 1 if they were unsuccessful.
Some commands return additional exit statuses for particular reasons. For example, some commands
differentiate between kinds of errors and will return various exit values depending on the specific type of
failure.
Following is the example of successful command −
$./test.sh Zara Ali
File Name : ./test.sh
First Parameter : Zara
Second Parameter : Ali
Quoted Values: Zara Ali
Quoted Values: Zara Ali
Total Number of Parameters : 2
$echo $?
0
$

UNIT – V
Using Conditional Execution in Shell Scripts: Conditional Execution – The case…esac Construct.
Managing repetitive tasks using Shell Scripts: Using Iteration in Shell Scripts – The while construct –
until construct – for construct – break and continue commands – Simple Programs using Shell
Scripts.
Decision Control Structures in Shell Scripts

Most of our programming languages today are able to make decisions based on conditions we set. A
condition is an expression that evaluates to a Boolean value - true or false. Any programmer can make his
program smart based on the decision and logic he puts into his program. The bash shell
supports if and switch (case)decision statements.
If statement
If is a statement that allows the programmer to make a decision in the program based on conditions he
specified. If the condition is met, the program will execute certain lines of code otherwise, the program will
execute other tasks the programmer specified. The following is the supported syntax of the if statement in
the bash shell.
General Syntax
Single decision:

if <condition>
then
### series of code goes here
fi

Double decision:

if <condition>
then
### series of code if the condition is satisfied

76
II B.C.A.-‘C’ (2016-2019) KGCAS
else
### series of code if the condition is not satisfied
fi

Multiple if condition:

if <condition1>
then
### series of code for condition1
elif <condition2>
then
### series of code for condition2
else
### series of code if the condition is not satisfied
fi

Single-bracket syntax

if [ condition ]
then
### series of code goes here
fi

Double-bracket syntax

if ((condition))
then
### series of code goes here
fi

The single bracket syntax is the oldest supported syntax in bash shell. It is used together with all conditional
statements in Linux. Meanwhile, the double-parenthesis syntax is used for a number-based conditional
statement to provide a familiar syntax to programmers. All types of ifstatements need a specified condition
in order to execute a task.
Conditional Statements in Linux
Conditional statements are used together with a decision control statement. There are different types of
conditional statements that you can use in the bash shell, the most common ones are: file-based, string-based
and arithmetic-based conditions.
File-based condition
File-based conditions are unary expressions and often used to examine a status of a file. The following list
shows the most commonly used file-based conditions in the bash shell.
OperatorDescription
-a file Returns true if file exists
-b file Returns true if file exists and is a block special file
-c file Returns true if file exists and is a character special file
-d file Returns true if file exists and is a directory
-e file Returns true if file exists
-r file Returns true if file exists and is readable
-s file Returns true if file exists and has a greater size that zero
-s file Returns true if file exists and has a greater size that zero
-w file Returns true if file exists and is writable
-x file Returns true if file exists and is executable
-N file Returns true if the file exists and has been modified since it was last read
77
II B.C.A.-‘C’ (2016-2019) KGCAS
Making a file-based decision
Let's have an example that will illustrate how to construct a file-based decision in a bash script. In this
example, we will create a script that determines whether the file exists in the home directory.

#!/bin/bash
cd
ls
if [ -e sample.sh ]
then
echo "file exists!"
else
echo "file does not exist"
fi

In this example, we ensured that the shell will return to home directory regardless of our current active
directory using the cd command. Additionally, the ls command is used to show the list of files in the
directory for us to verify the file really exists or not. As you can see, the script outputs text "file exists!"
because the sample.sh is in the home directory.

Note: The shell compiler is very strict in terms of syntax especially with spaces. There should be a space
between if and the open bracket and in between brackets and the condition.

Now let's improve our script by making our code more dynamic by allowing user to input a script name and
determine the permissions of the given file.

#!/bin/bash
cd
ls -l
read -p "Enter a file name: " filename
if [ -e $filename ]
then
echo "file exists!"
if [ -r $filename ]
then
status="readable "
fi
if [ -w $filename ]
then
status=$status"writable "
fi
if [ -x $filename ]

78
II B.C.A.-‘C’ (2016-2019) KGCAS
then
status=$status"executable"
fi
echo "file permission: "$status
else
echo "file does not exist"
fi

String-based Condition
Making decisions based on a string user-input is also possible in the bash shell. The string-based condition
returns a binary expression as a result meaning, it returns true if the specified condition is satisfied
otherwise, it returns false. The following are the commonly-used string-based conditional operators:
OperatorDescription
== Returns true if the strings are equal
!= Returns true if the strings are not equal
-n Returns true if the string to be tested is not null
-z Returns true if the string to be tested is null
Let's create a sample script using string-based conditional statement. The script will allow the user to input
two strings and assess whether one of the strings is null, both string are equal and not equal.

#!/bin/bash

read -p "First String: " str1


read -p "Second String: " str2
if [ -z "$str1" ]
then
echo "The 1st string is null"
elif [ -z "$str2" ]
then
echo "The 2nd string is null"
else
if [ $str1 == $str2 ]
then
echo "The strings are equal"
else
echo "The strings are not equal"
fi
fi

79
II B.C.A.-‘C’ (2016-2019) KGCAS
Arithmetic-based Condition
The shell provides several ways in declaring an arithmetic-based condition. First is by using mnemonics that
can be used together with the old-style single-bracket syntax and the other one is using mathematics-friendly
symbols that can be used together with the double parenthesis
Below is the list of available mnemonics for arithmetic-based conditional statements in shell:
OperatorUsage/Description
-eq Equal
-ge Greater Than or Equal
-gt Greater Than
-le Less Than or Equal
-lt Less Than
-ne Not Equal
Let's create a script that accepts an integer from the user and determines whether the integer is a zero,
negative, odd or an even number.

#!/bin/bash
read -p "Enter an integer: " int1
if [ $int1 -eq 0 ]
then
echo "Zero"
elif [ $int1 -lt 0 ]
then
echo "Negative"
else
if [ $((int1%2)) -eq 0 ]
then
echo "Even"
else
echo "Odd"
fi
fi

Arithmetic operator for double-parenthesis syntax:


OperatorUsage/Description
== Equal
>= Greater Than or Equal
> Greater Than
<= Less Than or Equal
< Less Than
!= Not Equal
Now, let's reconstruct our previous script and use double-parenthesis syntax:

#!/bin/bash
read -p "Enter an integer: " int1
if (( $int1 == 0 ))
then
echo "Zero"
elif (( $int1 < 0 ))
then

80
II B.C.A.-‘C’ (2016-2019) KGCAS
echo "Negative"
else
if (( $((int1%2)) == 0 ))
then
echo "Even"
else
echo "Odd"
fi
fi

Switch Statement
The switch statement is another kind of conditional statement in shell scripting. It allows a programmer to
compare several values against a variable in an easier way compared to if conditional statement. The syntax
for switch statement is:
case in
<pattern1>)
##series of code for pattern1
;;
<pattern2>)
##series of code for pattern2
;;
<patternN>)
##series of code for patternN
;;
*)
##default statements
esac

The pattern is the possible value of the variable. Each pattern is separated with a double semi-colon which
serves as a break statement of the pattern. The switch statement is closed with an esac statement.

#!/bin/bash
clear
read -p "Integer1: " int1
read -p "Integer2: " int2
echo "======================"
printf "Menu: \n[a] Addition\n[b]Subtraction\n[c]Multiplication\n[d]Division\n"
echo "======================"
read -p "Your choice: " choice
res=0
case $choice in
a)
res=$((int1+int2))
;;
b)
res=$((int1-int2))
;;
c)
res=$((int1*int2))
;;

81
II B.C.A.-‘C’ (2016-2019) KGCAS
d)
res=$((int1/int2))
;;
*)
echo "Invalid input"
esac
echo "The result is: " $res

The basic rules of conditions


When you start writing and using your own conditions, there are some rules you should know to prevent
getting errors that are hard to trace. Here follow three important ones:

1. Always keep spaces between the brackets and the actual check/comparison. The following won‘t work:
if [$foo -ge 3]; then
Bash will complain about a ―missing `]'‖.

2. Always terminate the line before putting a new keyword like “then”. The
words if, then, else,elif and fi are shell keywords, meaning that they cannot share the same line. Put a ―;‖
between the previous statement and the keyword or place the keyword on the start of a new line. Bash will
throw errors like ―syntax error near unexpected token `fi'‖ if you don‘t.
3. It is a good habit to quote string variables if you use them in conditions, because otherwise they are likely to
give trouble if they contain
spaces and/or newlines. By quoting I mean:
if [ "$stringvar" == "tux" ]; then
There are a few cases in which you should not
quote, but they are rare. You will see one of them further on in the tutorial.
Also, there are two things that may be useful to know:

1. You can invert a condition by putting an ―!‖ in front of it. Example:


if [ ! -f regularfile ]; then
Be sure to place the ―!‖ inside the brackets!

2. You can combine conditions by using certain operators. For the single-bracket syntax that we‘ve been using
so far, you can use ―-a‖ for and and ―-o‖ for or. Example:
if [ $foo -ge 3 -a $foo -lt 10 ]; then
The above condition will return true if $foo contains an integer greater than or equal to 3 and LessThan 10.
You can read more about these combining expressions at the respective condition syntaxes.
And, one more basic thing: don‘t forget that conditions can also be used in other statements,
likewhile and until. It is outside the scope of this tutorial to explain those, but you can read about them at
the Bash Guide for Beginners.

Unix / Linux - Shell Loop Types

82
II B.C.A.-‘C’ (2016-2019) KGCAS
In this chapter, we will discuss shell loops in Unix. A loop is a powerful programming tool that enables you
to execute a set of commands repeatedly. In this chapter, we will examine the following types of loops
available to shell programmers −
 The while loop
 The for loop
 The until loop
 The select loop
You will use different loops based on the situation. For example, the while loop executes the given
commands until the given condition remains true; the untilloop executes until a given condition becomes
true.
Once you have good programming practice you will gain the expertise and thereby, start using appropriate
loop based on the situation. Here, while andfor loops are available in most of the other programming
languages like C,C++ and PERL, etc.

Unix / Linux Shell - The while Loop


The while loop enables you to execute a set of commands repeatedly until some condition occurs. It is
usually used when you need to manipulate the value of a variable repeatedly.
Syntax
while command
do
Statement(s) to be executed if command is true
done
Here the Shell command is evaluated. If the resulting value is true, givenstatement(s) are executed.
If command is false then no statement will be executed and the program will jump to the next line after the
done statement.
Example
Here is a simple example that uses the while loop to display the numbers zero to nine −
#!/bin/sh
a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
Upon execution, you will receive the following result −
0
1
2
3
4
5
6
7
8
9
Each time this loop executes, the variable a is checked to see whether it has a value that is less than 10. If
the value of a is less than 10, this test condition has an exit status of 0. In this case, the current value of a is
displayed and latera is incremented by 1.
Unix / Linux Shell - The for Loop
The for loop operates on lists of items. It repeats a set of commands for every item in a list.
Syntax
for var in word1 word2 ... wordN
do
83
II B.C.A.-‘C’ (2016-2019) KGCAS

Statement(s) to be executed for every word.


done
Here var is the name of a variable and word1 to wordN are sequences of characters separated by spaces
(words). Each time the for loop executes, the value of the variable var is set to the next word in the list of
words, word1 to wordN.
Example
Here is a simple example that uses the for loop to span through the given list of numbers −
#!/bin/sh
for var in 0 1 2 3 4 5 6 7 8 9
do
echo $var
done
Upon execution, you will receive the following result −
0
1
2
3
4
5
6
7
8
9
Following is the example to display all the files starting with .bash and available in your home. We will
execute this script from my root −
#!/bin/sh
for FILE in $HOME/.bash*
do
echo $FILE
done
The above script will produce the following result −
/root/.bash_history
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
Unix / Linux Shell - The until Loop
The while loop is perfect for a situation where you need to execute a set of commands while some
condition is true. Sometimes you need to execute a set of commands until a condition is true.
Syntax
until command
do
Statement(s) to be executed until command is true
done
Here the Shell command is evaluated. If the resulting value is false, givenstatement(s) are executed. If
the command is true then no statement will be executed and the program jumps to the next line after the
done statement.
Example
Here is a simple example that uses the until loop to display the numbers zero to nine −
#!/bin/sh
a=0
until [ ! $a -lt 10 ]
84
II B.C.A.-‘C’ (2016-2019) KGCAS

do
echo $a
a = `expr $a + 1`
done
Upon execution, you will receive the following result −
0
1
2
3
4
5
6
7
8
9
Unix / Linux Shell - The select Loop
The select loop provides an easy way to create a numbered menu from which users can select options. It is
useful when you need to ask the user to choose one or more items from a list of choices.
Syntax
select var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done
Here var is the name of a variable and word1 to wordN are sequences of characters separated by spaces
(words). Each time the for loop executes, the value of the variable var is set to the next word in the list of
words, word1 towordN.
For every selection, a set of commands will be executed within the loop. This loop was introduced
in ksh and has been adapted into bash. It is not available in sh.
Example
Here is a simple example to let the user select a drink of choice −
#!/bin/ksh
select DRINK in tea cofee water juice appe all none
do
case $DRINK in
tea|cofee|water|all)
echo "Go to canteen"
;;
juice|appe)
echo "Available at home"
;;
none)
break
;;
*) echo "ERROR: Invalid selection"
;;
esac
done
The menu presented by the select loop looks like the following −
$./test.sh
1) tea
2) cofee
3) water
85
II B.C.A.-‘C’ (2016-2019) KGCAS

4) juice
5) appe
6) all
7) none
#? juice
Available at home
#? none
$
You can change the prompt displayed by the select loop by altering the variable PS3 as follows −
$PS3 = "Please make a selection => " ; export PS3
$./test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
Please make a selection => juice
Available at home
Please make a selection => none
$
Nesting Loops
All the loops support nesting concept which means you can put one loop inside another similar one or
different loops. This nesting can go up to unlimited number of times based on your requirement.
Here is an example of nesting while loop. The other loops can be nested based on the programming
requirement in a similar way −
Nesting while Loops
It is possible to use a while loop as part of the body of another while loop.
Syntax
while command1 ; # this is loop1, the outer loop
do
Statement(s) to be executed if command1 is true
while command2 ; # this is loop2, the inner loop
do
Statement(s) to be executed if command2 is true
done
Statement(s) to be executed if command1 is true
done
Example
Here is a simple example of loop nesting. Let's add another countdown loop inside the loop that you used to
count to nine −
#!/bin/sh
a=0
while [ "$a" -lt 10 ] # this is loop1
do
b="$a"
while [ "$b" -ge 0 ] # this is loop2
do
echo -n "$b "
b=`expr $b - 1`
done
echo
86
II B.C.A.-‘C’ (2016-2019) KGCAS

a=`expr $a + 1`
done
This will produce the following result. It is important to note how echo -n works here. Here -n option lets
echo avoid printing a new line character.
0
10
210
3210
43210
543210
6543210
76543210
876543210
9876543210

Unix / Linux - Shell Loop Control


In this chapter, we will discuss shell loop control in Unix. So far you have looked at creating loops and
working with loops to accomplish different tasks. Sometimes you need to stop a loop or skip iterations of
the loop.
In this chapter, we will learn following two statements that are used to control shell loops−
 The break statement
 The continue statement
The infinite Loop
All the loops have a limited life and they come out once the condition is false or true depending on the
loop.
A loop may continue forever if the required condition is not met. A loop that executes forever without
terminating executes for an infinite number of times. For this reason, such loops are called infinite loops.
Example
Here is a simple example that uses the while loop to display the numbers zero to nine −
#!/bin/sh
a=10
until [ $a -lt 10 ]
do
echo $a
a=expr $a + 1`
done
This loop continues forever because a is always greater than or equal to 10and it is never less than 10.
The break Statement
The break statement is used to terminate the execution of the entire loop, after completing the execution of
all of the lines of code up to the break statement. It then steps down to the code following the end of the
loop.
Syntax
The following break statement is used to come out of a loop −
break
The break command can also be used to exit from a nested loop using this format −
break n
Here n specifies the nth enclosing loop to the exit from.
Example
Here is a simple example which shows that loop terminates as soon as abecomes 5 −
#!/bin/sh
87
II B.C.A.-‘C’ (2016-2019) KGCAS

a=0
while [ $a -lt 10 ]
do
echo $a
if [ $a -eq 5 ]
then
break
fi
a=`expr $a + 1`
done
Upon execution, you will receive the following result −
0
1
2
3
4
5
Here is a simple example of nested for loop. This script breaks out of both loops if var1 equals 2 and var2
equals 0 −
#!/bin/sh
for var1 in 1 2 3
do
for var2 in 0 5
do
if [ $var1 -eq 2 -a $var2 -eq 0 ]
then
break 2
else
echo "$var1 $var2"
fi
done
done
Upon execution, you will receive the following result. In the inner loop, you have a break command with
the argument 2. This indicates that if a condition is met you should break out of outer loop and ultimately
from the inner loop as well.
10
15

The continue statement


The continue statement is similar to the break command, except that it causes the current iteration of the
loop to exit, rather than the entire loop.
This statement is useful when an error has occurred but you want to try to execute the next iteration of the
loop.
Syntax
continue
Like with the break statement, an integer argument can be given to the continue command to skip
commands from nested loops.
continue n
Here n specifies the nth enclosing loop to continue from.

88
II B.C.A.-‘C’ (2016-2019) KGCAS
Example
The following loop makes use of the continue statement which returns from the continue statement and
starts processing the next statement −
#!/bin/sh

NUMS="1 2 3 4 5 6 7"

for NUM in $NUMS


do
Q=`expr $NUM % 2`
if [ $Q -eq 0 ]
then
echo "Number is an even number!!"
continue
fi
echo "Found odd number"
done
Upon execution, you will receive the following result −
Found odd number
Number is an even number!!
Found odd number
Number is an even number!!
Found odd number
Number is an even number!!
Found odd number

Linux Shell Practical Example Programs


Q-1. Presumably, What Could Be The Reason That The <Cat Filename> Command Start Giving The
Following Error Message?
--bash: cat: Command not found
Ans.
The command not found error occurs when either the PATH variable is corrupt or have some issue in
configuration.
Hence, it‘ll throw the error message as mentioned in the question.

Q-2. How Can We Pass Arguments To A Script In Linux? And How To Access These Arguments From
Within The Script?
Ans.
We can write a bash script that can accept arguments from the command line in the following manner.
$ ./scriptName "arg1" "arg2"..."argn"
To access these arguments, we can use the positional parameters ($1 to $9) in the script. Each parameter
corresponds to the position of the argument on the command line.
The first argument is read by the shell into the parameter $1, the second argument into $2, and so on. After
$9, start enclosing the arguments in brackets like ${10}, ${11}, and ${12}.
Some shells don‘t support the above method. In that case, to access parameters with numbers greater than 9,
you can use the shift command. It moves the parameter list to the left.
$1 is lost, $2 becomes $1, $3 becomes $2, and so on. The inaccessible 10th parameter becomes $9 and
becomes accessible.
Let‘s check out an example for clarity.
#!/bin/bash
# Call this script with at least 3 parameters
echo "First parameter is $1"
echo "Second parameter is $2"
89
II B.C.A.-‘C’ (2016-2019) KGCAS
echo "Third parameter is $3"
exit 0
Upon execution, the test script will yield the following output.
# sh parameters.sh 50 51 52
First parameter is 50
Second parameter is 51
Third parameter is 52

Q-3. What Is The Difference Between $* And $@?


Ans.
$@ treats each quoted arguments as separate arguments but $* considers the entire set of positional
parameters as a single string.

Q-4. What Is The Command To Display The List Of Files In A Directory?


Ans.
The following command displays the list of files in a directory.
$ ls -lrt | grep ^-

Q-5. Write A Shell Script To Display The Last Updated File Or The Newest File In A Directory?
Ans.
Following is the test script to list the most recently changed file.
#!/bin/bash
ls -lrt | grep ^- | awk 'END{print $NF}'

Q-6. Write A Shell Script To Get The Current Date, Time, Username And Current Working Directory.
Ans.
Let‘s create a file named <stats.sh> and add the following code in it.
#!/bin/bash
echo "Hello, $LOGNAME"
echo "Current date is 'date'"
echo "User is 'who i am'"
echo "Current directory 'pwd'"
First of all, assign the execute permission for the script and then execute it. It‘ll display the required
information.

Q-7. Write A Shell Script That Adds An Extension ―.New‖ To All The Files In A Directory.
Ans.
Please use the following script to change all the files in a directory to a ―.new‖ extension. Kindly make sure
to supply the directory name as an argument while running the test script.
#!/bin/bash
dir=$1
for file in `ls $1/*`
do
mv $file $file.new
done

Q-8. Write A Shell Script To Print A Number In Reverse Order. It Should Support The Following
Requirements.
 The script should accept the input from the command line.
 If you don‘t input any data, then display an error message to execute the script correctly.
Ans.
Let‘s first, jot down the steps involved in the test script.
1. Suppose the input number is n.
2. Set reverse and single digit to 0 (i.e. rev=0, sd=0).
90
II B.C.A.-‘C’ (2016-2019) KGCAS
3. The expression (n % 10) will give the single left most digit i.e. sd.
4. To reverse the number, use this expression <rev * 10 + sd>.
5. Decrease the input number (n) by 1.
6. If n is greater than 0, then go to step no. 3. Else, execute the step no. 7.
7. Print the result.
The script code is as follows.
#!/bin/bash
if [ $# -ne 1 ]
then
echo "Please provide the correct input in the below format."
echo "Usage: $0 number"
echo " This script will reverse the given number."
echo " For eg. $0 1234, will print 4321"
exit 1
fi

n=$1
rev=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
rev=`expr $rev \* 10 + $sd`
n=`expr $n / 10`
done
echo "Reverse number is $rev"
There are two ways in which we can run the above script.
Instance 1:
If there is no input, you will get the following output.
$ ./testscript.sh
Please provide the correct input in the below format.
Usage: ./testscript.sh number
This script will reverse the given number.
For eg. ./testscript.sh 1234, will print 4321
Instance 2:
When the input is available in the command line Argument, you will get the following output.
$ ./testscript.sh 12345
Reverse number is 54321

Q-9. How Will You Delete A File Which Has Special Characters In Its File Name?
Ans.
In Linux or Unix-like system, you may come across file names including the following special characters,
white spaces, backslashes and more.
-
--
;
&
$
?
*
Bash shell considers most of the above special characters as commands. Thus, the ―rm‖ command may not
be able to delete such files. The simplest way to delete files having special characters in its name is by using
the inode number.
Step-1.
The <-i> option of <ls> command displays the index number (inode) of each file.
91
II B.C.A.-‘C’ (2016-2019) KGCAS
$ ls -li
9966571 -rw-r--r-- 1 techbeamers users 0 Sep 16 10:05
Step-2.
Use the <find> command given below to delete the file, if it has inode number such as 9966571.
$ find . -inum 9966571 -exec rm -i {} \;

Q-10. How To Ask For Input In A Shell Script From The Terminal?
Ans:
In Linux, we can use the <read> command to take input from the user. It reads data from the terminal as the
user enters it from the keyboard. And stores into a variable.
Let‘s see a sample test script featuring the <read> command.
#!/bin/bash
echo ‗Please enter your name‘
read name
echo ―My Name is $name‖
Upon running the above script, it‘ll prompt for the name and assigns the input value to the variable <name>.
$ ./test.sh
Please enter your name
TechBeamers
My Name is TechBeamers

Q-11. How Can We Perform Numeric Comparisons In Linux?


Ans.
The test command can perform various types of numeric comparison using the following operators.
1. <Eq>
Syntax: INTEGER1 -eq INTEGER2
Description: INTEGER1 is equal to INTEGER2
Example Code:
#!/bin/bash
read -p "Please enter number 20 from the keyboard : " n
if test $n -eq 20
then
echo "Thanks for entering the number 20."
else
echo ―You are not following my instructions.‖
fi
2. <Ge>
Syntax: INTEGER1 -ge INTEGER2
Description: INTEGER1 is greater than or equal to INTEGER2
Example Code:
#!/bin/bash
read -p "Enter a number >= 10 : " n
if test $n -ge 10
then
echo "Correct value entered."
else
echo ―You are not following my instructions.‖
fi
3. <Gt>
Syntax: INTEGER1 -gt INTEGER2
Description: INTEGER1 is greater than INTEGER2
Example Code:
#!/bin/bash
read -p "Enter number > 20 : " n
if test $n -gt 20
92
II B.C.A.-‘C’ (2016-2019) KGCAS
then
echo "$n is greater than 20."
else
echo ―You are not following my instructions.‖
fi
4. <Le>
Syntax: INTEGER1 -le INTEGER2
Description: INTEGER1 is less than or equal to INTEGER2
Example Code:
#!/bin/bash
read -p "Enter backup level : " n
if test $n -le 6
then
echo "Incremental backup requested."
fi
if test $n -eq 7
then
echo "Full backup requested."
else
echo ―You are not following my instructions.‖
fi
5. <Lt>
Syntax: INTEGER1 -lt INTEGER2
Description: INTEGER1 is less than INTEGER2
Example Code:
#!/bin/bash
read -p "Do not enter negative number here : " n
if test $n -lt 0
then
echo "You entered a negative number!!"
else
echo ―You are not following my instructions.‖
fi
6. <Ne>
Syntax: INTEGER1 -ne INTEGER2
Description: INTEGER1 is not equal to INTEGER2
Example Code:
#!/bin/bash
read -p "Do not enter number 5 here : " n
if test $n -ne 5
then
echo "Thanks for not entering number 5."
else
echo ―You are not following my instructions.‖
fi

Q-12. What Is The Syntax For Different Types Of Loops Available In Shell Scripting?
Ans.
Following are the loops supported in shell scripting.
1. <For Loop>.
#!/bin/bash
for i in $( ls ); do
echo item: $i
done

93
II B.C.A.-‘C’ (2016-2019) KGCAS
2. <While Loop>.
#!/bin/bash
COUNT=0
while [ $COUNT -lt 10 ]; do
echo The counter value is $COUNT
let COUNT+=1
done
3. <Until Loop>.
#!/bin/bash
COUNT=20
until [ $COUNT -lt 10 ]; do
echo The counter value is $COUNT
let COUNT-=1
done
4. <Select Loop>.
#!/bin/bash
PS3="Select a week day (1-7): "
select i in mon tue wed thur fri sat sun exit
do
case $i in
mon) echo "Monday";;
tue) echo "Tuesday";;
wed) echo "Wednesday";;
thur) echo "Thursday";;
fri) echo "Friday";;
sat) echo "Saturday";;
sun) echo "Sunday";;
exit) exit;;
esac
done

Q-13. How Will You Find The Sum Of All Numbers In A File In Linux?
Ans.
Following is the script which computes the sum of all numbers stored in a file.
#!/bin/bash
awk '{x+=$0}END{print x}' filename

Q-14. Write A Shell Script To Delete The Lines Containing A Word <Dd> If It Appears Between The 5th
And 7th Position?
Ans.
Let‘s take a fixed width file as:
ff1 ff2 ff3
sds dd fd
sd ss ew
dd dd se
The expected output after script execution is.
ff1 ff2 ff3
sd ss ew
The test script to do the expected task is as follows.
for i in 'cat test.txt'
do
j = 'expr length $i'
if [[ ( "$j" == 5 || "$j" == 7 ) ]]
then
v = 'echo $i | grep dd'
94
II B.C.A.-‘C’ (2016-2019) KGCAS
if [ "$v" != "" ]
then
echo $v
sed -i "s/$v/lalat/g" test.txt
fi
fi
done

Q-15. Write A Shell Script To Find Out The Unique Words In A File And Also Count The Occurrence Of
Each Of These Words. We Can Say That File Under Consideration Contains Many Lines, And Each Line
Has Multiple Words.
Ans.
$ cat animal.txt
tiger bear
elephant tiger bear
bear
Following test script/command will count the unique words.
$ awk '{for(i=1;i<=NF;i++)a[$i]++;}END{for(i in a){print i, a[i];}}' animal.txt
Output:
elephant 1
bear 3
tiger 2

Q-16. Write A Shell Script To Get The Total Count Of The Word ―Linux‖ In All The ―.Txt‖ Files And Also
Across Files Present In Subdirectories.
Ans.
The following is the test script/command which recursively searches for the ―.txt‖ files and returns the total
occurrences of a word <Linux>.
$ find . -name *.txt -exec grep -c Linux '{}' \; | awk '{x+=$0;}END{print x}'

Q-17. Write A Shell Script To Validate Password Strength. Here Are A Few Assumptions For The Password
String.
 Length – minimum of 8 characters.
 Contain both alphabet and number.
 Include both the small and capital case letters.
If the password doesn‘t comply to any of the above conditions, then the script should report it as a <Weak
Password>.
Ans.
#Password Validation Script
echo "Enter your password"
read password
len="${#password}"
if test $len -ge 8 ; then
echo "$password" | grep -q [0-9]
if test $? -eq 0 ; then
echo "$password" | grep -q [A-Z]
if test $? -eq 0 ; then
echo "$password" | grep -q [a-z]
if test $? -eq 0 ; then
echo "Strong Password"
else
echo "Weak Password -> Should include a lower case letter."
fi
else
95
II B.C.A.-‘C’ (2016-2019) KGCAS
echo "Weak Password -> Should include a capital case letter."
fi
else
echo "Weak Password -> Should use numbers in your password."
fi
else
echo "Weak Password -> Password length should have at least 8 characters."
fi

Q-18. Write A Shell Script To Print The Count Of Files And Subdirectories In The Specified Directory.
Ans.
#!/bin/bash
if [ -d "$@" ]; then
echo "Files found: $(find "$@" -type f | wc -l)"
echo "Folders found: $(find "$@" -type d | wc -l)"
else
echo "[ERROR] Please retry with a folder."
exit 1
fi

Q-19. Write A Shell Script To Print The Reverse Of An Input Number.


Ans.
#!/bin/bash
if [ $# -eq 1 ]; then
if [ $1 -gt 0 ]; then
num=$1
revNum=0
while [ $num -ne 0 ]
do
testnum=$(( $num % 10 ))
revNum=$(( $revNum * 10 + $testnum ))
num=$(( $num / 10 ))
done
echo "Reverse Number: $revNum of $1"
else
echo "Input is less than 0, retry with a different number."
fi
else
echo "ERROR: Retry with one parameter."
fi

Q-20. Write A Shell Script To Reverse The List Of Strings And Reverse Each String Further In The List.
Ans.
#!/bin/bash
if [ $# != 0 ]; then
len=`echo $@ | wc -c`
len=`expr $len - 1`
strrev=""
while test $len -gt 0
do
strrev1=`echo $@ | cut -c$len`
strrev=$strrev$strrev1
len=`expr $len - 1`
done
echo $strrev
96
II B.C.A.-‘C’ (2016-2019) KGCAS
else
echo "ERROR: Retry with a list of strings."
fi

97

You might also like