0% found this document useful (0 votes)
124 views

History, Linux Foundation, Linux Requirements, Linux Components

This document provides an introduction to the CS-110 Introduction to Linux class at Chitkara University. It includes an index listing topics such as Linux history, distributions, features, architecture, and differences between Windows and Linux. The document discusses the Linux foundation, requirements, events, and the history of Linux from its inception in 1991 to today, where it powers many servers, smartphones, supercomputers and more. It also summarizes the major Linux distribution families and provides examples of distributions from the Fedora and SUSE families.

Uploaded by

Just Movies
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)
124 views

History, Linux Foundation, Linux Requirements, Linux Components

This document provides an introduction to the CS-110 Introduction to Linux class at Chitkara University. It includes an index listing topics such as Linux history, distributions, features, architecture, and differences between Windows and Linux. The document discusses the Linux foundation, requirements, events, and the history of Linux from its inception in 1991 to today, where it powers many servers, smartphones, supercomputers and more. It also summarizes the major Linux distribution families and provides examples of distributions from the Fedora and SUSE families.

Uploaded by

Just Movies
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/ 510

Introduction to Linux (CS-110) Class

On
History, Linux Foundation, Linux
Requirements, Linux Components

Dated: 05-07-2021

Department of Computer Science and Engineering,


Chitkara University, Punjab

Jatin Arora Introduction to Linux G25


Index
1. Introduction to Linux
2. Linux Foundation
3. Linux Requirements in Today’s World
4. Linux History
5. Distributions
6. Features
7. Linux Architecture
8. Kernel
9. Difference between Windows and Linux (Task for Students)

Jatin Arora Introduction to Linux G25 2


Introduction to Linux
Since its inception in 1991, Linux has grown to become a major force in computing
- powering everything from the New York Stock Exchange, to mobile phones,
supercomputers, and consumer devices.
The Linux Foundation partners with the world's leading developers and companies
to solve the hardest technology problems and accelerate open technology
development and commercial adoption. The Linux Foundation makes it its mission
to provide experience and expertise to any initiative working to solve complex
problems through open source collaboration, providing the tools to scale open
source projects: security best practices, governance, operations and ecosystem
development, training and certification, licensing, and promotion.
Linux is the world's largest and most pervasive open source software project in
history. The Linux Foundation is home to Linux creator Linus Torvalds and lead
maintainer Greg Kroah-Hartman, and provides a neutral home where Linux kernel
development can be protected and accelerated for years to come. The success
of Linux has catalyzed growth in the open source community, demonstrating the
commercial efficacy of open source and inspiring countless new projects across all
industries and levels of the technology stack.

Jatin Arora Introduction to Linux G25


Linux Requirements
The Linux Foundation's work today extends far beyond Linux, fostering innovation at
every layer of the software stack. The Linux Foundation is the umbrella organization
for many critical open source projects that power corporations today, spanning all
industry sectors:

1. Networking: OpenDaylight, OPNFV


2. Embedded: Dronecode
3. Web tools: JS Foundation, Node.js
4. Cloud computing: Cloud Foundry, Cloud Native Computing
Foundation, Open Container Initiative
5. Automotive: Automotive Grade Linux
6. Security: The Core Infrastructure Initiative
7. Blockchain: Hyperledger
8. And many more.

Jatin Arora Introduction to Linux G25


Linux Foundation Events

The Linux Foundation Events


1. Open Source Summit North America, Europe, Japan, and China
2. MesosCon North America, Europe, and China
3. Embedded Linux Conference/OpenIoT Summit North America and Europe
4. Open Source Leadership Summit
5. Automotive Linux Summit
6. Apache: Big Data North America & ApacheCon
7. KVM Forum
8. Linux Storage Filesystem and Memory Management Summit
9. Vault
10. Open Networking Summit.

Jatin Arora Introduction to Linux G25


History of Linux
Linux is a free open source computer operating system, initially developed for Intel
x86-based personal computers. It has been subsequently ported to many other
hardware platforms.
In this section, you will become familiar with how Linux evolved from a student
project into a massive effort with an enormous impact on today's world.

Linus Torvalds was a student in Helsinki, Finland, in 1991, when he started a


project: writing his own operating system kernel. He also collected together and/or
developed the other essential ingredients required to construct an entire operating
system with his kernel at the center. Soon, this became known as
the Linux kernel.
In 1992, Linux was re-licensed using the General Public License (GPL) by GNU (a
project of the Free Software Foundation or FSF, which promotes freely available
software), which made it possible to build a worldwide community of developers. By
combining the kernel with other system components from the GNU project,
numerous other developers created complete systems called Linux
Distributions in the mid-90’s.

Jatin Arora Introduction to Linux G25


History Cont..
Linux is open source software.

Being open source does not just mean that you can see how the system works.
You can also experiment with changes and share them freely for others to use. The
open source model means that improvements are easier to make, enabling faster
innovation.
Linux provides easy access to a powerful and scriptable command-line interface
(CLI).
Linux was built around the basic design philosophy that users can perform all
administration tasks from the CLI. It enables easier automation, deployment, and
provisioning, and simplifies both local and remote system administration. Unlike
other operating systems, these capabilities have been built in from the beginning,
and the assumption has always been to enable these important capabilities.
Linux is a modular operating system that allows you to easily replace or remove
components.
Components of the system can be upgraded and updated as needed. A Linux
system can be a general-purpose development workstation or an extremely
stripped-down software appliance.

Jatin Arora Introduction to Linux G25


The Linux distributions created in the mid-90s
provided the basis for fully free computing and
became a driving force in the open source
software movement. In 1998, major companies
like IBM and Oracle announced their support for
the Linux platform and began major development
efforts as well.
Today, Linux powers more than half of the servers
on the Internet, the majority of smartphones (via
the Android system, which is built on top of
Linux), and nearly all of the world’s most powerful
supercomputers.

Jatin Arora Introduction to Linux G25


Linux distribution
In order to fully benefit from this course, you will need to have at least
one Linux distribution installed (if you are not already familiar with the term
distribution, as it relates to Linux, you soon will be!).
On the next screen, you will learn some more details about the many
available Linux distributions and the families they can be considered to
belong to. Because there are literally hundreds of distributions, we have not
covered them all in this course. Instead, we have decided to focus on the three
major distribution families, and we have chosen one specific distribution from
within each family to use for all illustrations, examples, and exercises. This is
not meant to suggest that we endorse these specific distributions; they were
simply chosen because they are fairly widely used and each is broadly
representative of its respective family.
The families and representative distributions we are using are:
Debian Family Systems (such as Ubuntu)
SUSE Family Systems (such as openSUSE)
Fedora Family Systems (such as CentOS)

Jatin Arora Introduction to Linux G25


Desktop Environment

Jatin Arora Introduction to Linux G25


Example of Linux Distribution

Jatin Arora Introduction to Linux G25


The Fedora Family
Fedora is the community distribution that forms the
basis of Red Hat Enterprise
Linux (RHEL), CentOS, Scientific Linux,
and Oracle Linux. Fedora contains significantly
more software than Red Hat’s enterprise version.
One reason for this is that a diverse community is
involved in building Fedora, with many contributors
who do not work for Red Hat. Furthermore, it is
used as a testing platform for future RHEL releases.
In this course, CentOS is used for activities, demos,
and labs because it is available at no cost to the end
user and has a much longer release cycle
than Fedora (which releases a new version every
six months or so).
For this reason, we have standardized
the Fedora part of this course material on CentOS
7. The basic version of CentOS is also virtually
identical to RHEL, the most popular Linux
distribution in enterprise environments.
Jatin Arora Introduction to Linux G25
Fedora distribution
Some of the key facts about the Fedora distribution family are:
• The Fedora family is upstream for CentOS, RHEL, and Oracle
Linux.
• Kernel version 3.10 is used in RHEL/CentOS 7.
• It supports hardware platforms such as x86, x86-64, Itanium,
PowerPC, and IBM System z.
• It uses the RPM-based yum package manager (we cover it in
more detail later) to install, update, and remove packages in the
system.
• RHEL is widely used by enterprises which host their own
systems.

SUSE
The relationship between SUSE, SUSE Linux Enterprise Server
(SLES), and openSUSE is similar to the one described
between Fedora, Red Hat Enterprise Linux, and CentOS. In this
case, we decided to use openSUSE-Leap-42.2 as the reference
distribution for the SUSE family, as it is available to end users at no
cost. Because the two products are extremely similar, the material
that covers openSUSE can typically be applied to SLES with few
problems.
Jatin Arora Introduction to Linux G25
Some of the key facts about the SUSE family are listed below:
SUSE Linux Enterprise Server (SLES) is upstream
for openSUSE.
Kernel version 4.4 is used in openSUSE-Leap-42.2.
It uses the RPM-based zypper package manager (we cover
it in more detail later) to install, update, and remove
packages in the system.
It includes the YaST (Yet Another Setup Tool) application
for system administration purposes.
SLES is widely used in retail and other sectors.

Jatin Arora Introduction to Linux G25


Debian Family
The Debian distribution is upstream for several other distributions,
including Ubuntu. In turn, Ubuntu is upstream for Linux Mint and
a number of other distributions. It is commonly used on both
servers and desktop computers. Debian is a pure open source
project and has a strong focus on stability.
Debian provides by far the largest and most complete software
repository to its users of any Linux distribution.
Ubuntu aims at providing a good compromise between long term
stability and ease of use. Since Ubuntu gets most of its packages
from Debian’s stable branch, Ubuntu also has access to a very
large software repository. For those reasons, we will use Ubuntu
21.0 LTS (Long Term Support) as the reference Debian
family distribution for this course. Ubuntu is a registered
trademark of Canonical Ltd. and is used throughout this course
with their permission.

Jatin Arora Introduction to Linux G25


The material produced by The Linux Foundation
is distribution-flexible. This means that technical
explanations, labs, and procedures should work on almost all
most modern distributions. While choosing between available
Linux systems, you will notice that the technical differences
are mainly about package management systems, software
versions, and file locations. Once you get a grasp of those
differences, it becomes relatively painless to switch from one
Linux distribution to another.
The desktop environment used for this course is GNOME.

Jatin Arora Introduction to Linux G25


Philosphy of Linux
Every organization or project has a philosophy that works as a guide while framing
its objectives and delineating its growth path. This section contains a description of
the Linux philosophy and how this philosophy has impacted its development.
Linux is constantly enhanced and maintained by a network of developers from all
over the world collaborating over the Internet, with Linus Torvalds at the head.
Technical skill and a desire to contribute are the only qualifications for participating.

Is Linux and Unix are same


Linux borrows heavily from the UNIX operating system because it was written to
be a free and open source version of UNIX. Files are stored in a hierarchical
filesystem, with the top node of the system being root or simply "/". Whenever
possible, Linux makes its components available via files or objects that look like
files. Processes, devices, and network sockets are all represented by file-like
objects, and can often be worked with using the same utilities used for regular
files.
Linux is a fully multitasking (i.e., multiple threads of execution are performed
simultaneously), multiuser operating system, with built-in networking and service
processes known as daemons in the UNIX world.

Jatin Arora Introduction to Linux G25


Linux Community
Suppose that, as part of your job, you need to configure a Linux file server, and you
run into some difficulties. If you are not able to figure out the answer yourself or get
help from a co-worker, the Linux community might just save the day! There are many
ways to engage with the Linux community: you can post queries on relevant
discussion forums, subscribe to discussion threads, and even join local Linux groups
that meet in your area.
The Linux community is a far-reaching ecosystem consisting of developers, system
administrators, users and vendors, who use many different forums to connect with
one another. Among the most popular are:
• Linux User Groups (both local and online)
• Internet Relay Chat (IRC) software (such as Pidgin and XChat)
• Online communities and discussion boards
• Newsgroups and mailing lists, including the Linux Kernel Mailing List
• Community events (such as Open Source Summits and Embedded Linux
Conferences)
One of the most powerful online user communities is linux.com. This site is hosted
by The Linux Foundation and serves over one million unique visitors every month. It
has active sections on:
• News, Community discussion threads, Free tutorials and user tips.

Jatin Arora Introduction to Linux G25


Linux Distributions
As illustrated below, the Linux kernel is the core of a computer operating
system. A full Linux distribution consists of the kernel plus a number of other
software tools for file-related operations, user management, and software
package management. Each of these tools provides a small part of the complete
system. Each tool is often its own separate project, with its own developers
working to perfect that piece of the system.

Jatin Arora Introduction to Linux G25


As mentioned earlier, the current Linux kernel (as well as earlier release
versions) can be found at www.kernel.org .. The various Linux distributions
may be based on different kernel versions. For example, the very
popular RHEL 8 distribution is based on the 3.10 kernel, which is not new , but
is extremely stable. Other distributions may move more quickly in adopting the
latest kernel releases. It is important to note that the kernel is not an all or
nothing proposition, for example, RHEL 8 and CentOS 7 have incorporated
many of the more recent kernel improvements into their older version, as
have Ubuntu, openSUSE, SLES, etc.
Examples of other essential tools and ingredients provided by distributions
include the C/C++ compiler, the gdb debugger, the core system libraries
applications need to link with in order to run, the low-level interface for drawing
graphics on the screen, as well as the higher-level desktop environment, and the
system for installing and updating the various components, including the kernel
itself.

Jatin Arora Introduction to Linux G25


The vast variety of Linux distributions are designed to cater to many different audiences and
organizations, according to their specific needs and tastes. However, large organizations, such
as companies and governmental institutions and other entities, tend to choose the major
commercially-supported distributions from Red Hat, SUSE, and Canonical (Ubuntu).
CentOS is a popular free alternative to Red Hat Enterprise Linux (RHEL) and is often used
by organizations that are comfortable operating without paid technical
support. Ubuntu and Fedora are popular in the educational realm. Scientific Linux is favored
by the scientific research community for its compatibility with scientific and mathematical
software packages. Both CentOS and Scientific Linux are binary-compatible with RHEL; i.e.,
binary software packages in most cases will install properly across the distributions.
Many commercial distributors, including Red Hat, Ubuntu, SUSE, and Oracle, provide long
term fee-based support for their distributions, as well as hardware and software certification. All
major distributors provide update services for keeping your system primed with the latest
security and bug fixes, and performance enhancements, as well as provide online support
resources.
Jatin Arora Introduction to Linux G25
Linux Terminology
When you start exploring Linux, you will soon come across some
unfamiliar terms, like distribution, boot loader, desktop environment, etc.
So, before we proceed further, let's stop and take a look at some basic
terminology used in Linux to help you get up to speed.
Play Video

Jatin Arora Introduction to Linux G25


Quiz Time
What are the qualifications required for participating in Linux kernel development?
1. Technical skills
2. A desire to contribute
3. Citizen of the U.S.
4. University degree in computer science

Which of the following communication channels does the Linux community use to communicate?
1. cnn.com
2. IRC Channels
3. Mailing Lists
4. Community Events

What is a kernel?
1. The glue between hardware and applications.
2. A collection of software making up a Linux-based Operating System.
3. A program that runs as a background process.
4. A graphical subsystem on nearly all Linux systems.

What is a desktop environment?


1. A collection of software making up a Linux-based Operating System.
2. A program that runs as a background process.
3. A graphical subsystem on nearly all Linux systems.
4. A graphical user interface on top of the Operating System.

When did Linus Torvalds start writing the Linux kernel?


1. 1952
2. 1991
3. 2002
4. 2010 Jatin Arora Introduction to Linux G25
Activity Time
You should be able to log in to a Linux system using the GNOME 3 desktop environment, and run commands from a shell
prompt in a terminal program.
Ensure that the workstation virtual machine is running. Perform the following tasks on workstation.
Log in to workstation as student using student as the password.
On workstation, at the GNOME login screen, click the student user account. Enter student when prompted for the
password.
Click Sign In.
Important
Log out and log back in as student.
Click the system menu in the upper-right corner.
Select Student User → Log Out.
Click Log Out in the confirmation dialog box that displays.
At the GNOME login screen, click the student user account. Enter password when prompted for the password.
Click Sign In.
Lock the screen.
From the system menu in the upper-right corner, press the lock screen button at the bottom of the menu.
Unlock the screen.
Press Enter to lift the lock screen curtain.
In the Password field, enter 55TurnK3y as the password.
Click Unlock.
Determine how to shut down workstation from the graphical interface, but Cancel the operation without shutting down the
system. From the system menu in the upper-right corner, click the power button at the bottom of the menu. A dialog box
displays with the options to either Restart or Power Off the machine.
Click Cancel in the dialog box that displays.

Jatin Arora Introduction to Linux G25


Practice Time

All the students are required to practice


Question on Ubuntu OS till 11:15 am.
In case of any issues, you can come over
for discussion on MS Teams.
We will start the next topic at 11:30 am

Jatin Arora Introduction to Linux G25


Thank
You
Jatin Arora Introduction to Linux G25
Configuration & Customizations of Linux, Linux
Structure and Installation. Linux file-system basics,
the boot process

Mr. Vivek Bhardwaj


Department of Computer Science and Engineering
Chitkara University, Punjab
ITL CS110 1
Content

The boot process


Configuration & Customizations of Linux
Linux Structure and Installation
Linux file-system basics

ITL CS110 Mr. Jatin Arora - G25 2


The Boot Process
• Have you ever wondered what happens in the background from the time
you press the Power button until the Linux login prompt appears?
• The Linux boot process is the procedure for initializing the system. It
consists of everything that happens from when the computer power is
first switched on until the user interface is fully operational.
• Once you start using Linux, you will find that having a good
understanding of the steps in the boot process may help you with
troubleshooting problems, as well as with tailoring the computer's
performance to your needs.
• On the other hand, the boot process can be rather technical. You may
want to come back and study this section later, if you want to first
get a good feel for how to use a Linux system.

ITL CS110 3
ITL CS110 4
BIOS - The First Step

• Starting an x86-based Linux system involves a


number of steps. When the computer is powered
on, the Basic Input/Output
System (BIOS) initializes the hardware, including
the screen and keyboard, and tests the main
memory. This process is also called POST (Power
On Self Test).
• The BIOS software is stored on a ROM chip on
the motherboard. After this, the remainder of the
boot process is controlled by the operating system
(OS).

ITL CS110 Mr. Jatin Arora - G25 5


Master Boot Record (MBR)

• Once the POST is completed, the system control passes from


the BIOS to the boot loader. The boot loader is usually stored on
one of the hard disks in the system, either in the boot sector (for
traditional BIOS/MBR systems) or the EFI partition (for more
recent (Unified) Extensible Firmware
Interface or EFI/UEFI systems). Up to this stage, the machine does
not access any mass storage media. Thereafter, information on the
date, time, and the most important peripherals are loaded from
the CMOS values (after a technology used for the battery-powered
memory store - which allows the system to keep track of the date
and time even when it is powered off).

ITL CS110 Mr. Jatin Arora - G25 6


• A number of boot loaders exist for Linux; the most common ones
are GRUB (for GRand Unified Boot loader) and ISOLINUX (for
booting from removable media), and DAS U-Boot (for booting on
embedded devices/appliances). Most Linux boot loaders can present
a user interface for choosing alternative options for booting Linux,
and even other operating systems that might be installed. When
booting Linux, the boot loader is responsible for loading the kernel
image and the initial RAM disk or filesystem (which contains some
critical files and device drivers needed to start the system) into
memory.

ITL CS110 Mr. Jatin Arora - G25 7


ITL CS110 Mr. Jatin Arora - G25 8
The boot loader has two distinct stages:
First Stage:
• For systems using the BIOS/MBR method, the boot loader resides at
the first sector of the hard disk, also known as the Master Boot
Record (MBR). The size of the MBR is just 512 bytes. In this stage,
the boot loader examines the partition table and finds a bootable
partition. Once it finds a bootable partition, it then searches for the
second stage boot loader e.g, GRUB, and loads it
into RAM (Random Access Memory).
• For systems using the EFI/UEFI method, UEFI firmware reads
its Boot Manager data to determine which UEFI application is to
be launched and from where (i.e., from which disk and partition
the EFI partition can be found). The firmware then launches
the UEFI application, for example, GRUB, as defined in the boot
entry in the firmware's boot manager. This procedure is more
complicated, but more versatile than the older MBR methods.
ITL CS110 Mr. Jatin Arora - G25 9
Second Stage:
• The second stage boot loader resides under /boot. A splash screen is
displayed, which allows us to choose which Operating System (OS)
to boot. After choosing the OS, the boot loader loads the kernel of
the selected operating system into RAM and passes control to it.
• The boot loader loads the selected kernel image and passes control
to it. Kernels are almost always compressed, so its first job is to
uncompress itself. After this, it will check and analyze the system
hardware and initialize any hardware device drivers built into the
kernel.

ITL CS110 Mr. Jatin Arora - G25 10


ITL CS110 Mr. Jatin Arora - G25 11
Initial RAM Disk
• The initramfs filesystem image contains programs and binary files that
perform all actions needed to mount the proper root filesystem, like
providing kernel functionality for the needed filesystem and device
drivers for mass storage controllers with a facility
called udev (for User Device), which is responsible for figuring out
which devices are present, locating the drivers they need to operate
properly, and loading them. After the root filesystem has been found, it
is checked for errors and mounted.
• The mount program instructs the operating system that a filesystem is
ready for use, and associates it with a particular point in the overall
hierarchy of the filesystem (the mount point). If this is successful,
the initramfs is cleared from RAM and the init program on the root
filesystem (/sbin/init) is executed.
• init handles the mounting and pivoting over to the final real root
filesystem. If special hardware drivers are needed before the mass
storage can be accessed, they must be in the initramfs image.
ITL CS110 Mr. Jatin Arora - G25 12
Init RAM FS

ITL CS110 Mr. Jatin Arora - G25 13


• Near the end of the boot process, init starts a number of text-mode login
prompts. These enable you to type your username, followed by your
password, and to eventually get a command shell. However, if you are
running a system with a graphical login interface, you will not see these
at first.
• As you will learn in the Command Line Operations section, the
terminals which run the command shells can be accessed using
the ALT key plus a function key. Most distributions start six
text terminals and one graphics terminal starting with F1 or F2. Within a
graphical environment, switching to a text console requires
pressing CTRL-ALT + the appropriate function key
(with F7 or F1 leading to the GUI).

ITL CS110 Mr. Jatin Arora - G25 14


• Usually, the default command shell
is bash (the GNU Bourne Again Shell), but
there are a number of other advanced
command shells available. The shell prints a
text prompt, indicating it is ready to accept
commands; after the user types the
command and presses Enter, the command
is executed, and another prompt is displayed
after the command is done.

ITL CS110 Mr. Jatin Arora - G25 15


The Linux Kernel

• The boot loader loads both the kernel and


an initial RAM–based file system
(initramfs) into memory, so it can be
used directly by the kernel.
• When the kernel is loaded in RAM, it
immediately initializes and configures the
computer’s memory and also configures
all the hardware attached to the system.
This includes all processors, I/O
subsystems, storage devices, etc. The
kernel also loads some necessary user
space applications.

ITL CS110 Mr. Jatin Arora - G25 16


/sbin/init and Services
• Once the kernel has set up all its hardware and mounted the root
filesystem, the kernel runs the /sbin/init program. This then
becomes the initial process, which, in turn, starts other processes to
get the system running. Most other processes on the system trace
their origin ultimately to init; the exceptions are the kernel
processes, started by the kernel directly for managing internal
operating system details.
• Besides starting the system, init is responsible for keeping the
system running and for shutting it down cleanly. Whenever
necessary, one of its responsibilities is to act as manager for all non-
kernel processes; it cleans up after them upon completion, and
restarts user login services as needed when users log in and out, and
does the same for other background system services.

ITL CS110 Mr. Jatin Arora - G25 17


• Traditionally, this process startup was done
using conventions that date back to System V
UNIX, with the system passing through a
sequence of runlevels containing collections of
scripts that start and stop services. Each
runlevel supports a different mode of running
the system. Within each runlevel, individual
services can be set to run, or to be shut down if
running.
• However, all major recent distributions have
moved away from this sequential runlevel
method of system initialization, although they
usually support the System V conventions for
compatibility purposes. Next, we discuss the
new methods, systemd and Upstart.

ITL CS110 Mr. Jatin Arora - G25 18


Linux Filesystems

• Think of a refrigerator that has


multiple shelves that can be used for
storing various items. These shelves
help you organize the grocery items
by shape, size, type, etc. The same
concept applies to a filesystem, which
is the embodiment of a method of
storing and organizing arbitrary
collections of data in a human-usable
form.

ITL CS110 Mr. Jatin Arora - G25 19


• Different Types of Filesystems Supported by Linux:
• Conventional disk
filesystems: ext2, ext3, ext4, XFS, Btrfs, JFS, NTFS, etc.
• Flash storage filesystems: ubifs, JFFS2, YAFFS, etc.
• Database filesystems
• Special purpose filesystems: procfs, sysfs, tmpfs, debugfs, etc.
• This section will describe the standard filesystem layout shared by
most Linux distributions.

ITL CS110 Mr. Jatin Arora - G25 20


Partitions and Filesystems
A partition is a logical part of the disk, whereas a filesystem is a method
of storing/finding files on a hard disk (usually in a partition). By way of
analogy, you can think of filesystems as being like family trees that show
descendants and their relationships, while the partitions are like different
families (each of which has its own tree).

Windows Linux

Partition Disk1 /dev/sda1

Filesystem type NTFS/VFAT EXT3/EXT4/XFS/BTRFS...

Mounting Parameters DriveLetter MountPoint

Base Folder where OS is C:\ /


stored

ITL CS110 Mr. Jatin Arora - G25 21


The Filesystem Hierarchy Standard

ITL CS110 Mr. Jatin Arora - G25 22


Linux Installation: Planning
• A partition layout needs to be decided at the
time of installation because Linux systems
handle partitions by mounting them at specific
points in the filesystem. You can always
modify the design later, but it is always easier
to try and get it right to begin with.
• Nearly all installers provide a reasonable
filesystem layout by default, with either all
space dedicated to normal files on one big
partition and a smaller swap partition, or with
separate partitions for some space-sensitive
areas like /home and /var. You may need to
override the defaults and do something
different if you have special needs, or if you
want to use more than one disk.
ITL CS110 Mr. Jatin Arora - G25 23
Partition

ITL CS110 Mr. Jatin Arora - G25 24


Linux Installation: Install Source
Like other operating systems, Linux distributions are provided on
removable media such as USB drives and CDs or DVDs. Most Linux
distributions also support booting a small image and downloading the rest
of the system over the network. These small images are usable on media
or as network boot images, making it possible to install without any local
media at all.

ITL CS110 Mr. Jatin Arora - G25 25


• Install Virtual Box and Create a Machine.
• Browse iso image file and start installation.

Play Installation Video.

ITL CS110 Mr. Jatin Arora - G25 26


Requirements
• At least 2GB of free storage space.
• Have access to either a DVD or a USB flash drive containing the
version of Ubuntu Server you want to install.
• 50 GB HDD Space
Boot from install media
• Choose your language
• Choose the correct keyboard layout
• Choose your install

Download Ubuntu and its Steps


https://ubuntu.com/download/desktop
https://ubuntu.com/tutorials/install-ubuntu-desktop#1-overview
ITL CS110 Mr. Jatin Arora - G25 27
ITL CS110 Mr. Jatin Arora - G25 28
ITL CS110 Mr. Jatin Arora - G25 29
• choose the ’Something else’ option.

ITL CS110 Mr. Jatin Arora - G25 30


ITL CS110 Mr. Jatin Arora - G25 31
ITL CS110 Mr. Jatin Arora - G25 32
Quiz

Which one of the following component actually loads Linux?


a) Boot loader
b) Init
c) X Window System
d) BIOS
Which of the following is responsible for starting system and network services at boot time?
a) Kernel
b) boot loader
c) init
d) GRUB
Please select a possible correct full path for a file named photo.jpg on a Linux system.
a) /.data.user1.images.photo.jpg
b) /Data_user1_images_photo.jpg
c) /data/user1/images/photo.jpg
d) c:\data\user1\images\photo.jpg

ITL CS110 Mr. Jatin Arora - G25 33


Activity

Create Virtual Machine by specifying the following details


• RAM Size = Half of the Computer’s RAM
• Virtual HDD Size = Minimum 30 GM
Start Newly created Machine by clicking on Start
Browse the ubuntu.iso image and start the installation.

Repeat the New Machine creation and Ubuntu Installation 4 times.

ITL CS110 Mr. Jatin Arora - G25 34


Study basic & User status Unix/Linux Commands:
Purpose commands

Date 9th July 2021

Mr. Jatin Arora


Department of Computer Science and Engineering
Chitkara University, Punjab
ITL CS110 Mr. Jatin Arora - G25 1
Content
Introduction to the Command Line

Man
Help
History
Who, Whoami
id
uname, uptime,
free, tty, cal,
date, hostname, reboot,
clear, bc.
su, sudo

ITL CS110 Mr. Jatin Arora - G25 2


Command Line
The command line interface often automate and troubleshoot tasks in
the text environment.
Graphical user interfaces make easy tasks easier, while command line
interfaces make difficult tasks possible.
Linux relies heavily on the abundance of command line tools. The
command line interface provides the following advantages:
• No GUI overhead.
• Virtually every task can be accomplished using the command line.
• You can script tasks and series of procedures.
• You can sign into remote machines anywhere on the Internet.
• You can initiate graphical applications directly from the command
line.

ITL CS110 Mr. Jatin Arora - G25 3


General Purpose commands
There are some basic command line utilities that are used constantly, and
it would be impossible to proceed further without using some of them in
simple form before we discuss them in more detail. A short list has to
include:
su, sudo, man, help, history, who, whoami, id, uname, uptime, free, tty,
cal, date, hostname, reboot, clear, bc.

ITL CS110 Mr. Jatin Arora - G25 4


Command Syntax

Most input lines entered at the shell prompt have three basic elements:
• Command
• Options
• Arguments.

The command is the name of the program you are executing.


It may be followed by one or more options (or switches) that modify
what the command may do.
Options usually start with one or two dashes, for example, -p or --
print, in order to differentiate them from arguments, which represent
what the command operates on.

ITL CS110 Mr. Jatin Arora - G25 5


Text Terminal on the Graphical Desktop

A terminal emulator program emulates (simulates) a stand-alone


terminal within a window on the desktop. By this, we mean it behaves
essentially as if you were logging into the machine at a pure text
terminal with no running graphical interface. Most terminal emulator
programs support multiple terminal sessions by opening additional tabs
or windows.
By default, on GNOME desktop environments, the gnome-
terminal application is used to emulate a text-mode terminal in a
window. Other available terminal programs include:
• xterm
• rxvt
• konsole
• terminator

ITL CS110 Mr. Jatin Arora - G25 6


Launching Terminal Windows

• To open a terminal in Ubuntu 21.04 LTS:


– Click the Ubuntu icon and type terminal in the Search box.

ITL CS110 Mr. Jatin Arora - G25 7


Switching Between the GUI and the
Command Line
• Linux production servers are usually installed without X and even if it is
installed, usually do not launch it during system start up.
Removing X from a production server can be very helpful in maintaining
a lean system which can be easier to support and keep secure.
Virtual Terminals
Virtual Terminals (VT) are console sessions that use the entire display and
keyboard outside of a graphical environment. Such terminals are considered
"virtual" because although there can be multiple active terminals, only one
terminal remains visible at a time. A VT is not quite the same as a command
line terminal window; you can have many of those visible at once on a
graphical desktop.

Ubuntu uses VT 7, but CentOS/RHEL and openSUSE use VT 1 for the


graphical display.

ITL CS110 Mr. Jatin Arora - G25 8


switch between the VTs

• To switch between the VTs,


press CTRL-ALT-
corresponding function key for
the VT.

• For example, press


CTRL-ALT-F6 for VT 6.

ITL CS110 Mr. Jatin Arora - G25 9


sudo
sudo allows users to run programs using the security privileges of
another user, generally root (superuser). The functionality of sudo is
similar to that of run as in Windows.

sudo ls –l /root
sudo cat /etc/passwd

ITL CS110 Mr. Jatin Arora - G25 10


shutdown
• The preferred method to shut down or reboot the system is to use
the shutdown command. This sends a warning message and then
prevents further users from logging in.
• The init process will then control shutting down or rebooting the system.
It is important to always shut down properly; failure to do so can result in
damage to the system and/or loss of data.
• The halt and poweroff commands issue shutdown -h to halt the
system; reboot issues shutdown -r now and causes the machine to
reboot instead of just shutting down. Both rebooting and shutting down
from the command line requires superuser (root) access.
$ sudo shutdown -h 10:00 "Shutting down for scheduled maintenance."

ITL CS110 Mr. Jatin Arora - G25 11


Accessing Directories
When you first log into a system or open a terminal, the default directory
should be your home directory; you can print the exact path of this by
typing echo $HOME. (Many Linux distributions actually open
new graphical terminals in $HOME/Desktop.) The following commands
are useful for directory navigation:
Command Result
pwd Displays the present working directory
cd ~ or cd Change to your home directory (short-cut
name is ~ (tilde))
cd .. Change to parent directory (..)
cd - Change to previous directory (- (minus))

Play Basic Command Video


ITL CS110 Mr. Jatin Arora - G25 12
man

• The man pages are the most often-used source of Linux


documentation. They provide in-depth documentation about many
programs and utilities, as well as other topics, including
configuration files, and programming APIs for system calls, library
routines, and the kernel.
The syntax for using the man command is as follows
man [options] [command_name]
man ls
man -k passwd (Searching for man pages by keyword)
man -k ext4
man -k boot
man -k zip

ITL CS110 Mr. Jatin Arora - G25 13


Section of man Pages

The man command searches manual Secti Content type


sections in alphanumeric order. on
1 User commands (both executable and
For example, man shell programs)
passwd displays passwd(1) by default. 2 System calls (kernel routines invoked from
user space)
To display the man page topic from a 3 Library functions (provided by program
specific section, include the section libraries)
number argument: 4 Special files (such as device files)
5 File formats (for many configuration files
man 5 passwd displays passwd(5). and structures)
6 Games (historical section for amusing
programs)
7 Conventions, standards, and
miscellaneous (protocols, file systems)

8 System administration and privileged


commands (maintenance tasks)

ITL CS110 9
Mr. Jatin Arora - G25 Linux kernel API (internal kernel calls)14
Info
• Info documentation is comprehensive and hyperlinked. It is possible to
output info pages to multiple formats. By contrast, man pages are
optimized for printed output. Info nodes are read from the command
line, using the pinfo command.
• A typical man page has a small amount of content focusing on one
particular topic, command, tool, or file. The Info documentation is a
comprehensive document. Info provides the following improvements:
• One single document for a large system containing all the necessary
information for that system
• Hyperlinks
• A complete browsable document index
• A full text search of the entire document
[user@host ~]$ man tar
[user@host ~]$ pinfo tar
ITL CS110 Mr. Jatin Arora - G25 15
help

help command just displays information about shell built-in


commands. Here’s the syntax for it:
help ls
Or Help could be used as an option of command
ls –help

ITL CS110 Mr. Jatin Arora - G25 16


who command:
• This command will output the name of all the users who are
currently logged in on the system. It also shows some attributes like
terminal name and login time for all users.
user1@home:~$ who

whoami command:
• This is another command to check who is logged in on a particular
terminal. So it will return the details of the user logged in on that
particular terminal. whoami (without spaces) shows the login name
only without other details.
user1@home:~$ whoami
root //Name of the current user
ITL CS110 Mr. Jatin Arora - G25 17
history
Command History [user@host ~]$ history
...output omitted...
• The history command displays a 23 clear
24 who
list of previously executed 25 pwd
commands prefixed with a 26 ls /etc
27 uptime
command number. 28 ls -l
• The exclamation point character 29 date
30 history
(!) is a metacharacter that is used [user@host ~]$ !ls
ls -l
to expand previous commands total 0
without having to retype them. drwxr-xr-x. 2 user user 6 Mar 29 21:16 Desktop
...output omitted...
The !number command expands [user@host ~]$ !26
to the command matching the ls /etc
abrt hosts pulse
number specified. adjtime hosts.allow purple
aliases hosts.deny qemu-ga
• The !string command expands to ...output omitted...
the most recent command that
begins with the string specified.
ITL CS110 Mr. Jatin Arora - G25 18
date
• The date command displays the current date and time. It can also be
used by the superuser to set the system clock. An argument that begins
with a plus sign (+) specifies a format string for the date command.
[user@host ~]$ date
Sat Jan 26 08:13:50 IST 2019
[user@host ~]$ date +%R
08:13
[user@host ~]$ date +%x
01/26/2019
[user@host ~]$ cal 07 2021
[user@host ~]$ cal –y # current Year
[user@host ~]$ cal -3 # prev-current-next month cal
[user@host ~]$ cal 2007 # cal of particular year

ITL CS110 Mr. Jatin Arora - G25 19


Uname

Uname is the short name for 'UNIX s Kernel name – Linux


name'. Uname command works on all n Node name – HowLinux
Linux and Unix like operating r Kernel Release – 4.4.0-170-
systems. generic
v Kernel-Version – 199-Ubuntu
When you run Uname command SMP Thu Nov 14 01:45:04 UTC 2019
without any options it just prints the m Machine – x86_64
core operating system name. p Processor (if known) – x86_64
i Hardware platform (if
$ uname known) – x86_64
Linux o Operating System – GNU/Linux
$ uname –s 1) Kernel name a all information

Linux

ITL CS110 Mr. Jatin Arora - G25 20


Tty/uptime
The tty command will print the name of the device file that your
pseudo-teletype slave is using to interface to the master. And that,
effectively, is the number of your terminal window.
tty

Uptime Command In Linux: It is used to find out how long the


system is active (running). This command returns set of values that
involve, the current time, and the amount of time system is in running
state, number of users currently logged into, and the load time for the
past 1, 5 and 15 minutes respectively.
$uptime (give Options to Cover)
08:24:37 up 207 days, 11:10, 0 users, load average: 0.00, 0.03, 0.05

ITL CS110 Mr. Jatin Arora - G25 21


id

Id
The id command shows you the real and the effective user and group
IDs of a given user. Following is its syntax:

id –G Himanshu # Ids of all groups to whom Himanshu belongs


id –g Himanshu # Effective Group Id of Himanshu
id –u Himanshu # Effective User Id of Himanshu

ITL CS110 Mr. Jatin Arora - G25 22


Clear/free/bc/hostname/
• free

The figures are given in kibibytes


• clear
Type clear to clear all the previous commands that were ran in the current
terminal.
• Hostname It is the program that is used to either set or display the
current host, domain or node name of the system. These names are used
by many of the networking programs to identify the machine. (Explore
s, i, d option)
• bc BC stands for Basic Calculator, is a command in Bash that is used to
provide the functionality of a scientific calculator within a Bash script.

ITL CS110 Mr. Jatin Arora - G25 23


Quiz

1. Which key combination would allow you to switch from the GUI to the text virtual terminal
4?
a) CTRL-SHIFT-F4
b) CTRL-ALT-SHIFT-SUPER-F4
c) CTRL-ALT-F4
d) CTRL-SHIFT-F4
2. Assuming you are the root user, which commands allow you to shut down your
system without rebooting?
a) a) shutdown -r now
b) b) halt
c) c) poweroff
d) d) shutdown -h now
3. Command used to check the boot time of Machine
who –b
who –boot who –c boot

ITL CS110 Mr. Jatin Arora - G25 24


Activity
Editing the Command Line: Try the Useful Command-line
Editing Shortcuts on the terminal for lengthy commands

Shortcut Description
Ctrl+A Jump to the beginning of the command line.
Ctrl+E Jump to the end of the command line.
Ctrl+U Clear from the cursor to the beginning of the command
line.
Ctrl+K Clear from the cursor to the end of the command line.

Ctrl+LeftArrow Jump to the beginning of the previous word on the


command line.
Ctrl+RightArro Jump to the end of the next word on the command line.
w
Ctrl+R Search the history list of commands for a pattern.

ITL CS110 Mr. Jatin Arora - G25 25


Introduction to Linux (CS-110) Class
On
File System: Introduction to File system, File
System Architecture and File Types

Dated: Wednesday, July 14, 2021

Department of Computer Science and Engineering,


Chitkara University, Punjab

Ms. A.Sangeetha Introduction to Linux G27


Index
1. Introduction to File system
2. File system Architecture
3. File Types

Ms. A.Sangeetha Introduction to Linux G27 2


Linux file system

• A Linux file system is a structured collection of files on a disk drive or a


partition.
• A partition is a segment of memory and contains some specific data.
• In our machine, there can be various partitions of the memory.
Generally, every partition contains a file system.

The Role of the Linux File System

• The data is organized and can be easily located


• The data can be easily retrieved at any later point in time
• The integrity of the data is preserved

Ms. A.Sangeetha Introduction to Linux G27


Types of Linux File System

When we install the Linux operating system, Linux offers many file
systems such as Ext, Ext2, Ext3, Ext4, JFS, ReiserFS, XFS,
btrfs, and swap.

Ms. A.Sangeetha Introduction to Linux G27


Types of Linux File System

1. Ext, Ext2, Ext3 and Ext4 file system

The file system Ext stands for Extended File System. It was primarily
developed for MINIX OS. The Ext file system is an older version, and is
no longer used due to some limitations.

Ext2 is the first Linux file system that allows managing two terabytes of
data. Ext3 is developed through Ext2; it is an upgraded version of Ext2 and
contains backward compatibility. The major drawback of Ext3 is that it
does not support servers because this file system does not support file
recovery and disk snapshot.

Ext4 file system is the faster file system among all the Ext file systems. It is
a very compatible option for the SSD (solid-state drive) disks, and it is the
default file system in Linux distribution.

Ms. A.Sangeetha Introduction to Linux G27


Types of Linux File System

2. JFS File System


JFS stands for Journaled File System, and it is developed by IBM for
AIX Unix. It is an alternative to the Ext file system. It can also be used
in place of Ext4, where stability is needed with few resources. It is a
handy file system when CPU power is limited.

3. ReiserFS File System


ReiserFS is an alternative to the Ext3 file system. It has improved
performance and advanced features. In the earlier time, the ReiserFS
was used as the default file system in SUSE Linux, but later it has
changed some policies, so SUSE returned to Ext3. This file system
dynamically supports the file extension, but it has some drawbacks in
performance.

Ms. A.Sangeetha Introduction to Linux G27


Types of Linux File System

4. XFS File System


XFS file system was considered as high-speed JFS, which is developed for
parallel I/O processing. NASA still using this file system with its high
storage server (300+ Terabyte server).

5. Btrfs File System


Btrfs stands for the B tree file system. It is used for fault tolerance, repair
system, fun administration, extensive storage configuration, and more. It is
not a good suit for the production system.

6. Swap File System


The swap file system is used for memory paging in Linux operating system
during the system hibernation. A system that never goes in hibernate state is
required to have swap space equal to its RAM size.

Ms. A.Sangeetha Introduction to Linux G27


Linux File System Structure

• Linux file system has a hierarchal file structure


• Files in Unix System are organized into multi-level hierarchy structure
known as a directory tree.
• At the very top of the file system is a directory called “root” which is
represented by a “/”.
• All other directories can be accessed from the root directory.
• "On a UNIX system, everything is a file; if something is not a file, it is a
process."

Ms. A.Sangeetha Introduction to Linux G27


Linux File System Structure

Ms. A.Sangeetha Introduction to Linux G27


File Types

When navigating the Linux file system you are sure to encounter
different file types. The most used and obvious file types are regular
files and directories. However, the Linux operating system has more
to offer in terms of file types as it also includes another 5 file types.

Ms. A.Sangeetha Introduction to Linux G27


Linux file types

Seven different types of Linux file types :


1) - : regular file
2) d : directory
3) c : character device file
4) b : block device file
5) s : local socket file
6) p : named pipe
7) l : symbolic link

Ms. A.Sangeetha Introduction to Linux G27


Default color scheme

Color File type


blue directories

red compressed archives

white text files


pink images
cyan links
yellow devices
green executables
flashing red broken links

Ms. A.Sangeetha Introduction to Linux G27


Identifying Linux File types

There is only one command you need to know, which will help you to
identify and categorize all the seven different file types found on the Linux
system.

$ ls -ld <file name>


Here is an example output of the above command.
$ ls -ld /etc/services
-rw-r--r-- 1 root root 19281 Feb 14 2012 /etc/servi

ls -l | grep ^-

ls command will show the file type as an encoded symbol found as the
first character of the file permission part. In this case it is "-", which
means "regular file".

Ms. A.Sangeetha Introduction to Linux G27


Regular file

The regular file is a most common file type found on the Linux system. It
governs all different files such us text files, images, binary files, shared
libraries, etc. You can create a regular file with the touch command:

$ touch linux.com
$ ls -ld linux.com
-rw-rw-r-- 1 lubos lubos 0 Jan 10 12:52 linux.com

Ms. A.Sangeetha Introduction to Linux G27


Directory and Character device

Directory is second most common file type found in Linux. Directory can
be created with the mkdir command:

$ mkdir FileTypes
$ ls -ld FileTypes/
drwxrwxr-x 2 lubos lubos 4096 Jan 10 13:14 FileTypes/

Character and block device files allow users and programs to communicate
with hardware peripheral devices. For example:
$ ls -ld /dev/vmmon
crw------- 1 root root 10, 165 Jan 4 10:13 /dev/vmmon

Ms. A.Sangeetha Introduction to Linux G27


Block Device and Local domain
sockets

Block devices are similar to character devices. They mostly govern hardware
as hard drives, memory, etc.
$ ls -ld /dev/sda
brw-rw---- 1 root disk 8, 0 Jan 4 10:12 /dev/sda

Local domain sockets are used for communication between processes.


Generally, they are used by services such as X windows, syslog and etc.
$ ls -ld /dev/log
srw-rw-rw- 1 root root 0 Jan 4 10:13 /dev/log

Ms. A.Sangeetha Introduction to Linux G27


Named Pipes

Similarly as Local sockets, named pipes allow communication


between two local processes. They can be created by
the mknod command and removed with the rm command.

Ms. A.Sangeetha Introduction to Linux G27


Symbolic Links

With symbolic links an administrator can assign a file or directory


multiple identities. Symbolic link can be though of as a pointer to
an original file. There are two types of symbolic links:
•hard links
•soft links

Ms. A.Sangeetha Introduction to Linux G27


Soft link

 Can cross the file system,


 allows you to link between directories,
 has different inodes number and file permissions than original file,
 permissions will not be updated,
 has only the path of the original file, not the contents.

$ ln -s source.file softlink.file

Ms. A.Sangeetha Introduction to Linux G27


Hard Link

 can’t cross the file system boundaries,


 can’t link directories,
 has the same inodes number and permissions of original file,
 permissions will be updated if we change the permissions of source file,
 has the actual contents of original file, so that you still can view the
contents, even if the original file moved or removed.
 ln source.file hardlink.file

Ms. A.Sangeetha Introduction to Linux G27


MCQ

1. Which file system has journaling capability?


a) Ext2
b) Ext4
c) Isofs
d) Procfs

2. Which file contains the filesystems to be automatically mounted during boot?


a) /etc/mount
b) /etc/fstab
c) /etc/inittab
d) /etc/boot

Ms. A.Sangeetha Introduction to Linux G30


MCQ

3. Which one of the directory does not contain binary files?


a) /bin
b) /sbin
c) /etc
d) none of the mentioned

4. The /boot directory stores the data that is used


a) before the kernel begins executing user mode programs
b) after the kernel begins executing user mode programs
c) before the bootloader is loaded in the RAM
d) none of the mentioned

Ms. A.Sangeetha Introduction to Linux G27


Activity
Create a hard link named /home/student/backups/source.backup for the existing file,
/home/student/files/source.file.

View the link count for the file, /home/student/files/source.file.

[student@servera ~]$ ls -l files/source.file


total 4
-rw-r--r--. 1 student student 11 Mar 5 21:19 source.file
Create a hard link named /home/student/backups/source.backup. Link it to the file,
/home/student/files/source.file.

[student@servera ~]$ ln /home/student/files/source.file \


/home/student/backups/source.backup
Verify the link count for the original /home/student/files/source.file and the new linked
file, /home/student/backups/source.backup. The link count should be 2 for both files.

[student@servera ~]$ ls -l /home/student/files/


-rw-r--r--. 2 student student 11 Mar 5 21:19 source.file
[student@servera ~]$ ls -l /home/student/backups/
-rw-r--r--. 2 student student 11 Mar 5 21:19 source.backup
Ms. A.Sangeetha Introduction to Linux G27
Activity

Create a soft link named /home/student/tempdir that points to the /tmp


directory on servera.

Create a soft link named /home/student/tempdir and link it to /tmp.

[student@servera ~]$ ln -s /tmp /home/student/tempdir


Use the ls -l command to verify the newly created soft link.

[student@servera ~]$ ls -l /home/student/tempdir


lrwxrwxrwx. 1 student student 4 Mar 5 22:04 /home/student/tempdir -> /tmp

Ms. A.Sangeetha Introduction to Linux G27


Introduction to Linux (CS-110) Class
On
Creating and Manipulating files

Dated: 7/16/2021

Department of Computer Science and Engineering,


Chitkara University, Punjab

Jatin Arora Introduction to Linux G25


Index

1. cat-concatenate
2. cp-copy
3. mv-move
4. rm-remove
5. ls-list
6. touch
7. which is
8. where is
9. what is

Jatin Arora Introduction to Linux G25 2


Connecting to a Unix/Linux system

Open up a terminal:

The “prompt”
• Open up a terminal:

The current directory (“path”)

The host

Jatin Arora Introduction to Linux G25


cat-concatenate

• The cat (short for “concatenate“) command is one of the most


frequently used command in Linux/Unix like operating systems.
• cat command allows us to create single or multiple files, view contain
of file, concatenate files and redirect output in terminal or files.
• Now, we are going to find out handy use of cat commands with their
examples in Linux.

cat: Concatenate files and print to stdout.


• Syntax: cat [OPTION]…[FILE]
• Example: Create file1 with entered
cotent
• $ cat > file1
• Hello
• ^D

Jatin Arora Introduction to Linux G25


cat-concatenate examples
1. How to view contents of a file using cat
$ cat [filename]

2. How to display multiple files using cat


$ cat [filename] [filename] ...

3. How to display contents of file with line numbers


$ cat -n [filename]

4. How to create file using cat command


$ cat > [name-of-new-file]
When run, the command requires you to enter the information on the
terminal. Once you're done, just press CTRL+d to exit.
For example:
$ cat > file4.txt
Jatin Arora Introduction to Linux G25
cat-concatenate examples
5. How to copy the contents of one file to another file
$ cat [filename-whose-contents-is-to-be-copied] > [destination-filename]
For example:
$ cat file1.txt > file3.txt

Please note that the output redirection operator (>) that we've used in this case
flushes the destination file before the content is moved to it. So, it's worth
knowing that there exists another redirection operator (>>) that just appends the
contents to the destination file, instead of flushing it first.

$cat file1 >> file2

6. Cat command can display content in reverse order using tac command.
Command:

$tac filename

Jatin Arora Introduction to Linux G25


cp-copy

• cp stands for copy.


• This command is used to copy files or group of files or directory.
• It creates an exact image of a file on a disk with different file
name.
• cp command require at least two filenames in its arguments.

Syntax:
cp [OPTION] Source Destination Directory
cp [OPTION] Source Directory Destination Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory

First and second syntax is used to copy Source file to Destination file
or Directory.
Third syntax is used to copy multiple Sources(files) to Directory.

Jatin Arora Introduction to Linux G25


cp command options

cp command options
cp command main options:
option description

cp -f force copy by removing the destination file if needed

cp -i interactive - ask before overwrite

cp -R
recursive copy (including hidden files)
OR r

cp -u update - copy when source is newer than dest

Jatin Arora Introduction to Linux


G25
cp command examples
Copy single file main.c to destination directory bak:
$ cp main.c bak

Copy 2 files main.c and def.h to destination absolute path


directory /home/usr/rapid/ :
$ cp main.c def.h /home/usr/rapid/

Copy all C files in current directory to subdirectory bak :


$ cp *.c bak

Copy directory src to absolute path directory /home/usr/rapid/ :


$ cp src /home/usr/rapid/

Copy all files and directories in dev recursively to subdirectory bak:


$ cp -R dev bak
Jatin Arora Introduction to Linux G25
cp command examples
Force file copy:
$ cp -f test.c bak

Interactive prompt before file overwrite:


$ cp -i test.c bak
cp: overwrite 'bak/test.c'? y

Update all files in current directory - copy only newer files to destination
directory bak:
$ cp -u * bak

$ cp file1 dir #Copy file1 to the dir directory

Like us/mnt/testThere is file1 under the directory, copy file1 to/mnt/test2Directory,


execute:cp /mnt/test/file1 /mnt/test2

$ cp -R dir1 dir2 #Copy all files under dir1 to dir2

Jatin Arora Introduction to Linux G25


mv-move

mv command is used to move files and directories.

mv command syntax
$ mv [options] source dest

mv command options
mv command main options:
option description

mv -f force move by overwriting destination file without prompt

mv -i interactive prompt before overwrite

mv -u update - move when source is newer than destination

Jatin Arora Introduction to Linux G25


mv command examples

Move main.c def.h files to /home/usr/rapid/ directory:


$ mv main.c def.h /home/usr/rapid/

Move all C files in current directory to subdirectory bak :


$ mv *.c bak

Move all files in subdirectory bak to current directory :


$ mv bak/* .

Rename file main.c to main.bak:


$ mv main.c main.bak

$mv file1 file2 #Rename the file file1 in the current directory to file2

$mv file1 dir2 #Move the file file1 to the directory dir2

Jatin Arora Introduction to Linux G25


mv command examples
Rename directory bak to bak2:
$ mv bak bak2

Update - move when main.c is newer:


$ mv -u main.c bak
$

Move main.c and prompt before overwrite bak/main.c:


$ mv -v main.c bak
'bak/main.c' -> 'bak/main.c'
$

Rename the file "My file.txt" to "My file 2.txt". Here, the spaces in the file
name are escaped, protecting them from being interpreted as part of the
command.
mv "My file.txt" "My file 2.txt"

Jatin Arora Introduction to Linux G25


'rm’-remove
The 'rm' means remove.
This command is used to remove a file.

rm Options

Option Description

rm *extension Used to delete files having same


extension.

rm -r or R To delete a directory recursively.

rm -i Remove a file interactively.

rm -rf Remove a directory forcefully.

Jatin Arora Introduction to Linux G25


rm command examples
rm myfile.txt
Remove the file myfile.txt. If the file is write-protected, you will be prompted to confirm that
you really want to delete it.
rm -f myfile.txt
Remove the file myfile.txt. You will not be prompted, even if the file is write-protected; if rm
can delete the file, it will.
rm *
Remove all files in the working directory. If it is write-protected, you will be prompted before
rm removes it.
rm -f *
Remove all files in the working directory. rm will not prompt you for any reason before
deleting them.
rm -i *
Attempt to remove every file in the working directory, but prompt before each file to confirm.
rm -I *
Remove every file in the working directory; prompt for confirmation if more than three files
are being deleted.

Jatin Arora Introduction to Linux G25


rm command examples

rm -r mydirectory
Remove the directory mydirectory, and any files and directories it contains. If a file or
directory that rm tries to delete is write-protected, you will be prompted to make sure
that you really want to delete it.
rm -rf mydirectory
Same as the above command, but you will never be prompted; if rm can delete the files,
it will.

For example
$ rm file1 #The current directoryfile1File delete, need to confirm before deleting,
confirm delete inputy

$ rm -rf /mnt/test #Will/mnt/test The directory and the files in the directory are deleted
without confirmation

$ rm -r /mnt/test2 #Will/mnt/test2 To delete directories and files in the directories, you


need to confirm one by one before deleting. In actual use, it is generally not
recommended to use-rThis parameter

Jatin Arora Introduction to Linux G25


ls command options
• The ls is the list command in Linux. It will show the full list or content
of your directory. Just type ls and press the enter key. The whole
content will be shown.
ls option Description

ls -a In Linux, hidden files start with . (dot) symbol and they are not visible in
the regular directory. The (ls -a) command will enlist the whole list of the
current directory including the hidden files.
ls -l It will show the list in a long list format.

ls -lh This command will show you the file sizes in human readable format. Size
of the file is very difficult to read when displayed in terms of byte. The (ls -
lh)command will give you the data in terms of Mb, Gb, Tb, etc.

ls -lhS If you want to display your files in descending order (highest at the top)
according to their size, then you can use (ls -lhS) command.

ls -l - -block-size=[SIZE] It is used to display the files in a specific size format. Here, in [SIZE] you
can assign size according to your requirement.

ls -d */ It is used to display only subdirectories.

ls -g or ls -lG With this you can exclude column of group information and owner.

ls -n It is used to print group ID and owner ID instead of their names.

Jatin Arora Introduction to Linux G25


Linux ls command options

ls --color=[VALUE] This command is used to print list as colored or discolored.

ls -li This command prints the index number if file is in the first column.

ls -p It is used to identify the directory easily by marking the directories with a slash
(/) line sign.

ls -r It is used to print the list in reverse order.

ls -R It will display the content of the sub-directories also.

ls -lX It will group the files with same extensions together in the list.

ls -lt It will sort the list by displaying recently modified filed at top.

ls ~ It gives the contents of home directory.

ls ../ It give the contents of parent directory.

ls --version It checks the version of ls command.

Jatin Arora Introduction to Linux G25


“Touch” Command

In Linux every single file is associated with timestamps, and every file
stores the information of last access time, last modification time and last
change time. So, whenever we create new file, access or modify an
existing file, the timestamps of that file automatically updated.

Touch Command Options


1.-a, change the access time only
2.-c, if the file does not exist, do not create it
3.-d, update the access and modification times
4.-m, change the modification time only
5.-r, use the access and modification times of file
6.-t, creates a file using a specified time

Jatin Arora Introduction to Linux G25


touch command examples
1. How to Create an Empty File
The following touch command creates an empty (zero byte) new file called sheena.
# touch sheena
2. How to Create Multiple Files
touch sheena meena leena
3. How to Change File Access and Modification Time
To change or update the last access and modification times of a file called leena, use the -a option as follows. The
following command sets the current time and date on a file. If the leena file does not exist, it will create the new empty
file with the name.\
# touch -a leena
4. How to Avoid Creating New File
Using -c option with touch command avoids creating new files. For example the following command will not create a
file called leena if it does not exists.
# touch -c leena
5. How to Change File Modification Time
If you like to change the only modification time of a file called leena, then use the -m option with touch command.
Please note it will only updates the last modification times (not the access times) of the file.
# touch -m leena
Jatin Arora Introduction to Linux G25
touch command examples
6. Explicitly Set the Access and Modification times
You can explicitly set the time using -c and -t option with touch command. The format would be as follows.
# touch -c -t YYDDHHMM leena
For example the following command sets the access and modification date and time to a file leena as 17:30(17:30
p.m.) December 10 of the current year (2012).
# touch -c -t 12101730 leena
Next verify the access and modification time of file leena, with ls -l command.
7. How to Use the time stamp of another File
The following touch command with -r option, will update the time-stamp of file meena with the time-stamp
of leena file. So, both the file holds the same time stamp.
# touch -r leena meena
8. Create a File using a specified time
If you would like to create a file with specified time other than the current time, then the format should be.

# touch -t YYMMDDHHMM.SS tecmint


For example the below command touch command with -t option will gives the tecmint file a time stamp
of 18:30:55 p.m. on December 10, 2012.
touch -t 201212101830.55 tecmint

Jatin Arora Introduction to Linux G25


which and whatis command

Which command is very small and simple command to locate executables in the system.

whatis command in Linux is used to get a one-line manual page descriptions. In Linux, each
manual page has some sort of description within it. So this command search for the manual
pages names and show the manual page description of the specified filename or argument.

Jatin Arora Introduction to Linux G25


Linux whereis Command

Whereis command is helpful to locate binary, source and manual pages of


commands in the Linux system. It is very simple utility and provides several
options which are given below with examples.
• Syntax:
• $ whereis [-options]
• For example, whereis command is run without any option.
• $ whereis open
• open: /bin/open /usr/share/man/man1/open.1.gz
/usr/share/man/man2/open.2.gz

Jatin Arora Introduction to Linux G25


Activity
1.Copy the contents of the files students.txt to a new file
studentNames.txt. You may need to create a new file

1.Also given a files newStudents.txt , append the data in the


newStudents.txt file to students.txt file

1.Rename the students.txt file to names.txt

Jatin Arora Introduction to Linux G25


Introduction to Linux (CS-110) Class
On
Directory oriented commands: cd, pwd, mkdir,
rmdir

Dated: Tuesday, July 20, 2021

Department of Computer Science and Engineering,


Chitkara University, Punjab

Ms. A.Sangeetha Introduction to Linux G27


Index

1. cd
2. pwd
3. mkdir
4. rmdir

Ms. A.Sangeetha Introduction to Linux G27 2


Linux Directory Commands

Directory Description
Command
pwd The pwd command stands for (present working directory). It displays the
current working location or directory of the user. It displays the whole
working path starting with /. It is a built-in command.
ls The ls command is used to show the list of a folder. It will list out all the
files in the directed folder.
cd The cd command stands for (change directory). It is used to change to the
directory you want to work from the present directory.
mkdir With mkdir command you can create your own directory.
rmdir The rmdir command is used to remove a directory from your system.

Ms. A.Sangeetha Introduction to Linux G27


Cd Command in Linux (Change
Directory)

The cd (“change directory”) command is used to change the current working


directory in Linux and other Unix-like operating systems.

The syntax for the cd command is as follows:

cd [OPTIONS] directory

Absolute and Relative Path Names


When specifying a directory to change to, you can use either absolute or relative path names.
The absolute or full path starts from the system root /, and the relative path starts from your
current directory.

By default, when you log into your Linux system, your current working directory is set to your
home directory. Assuming that the Downloads directory exists in your home directory, you can
navigate to it by using the relative path to the directory:

Ms. A.Sangeetha Introduction to Linux G27


Cd Command in Linux (Change
Directory)
cd Downloads

You can also navigate to the same directory by using its absolute path:

cd /home/username/Downloads

In short, if the path starts with a slash (/), it is the absolute path to the directory.

The Parent Directory


On Unix-like operating systems, the current working directory is represented by a single dot
(.). Two dots (..), one after the other, represent the parent directory or the directory
immediately above the current one.
If you type cd ., you will change into the current directory or, in other words, the
command will do nothing.

Suppose you are currently in the /usr/local/share directory. To switch to the /usr/local
directory (one level up from the current directory), you would type:

cd ../

To move two levels up to the /usr directory (the parent’s parent), you could run the
following:

Ms. A.Sangeetha Introduction to Linux G27


Cd Command in Linux (Change
Directory)

cd ../../

Here is another example. Let’s say you are in the /usr/local/share directory, and you
want to switch to the /usr/local/src. You can do that by typing:

cd ../src

Navigate to the Previous Directory


To change back to the previous working directory, pass the dash (-) character as an
argument to the cd command:
cd -

Ms. A.Sangeetha Introduction to Linux G27


Cd Command in Linux (Change
Directory)

Navigate to the Home Directory


To navigate to your home directory, simply type cd. Another way to return directly to
your home directory is to use the tilde (~) character, as shown below:

cd ~

For example, if you want to navigate to the Downloads directory, which is inside your
home directory, you would type:

cd ~/Downloads

You can also navigate to another user’s home directory using the following syntax:

cd ~username

Directories with Space in Their Names


If the directory you want to change to has spaces in its name, you should either
surround the path with quotes or use the backslash (\) character to escape the space:

cd 'Dir name with space’

cd Dir\ name\ with\ space

Ms. A.Sangeetha Introduction to Linux G27


pwd - Print Working Directory

pwd
This command displays the present working directory where you are
currently in.

In the following example I am inside yusufshakeel directory which is inside


the home directory.

$ pwd
/home/yusufshakeel

pwd -L: Prints the symbolic path.


pwd -P: Prints the actual path.

Ms. A.Sangeetha Introduction to Linux G27


mkdir – Make directory

mkdir command in Linux allows the user to create directories (also referred to as folders
in some operating systems ). This command can create multiple directories at once as
well as set the permissions for the directories. It is important to note that the user
executing this command must have enough permissions to create a directory in the
parent directory, or he/she may recieve a ‘permission denied’ error.

Syntax:

mkdir [options...] [directories ...]

Option / Syntax Description


mkdir directory_name Creates a directory in the current location
Creates multiple directories in the current
<strong>mkdir {dir1,dir2,dir3,dir4}</strong>
location. Do not use spaces inside {}
<strong>mkdir –p Creates a directory structure with the
directory/path/newdir</strong> missing parent directories (if any)
Creates a directory and sets full read,
mkdir –m777 directory_name
write, execute permissions for all users

Ms. A.Sangeetha Introduction to Linux G27


mkdir – Make directory

mkdir
This command will create a new directory, provided it doesn't exists.

In the following example we are creating a new directory example.

$ mkdir example
mkdir -p
This command will create nested directories.

In the following example we are creating world directory which is inside the hello
directory which is inside the example directory.

$ mkdir -p example/hello/world

mkdir -m a=rwx [directories]


The above syntax specifies that the directories created give access to all
the users to read from, write to and execute the contents of the created
directories. You can use ‘a=r’ to only allow all the users to read from the
directories and so on.

Ms. A.Sangeetha Introduction to Linux G27


rmdir – remove directory

rmdir command is used remove empty directories from the filesystem in Linux. The rmdir command removes
each and every directory specified in the command line only if these directories are empty. So if the specified
directory has some directories or files in it then this cannot be removed by rmdir command.
The syntax of rmdir command is as follows:

$ rmdir [OPTION]... DIRECTORY...


Remove a Directory using rmdir
To remove a single empty directory, type rmdir followed by the directory name or path to the directory as
follows:

$ rmdir ~/Documents/myfiles
This command will remove the “myfiles” directory located in the “~/Documents” path only if it is empty. If the
directory is not empty, the command will fail and displays the “Directory not empty” message.

Remove Multiple Directories using rmdir


To remove multiple empty directories, type rmdir followed by the directory names or path to directories as
follows:

$ rmdir ~/Documents/myfiles images games


This command will remove the “~/Documents/myfiles”, “images”, and “games” directories only if they are
empty.

Ms. A.Sangeetha Introduction to Linux G27


rmdir – remove directory

Remove Directory and its parent directories


The rmdir command also allows removing the directories along with their parent directories.
Let’s say we have the following directory structure:

To remove the “docs” directory along with its parent directories “myfiles” and “files”, use the -
p command as follows:

$ sudo rmdir –v -p files/myfiles/docs/

Ms. A.Sangeetha Introduction to Linux G27


MCQ

3.Which command is used for removing an empty


1. 'mkdir -m 444 any' will: directory?

a) Create a read-write-execute directory


a) mkdir
named any
b) Create a read-write directory named any b) rm
c) Create a write-only directory named any
c) rmdir
d) create a read-only directory named any
d) remove

2. What does the following command do?


$ mkdir dir dir/dir_01/dir_02

a) create dir, dir_01 and dir_02


b) creates dir_02
c) creates dir only
d) throws an error

Ms. A.Sangeetha Introduction to Linux G27


Activity

Perform the following tasks

1.Create a directory "students". In that directory create two directories


namely John and Tom.

1.A directory "marks" is already created. Your task is to delete the


directory, The directory may contains other items. So use appropriate
options

Ms. A.Sangeetha Introduction to Linux G27


Thank You

Ms. A.Sangeetha Introduction to Linux G27


Introduction to Linux (CS-110) Class
On
Mounting: mount, umount. USB, CD/DVD

Dated: 20-07-2021

Department of Computer Science and Engineering,


Chitkara University, Punjab

Ms. A.Sangeetha Introduction to Linux G27


Introduction
1. In Ubuntu desktop, the USB pen drive will be detected and mounted automatically. In
ubuntu server command line terminal, a message will appear to notice you that some
information about the USB pen drive that has been plug in or alternatively you can
use fdisk command to check whether USB has been mounted or not.
Then you can get mount location by using mount command
mount
2. If Step 1 did not work for you, then run demsg command to identify the correct device
name corresponding to the USB pen drive.
demsg
2. a. Create Mount Point
$ mkdir -p /media/usb
2. b. Mount USB pen drive
$ sudo mount /dev/sdb1 /media/usb
.

Ms. A.Sangeetha Introduction to Linux G27


Umount
Unmounting USB pen drive
1. To unmount a USB device simply run the following command
$ sudo umount /dev/sdb1
OR
$ sudo umount /media/usb

NOTE: /media/usb is a mount point.

Formatting USB pen drive


1. First make sure that USB device is not mounted, if it is then unmount it.
2. Format USB device in fat32 format
$ sudo mkfs.vfat /dev/sdb1
Note: You can use mkfs.ext3 to format to ext3 filesystem.
Caution: Enter your device name correctly otherwise it will wip out your entire data.
3. Format and change the Label of the USB drive
$ sudo mkfs.vfat –n /dev/sdb1

Ms. A.Sangeetha Introduction to Linux G27


Mount CD Rom
.
Mount/Unmount CD/DVD
In order to use a CD-ROM under Linux (or any Unix-like operating system), you must
first mount it. Mounting a CD-ROM (or any other physical media, such as a floppy
disk, a ZIP disk or a hard drive) tells the operating system which block device to use
and where that device is to appear within the directory tree — the mount point. Once
you have finished using the CD-ROM, you must unmount it.
Mounting the CD-ROM
The canonical command to mount the CD-ROM under Linux is:
# mount -t iso9660 -o ro device dir

The block device specifies the physical device or the CD-ROM is attached to. On
most Linux systems, you can use /dev/cdrom, which would be a symbolic link to
something like /dev/hdc for an ATAPI CD-ROM, selected as a master device, on the
secondary IDE
# mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom

Note- only the root superuser can run this version of the mount command, so you
need to log in as root to do this. For the safety and security of your own computer,
you should log out as soon as you have entered this command successfully.

Ms. A.Sangeetha Introduction to Linux G27


Unmounting the CD-ROM
Once you have finished using the CD-ROM, you need to unmount it. The operating
system makes you do this so that it can check that nothing is, in fact, using the CD-
ROM when you try to eject it.
To unmount the CD-ROM, you need to enter the following command as the root
superuser:
# umount dir

Ms. A.Sangeetha Introduction to Linux G27


Question

Practice Questions

1. What do you mean by mounting?


2. What do you mean by unmounting?
3. What does the /dev dir is for?
4. What does the –p option in mkdir state?
5. What do you mean by mount point?
6. In which directory will you find the media devices?
7. Give command to mount a CD-ROM.
8. Give command to create a mount point
9. Give command to mount A USB
10. Give command to unmount a USB.

Ms. A.Sangeetha Introduction to Linux G27


Activity
Activity for the students:

Mount a USB and change its name to your name.


Modify the Filesystem to ext4
Check the Filesystem consistency using fsck

Ms. A.Sangeetha Introduction to Linux G27


Thank
You
Ms. A.Sangeetha Introduction to Linux G27
Introduction to Linux (CS-110) Class
On
File system: Comparing Files using diff, cmp, comm

Dated: 22-07-2021

Department of Computer Science and Engineering,


Chitkara University, Punjab

Jatin Arora Introduction to Linux G25


Introduction
diff (short for difference) is a simple and easy to use tool which analyzes two files
and displays the differences in the files by comparing the files line by line. It prints the
lines that are different. Importantly, if you want the two files to be identical to each
other, diff also outputs a set of useful instructions on how to change one file to make it
identical to the second file.

1. To display Manual
# man diff
2. To compare file using diff command
diff file1 file2

Jatin Arora Introduction to Linux G25


Jatin Arora Introduction to Linux G25
Cmp command
.
cmp Command
cmp performs a byte-by-byte comparison of two files like this and helps you to find
out whether the two files are identical or not.
When cmp is used for comparison between two files, it reports the location of the first
mismatch to the screen if difference is found and if no difference is found i.e the files
compared are identical.
cmp displays no message and simply returns the prompt if the the files compared are
identical.

Syntax: cmp [OPTION]... FILE1 [FILE2]

SKIP1 ,SKIP2 & OPTION are optional and


FILE1 & FILE2 refer to the filenames .
OR
$cmp file1.txt file2.txt
If the files are not identical :

outpu :$cmp file1.txt file2.txt file1.txt file2.txt differ: byte 9, line 2 /*indicating that
the first mismatch found in two files at byte 20 in second line*/
Jatin Arora Introduction to Linux G25
Options of cmp

If the files are identical


$cmp file1.txt file2.txt $ _ /*indicating that the files are identical*/

Options for cmp command


1. -b(print-bytes) : If you want cmp displays the differing bytes in the
output when used with -b option.
$cmp -b file1.txt file2.txt
Output: file1.txt file2.txt differ: 12 byte, line 2 is 154 l 151 i /*
indicating that the difference is in 12 byte ,which is 'l' in file1.txt and 'i' in
file2.txt.*/The values 154 and 151 in the above output are the values for
these bytes, respectively.

2. -i [bytes-to-be-skipped] :
$cmp -i 10 file1.txt file2.txt $
Output: indicating that both files are identical after 10 bytes skipped from
both the files.

Jatin Arora Introduction to Linux G25


Option Cont..
• Note that in cases like these (where you use -i to skip bytes), the byte at which the
comparison begins is treated as byte number zero.

3. $cmp -i 10:12 file1.txt file2.txt $


• Output: It indicating that both files are identical after 10 bytes skipped from first file and
12 bytes skipped from second file.

• 4. -l option : This option makes the cmp command print byte position and byte value
for all differing bytes.
• Output $cmp -l file1.txt file2.txt 20 12 56 21 124 12 22 150 124 23 151 150 24 163
151 25 40 163 26 146 40 27 150 151 28 12 24 29 124 145 30 157 163

• /*indicating that files are different displaying the position of differing bytes along with the
differing bytes in both file*/ The first column in the output represents the position (byte
number) of differing bytes. The second column represents the byte value of the differing
byte in the first file, while the third column represents the byte value of the differing byte
in the second file.

Jatin Arora Introduction to Linux G25


Option Cont..
• 5. -s option : This allows you to suppress the output normally produced by cmp
command i.e it compares two files without writing any messages. This gives an
exit value of 0 if the files are identical, a value of 1 if different, or a value of 2 if an
error message occurs.
• $cmp -s file1.txt file.txt 1

• /*indicating files are different without displaying the differing byte and line*/

• 6. -n [number of bytes to be compared] option :This option allows you to limit


the number of bytes you want to compare ,like if there is only need to compare at
most 25 or 50 bytes.
• $cmp -n 50 file1.txt file2.txt $_ /

• *indicating files are identical for starting 50 bytes*/

• 8. – -v option : This gives the output information and exits.


• 9. – -help option : This displays a help message and exits.

Jatin Arora Introduction to Linux G25


Comm Command

• comm compare two sorted files line by line and write to standard output; the
lines that are common and the lines that are unique.

• $comm file1.txt file2.txt


Apaar
Ayush Rajput
Deepak Hemant
Lucky
Pranjal Thakral

Jatin Arora Introduction to Linux G25


Options of comm

• Options for comm command:


• 1. -1 :suppress first column(lines unique to first file).
2. -2 :suppress second column(lines unique to second file).
3. -3 :suppress third column(lines common to both files).
4. – -check-order :check that the input is correctly sorted, even if all input
lines are pairable.

Jatin Arora Introduction to Linux G25


Options Cont..

• 5. – -nocheck-order :do not check that the input is correctly


sorted.
6. – -output-delimiter=STR :separate columns with string STR
7. – -help :display a help message, and exit.
8. – -version :output version information, and exit.

Jatin Arora Introduction to Linux G25


Practice Questions

Questions

What is the function of diff command?


What is the difference between diff and diff3?
What is the difference between comm and cmp command?
Give the option used with diff to ignore case.
Give the option used with diff to ignore spaces.

Jatin Arora Introduction to Linux G25


ACTIVITY

1. Create a file by your section name having name of all the students of
your section,
2. create another file by your subsection name having name of ll the
students in your subsection.
3. Compare the two files using all the comparison commands

Jatin Arora Introduction to Linux G25


Thank
You
Jatin Arora Introduction to Linux G25
Introduction to Linux (CS-110) Class
On
Compressing files: tar, gzip, bzip2, compress, uncompress files.

Dated: 05-07-2021

Department of Computer Science and Engineering,


Chitkara University, Punjab

Ritu Rathee Introduction to Linux G28, G29


Introduction- Tar
The Linux “tar” stands for tape archive, The tar command used to rip a collection of
files and directories into a highly compressed archive file commonly
called tarball or tar, gzip and bzip in Linux.
1. Create tar Archive File
tar -cvf demo.tar
Let’s discuss each option used in the above command to create a tar archive file.
c – Creates a new .tar archive file.
v – Verbosely show the .tar file progress.
f – File name type of the archive file.

2. Create tar.gz Archive File


To create a compressed gzip archive file we use the option as z.
# tar -cvzf demo.tar.gz /home/MyImages
OR
# tar -cvzf demo.tgz /home/MyImages

Ritu Rathee Introduction to Linux G28, G29


Option Of Tar
.
3.Create tar.bz2 Archive File

The bz2 feature compresses and creates an archive file less than the size of
the gzip. The bz2 compression takes more time to compress and decompress files
than gzip, which takes less time. To create a highly compressed tar file we use the
option j.
# tar cvfj demo.tar.bz2
OR
# tar cvfj demo.tar.tbz
OR
# tar cvfj demo.tar.tb2

4. Untar tar Archive File


To untar or extract a tar file, just issue the following command using option x (extract

Untar files in Current Directory


# tar -xvf public_html-14-09-12.tar
Untar files in specified Directory
tar -xvf public_html-14-09-12.tar -C /home/public_html/videos/

Ritu Rathee Introduction To Linux G28 & G29


5. Uncompress tar.gz Archive File
To Uncompress tar.gz archive file, just run the following command. If we would like to
untar in different directories, just use option -C and the directory path, as shown in
the above example.
# tar –xvf demo.tar.gz
6. Uncompress tar.bz2 Archive File
To Uncompress the highly compressed tar.bz2 file, just use the following command.
# tar -xvf videos-14-09-12.tar.bz2
7. List Content of tar Archive File
To list the contents of the tar archive file, just run the following command
with option t (list content).
# tar -tvf demo.tar
8. 10. Untar Single file from tar File
To extract a single file called cleanfiles.sh from cleanfiles.sh.tar use the
following command.
# tar -xvf cleanfiles.sh.tar cleanfiles.sh
OR
# tar --extract --file=cleanfiles.sh.tar cleanfiles.sh

Ritu Rathee Introduction to Linux G28, G29


11. Untar Single file from tar.gz File
# tar -zxvf demo.tar.gz main.xml
OR
# tar --extract --file=demo.tar.gz main.xml

12. Untar Multiple files from tar, tar.gz, and tar.bz2 File

To extract or untar multiple files from the tar, tar.gz, and tar.bz2 archive file. For example,
the below command will extract “file 1” “file 2” from the archive files.

# tar -xvf main.tar "file1" "file2"


# tar -zxvf Main.tar.gz "file1" "file2"
# tar -jxvf main.tar.bz2 "file1" "file2”

14. Extract Group of Files using Wildcard


To extract a group of files we use wildcard-based extracting. For example, to extract a
group of all files whose pattern begins with .php from a tar, tar.gz, and tar.bz2 archive file.

# tar -xvf Phpfiles-org.tar --wildcards '*.php’


# tar -zxvf Phpfiles-org.tar.gz --wildcards '*.php’
# tar -jxvf Phpfiles-org.tar.bz2 --wildcards '*.php'

Ritu Rathee Introduction To Linux G28 & G29


Gzip command
gzip command compresses files. Each single file is compressed into a single file. The
compressed file consists of a GNU zip header and deflated data.
If given a file as an argument, gzip compresses the file, adds a “.gz” suffix, and deletes the
original file. With no arguments, gzip compresses the standard input and writes the
compressed file to standard output.
Syntax :

gzip [Options] [filenames]

Example:

$ gzip mydoc.txt
This command will create a compressed file of mydoc.txt named as mydoc.txt.gz and
delete the original file.

Ritu Rathee Introduction To Linux G28 & G29


Gzip Cont..

-f option : Sometimes a file cannot be compressed. Perhaps you are trying to compress a
file called “myfile1” but there is already a file called “myfile1.gz”. In this instance, the “gzip”
command won’t ordinarily work.
To force the “gzip” command to do its stuff simply use -f option: $ gzip -f myfile1.txt
This will forcefully compress a file named myfile.txt even if there already exists a file named
as myfile.txt.gz

$ gzip -9 mydoc.txt
To get minimum compression at the fastest speed
-v option: This option displays the name and percentage reduction for each file
compressed or decompressed.
$ gzip -v mydoc.txt
OUTPUT :
new.txt: 18.2% -- replaced with new.txt.gz
-d option :This option allows to decompress a file using the “gzip” command.
$ gzip -d mydoc.txt.gz

Ritu Rathee Introduction To Linux G28 & G29


Gzip Cont..
-k option :By default when you compress a file using the “gzip” command you end up with
a new file with the extension “.gz”.If you want to compress the file and keep the original file
you have to run the gzip command with -k option:
$ gzip -k mydoc.txt
The above command would end up with a file called “mydoc.txt.gz” and “mydoc.txt”.
-L option : This option displays the gzip license.
$ gzip -L filename.gz
OUTPUT :
Apple gzip 264.50.1 (based on FreeBSD gzip 20111009) Copyright (c) 1997, 1998, 2003,
2004, 2006 Matthew R. Green All rights reserved.
-r option : This option can compress every file in a folder and its subfolders.This option
doesn’t create one file called foldername.gz. Instead, it traverses the directory structure
and compresses each file in that folder structure.
gzip -r testfolder
This will compress all the files present in the testfolder.
-[1-9] option : It allows to change the compression level.A file can be compressed in
different ways. For instance, you can go for a smaller compression which will work faster or
you can go for maximum compression which has the tradeoff of taking longer to run.The
speed and compression level can vary by levels using numbers between 1 and 9.
$ gzip -1 mydoc.txt
This will get maximum compression at the slowest speed
bzip2 command
bzip2 command in Linux is used to compress and decompress the files i.e. it
helps in binding the files into a single file which takes less storage space as the
original file use to take. It has a slower decompression time and higher memory
use.
Syntax:
bzip2 [OPTIONS] filenames ...Options:
1. -z : This option forces compression. It is an opposite command of
decompression i.e. -d Option.$ bzip2 -z input.txt
2. -k: This option does compression but does not deletes the original file.
$ bzip2 -k input.txt
3. -d : This option is used for decompression of compressed files.
$ bzip2 -d input.txt.bz2

Ritu Rathee Introduction To Linux G28 & G29


bzip2 Cont..
4. -t : This option does the integrity check of the file and does not decompresses
the file. It gives us the idea that the file is corrupt or not.
$ bzip2 -t input.txt.bz2
5. -v : Verbose mode show the compression ratio for each file processed. It also
increases the verbosity level, spewing out lots of information which is primarily of
interest for diagnostic purposes.
$ bzip2 -v input.txt

-h –help : To display the help message and exit.


-L –license -V –version : It is used to display the software version, license terms, and
conditions.
-q –quiet : It will suppress non-essential warning messages. Messages pertaining to I/O
errors and other critical events will not be suppressed.
-f –force : It will force overwrite of output files.

Ritu Rathee Introduction To Linux G28 & G29


Practice Questions

QUESTIONS
1. What is the zcat command used for?
2. What do you mean by archiving?
3. Give commands for uncompressing a file?
4. What is the purpose for gzip command?
5. What are .tar files?
6. What are .Z files?
7. What extensions are given to the compressed files?
8. Why do you need to compress the files?

Ritu Rathee Introduction To Linux G28 & G29


ACTIVITY
Use the tar command with the -czf options to create an archive of the /etc directory
using gzip compression. Save the archive file as /tmp/etc.tar.gz.
[root@servera ~]# tar -czf /tmp/etc.tar.gz /etc tar: Removing leading `/' from member names
Use the tar command with the -tzf options to verify that the etc.tar.gz archive contains the files from
the /etc directory.
[root@servera ~]# tar -tzf /tmp/etc.tar.gz
etc/
etc/mtab
etc/resolv.conf ...output omitted...
Create a directory named /backuptest. Verify that the etc.tar.gz backup file is a valid archive by
decompressing the file to the /backuptest directory. Create the /backuptest directory.
[root@servera ~]# mkdir /backuptest
Change to the /backuptest directory.
[root@servera ~]# cd /backuptest [root@servera backuptest]#
List the contents of the etc.tar.gz archive before extracting.
[root@servera backuptest]# tar -tzf /tmp/etc.tar.gz etc/ etc/mtab etc/fstab etc/crypttab etc/resolv.conf
...output omitted...
Extract the /tmp/etc.tar.gz archive to the /backuptest directory.
[root@servera backuptest]# tar -xzf /tmp/etc.tar.gz [root@servera backuptest]#
List the content of the /backuptest directory. Verify that the directory contains the files from
the /etc directory.
[root@servera backuptest]# ls -l

Ritu Rathee Introduction To Linux G28 & G29


Thank
You
Ritu Rathee Introduction to Linux G28, G29
Introduction to Linux (CS-110) Class
On

Editors in Linux

Dr. Jatin Arora


Department of Computer Science and Engineering
Chitkara University, Punjab
Dr. Jatin Arora ITL CS110 - G25 1
Why Editor
• An editor is just like a word processor without lot of features.
• All Os come with a basic text editor.
• Main use – writing something in plain text with no formatting so
another program can read it.
• Most popular text editor for Linux is called ‘Vi’. It is a program
that comes from UNIX.
• There is more recent version called ‘VIM’ which means
‘Vi Mproved’.
• Problem with Vi/Vim – lot of people don’t like it, because of you
have to remember a lot of key combinations to do stuff that other
editors will do for you more easily.

Dr. Jatin Arora ITL CS110 - G25 2


Types of Editor
• Mainly 3 types of editors.
1. console based editors
2. GUI based editors
3. Structure text editors
1. console based editors: it contains various editors like emacs, jed,
nano, pico, vim.
2. GUI based editors: gedit, gvim, Nedit, Tea, sublime.
3. Structure text editors: It refers to logically formatted and/or
annotated text to represent a data schema or programmatic
function. Two common formats available today are HTML for
web markup of text documents and XML for data representation.
HTML editors and XML editors

Dr. Jatin Arora ITL CS110 - G25 3


Agenda

• Gedit
• Vi
• Vim

Dr. Jatin Arora ITL CS110 - G25 4


Gedit Editor
• Gedit editor is the default editor for the GNOME desktop
environment. When we open a file, it will open with the Gedit
editor. It provides straightforward functionalities like any basic
text editor. It is a lightweight editor with a straight forward user
interface. It was publicly released in the year 2000 with a
GNOME desktop environment. It is developed using the C
programming language and supports all font family.
• It provides syntax highlighting.
• It supports internationalized text.
• It supports several programming languages.

Dr. Jatin Arora ITL CS110 - G25 5


• To invoke gedit editor from terminal execute the following command from
terminal gedit Filename

Dr. Jatin Arora ITL CS110 - G25 6


Explore gedit

Dr. Jatin Arora ITL CS110 - G25 7


Multiple file in gedit

Dr. Jatin Arora ITL CS110 - G25 8


Vi Editor
• The visual editor on the Linux.
• The vi editor comes with every version of Linux or Unix.
• Using vi is similar to using other editors in that you can see
your file on the screen ..
• The vi editor is the most popular editor in linux. The current
version is really "vim", but to invoke it simply type "vi".
• Before vi the primary editor used on Unix was the line editor
– User was able to see/edit only one line of the text at a time
• The vi editor is not a text formatter (like MS Word, Word
Perfect, etc.)
– you cannot set margins
– center headings

Dr. Jatin Arora ITL CS110 - G25 9


Features of Vi
• The vi editor is:
A very powerful Editor.
It is required by all Linux Administrators to learn,
specially for Handling Linux Server.
It support navigation from point to point in the file,
and make changes.
Available on all UNIX/Linux systems.

Dr. Jatin Arora ITL CS110 - G25 10


Opening a file in VI

• Type vi filename at the shell prompt


• After pressing enter the command prompt disappears and
you see tilde(~) characters on all the lines
• These tilde characters indicate that the line is blank
• Vi –R filename : It will open the file in readonly mode
• View filename : It will open the file in command mode

Dr. Jatin Arora ITL CS110 - G25 11


vimtutor
Typing vimtutor launches a short but
very comprehensive tutorial for those
who want to learn their first vi
commands. This tutorial is a good
place to start learning vi.
Even though it provides only an
introduction and just seven lessons, it
has enough material to make you a
very proficient vi user, because it
covers a large number of commands.
After learning these basic ones, you
can look up new tricks to incorporate
into your list of vi commands because
there are always more optimal ways to
do things in vi with less typing.
Dr. Jatin Arora ITL CS110 - G25 12
Mode in vi
Mode Feature

Command • By default, vi starts in Command mode.


• Each key is an editor command.
• Keyboard strokes are interpreted as commands that can modify file
contents.
Insert • Type i to switch to Insert mode from Command mode.
• Insert mode is used to enter (insert) text into a file.
• Insert mode is indicated by an “? INSERT ?” indicator at the bottom of
the screen.
• Press Esc to exit Insert mode and return to Command mode.

Line • Type : to switch to the Line mode from Command mode. Each key is
an external command, including operations such as writing the file
contents to disk or exiting.
• Uses line editing commands inherited from older line editors. Most of
these commands are actually no longer used. Some line editing
commands are very powerful.
• Press Esc to exit Line mode and return to Command mode.
Dr. Jatin Arora ITL CS110 - G25 13
Command Mode
The table describes the most important commands used to start, exit, read, and
write files in vi. The ENTER key needs to be pressed after all of these commands

Command Usage
vi myfile Start the vi editor and edit the myfile file
vi -r myfile Start vi and edit myfile in recovery mode from a system
crash
:r file2 Read in file2 and insert at current position
:w Write to the file
:w myfile Write out the file to myfile
:w! file2 Overwrite file2
:x or :wq Exit vi and write out modified file
:q Quit vi
:q! Quit vi even though modifications have not been saved

Dr. Jatin Arora ITL CS110 - G25 14


Navigation in Vi
• Moving Cursor Position
• You can move around only when you are in the command
mode
• Arrow keys usually works(but may not)
• The standard keys for moving cursor are:
h - for left
l - for right
j - for down
k - for up
w - to move one word forward
b - to move one word backward

Dr. Jatin Arora ITL CS110 - G25 15


• Control-d scrolls the screen down (half screen)
• Control-u scrolls the screen up (half screen)
• Control-f scrolls the screen forward (full
screen)
• Control-b scrolls the screen backward (full
screen).

Dr. Jatin Arora ITL CS110 - G25 16


All Navigation Options
Key Usage
arrow keys To move up, down, left and right
j or <ret> To move one line down
k To move one line up
h or Backspace To move one character left
l or Space To move one character right
0 To move to beginning of line
$ To move to end of line
w To move to beginning of next word
:0 or 1G To move to beginning of file
:n or nG To move to line n
:$ or G To move to last line in file
CTRL-F or Page Down To move forward one page
CTRL-B or Page Up To move backward one page
^l To refresh and center screen

Dr. Jatin Arora ITL CS110 - G25 17


Working with Text in vi (Insert Mode)

Key Usage

a Append text after cursor; stop upon Escape key

A Append text at end of current line; stop upon Escape key

i Insert text before cursor; stop upon Escape key

I Insert text at beginning of current line; stop upon Escape key

o Start a new line below current line, insert text there; stop upon Escape key

O Start a new line above current line, insert text there; stop upon Escape key

r Replace character at current position

R Replace text starting with current position; stop upon Escape key

Dr. Jatin Arora ITL CS110 - G25 18


Delete in Vi
• x - deletes the current character
• d - is the delete command but pressing only d will not delete
anything you need to press a second key
– dw - deletes to end of word
– dd - deletes the current line
– d0 - deletes to beginning of line
• There are many more keys to be used with delete command
• The vi commands can be used followed by a number such as
n<command key(s)>
– For example dd deletes a line 5dd will delete five lines.

Dr. Jatin Arora ITL CS110 - G25 19


Delete Options
x Delete character at current position

Nx Delete N characters, starting at current


position
dw Delete the word at the current position

D Delete the rest of the current line

dd Delete the current line


Ndd or dNd Delete N lines
u Undo the previous operation
yy Yank (copy) the current line and put it in
buffer
Nyy or yNy Yank (copy) N lines and put it in buffer

pDr. Jatin Arora Paste


ITL CS110 - G25at the current position the yanked 20
Yank and paste
• yy - (yank) copy current line to buffer
• nyy - Where n is number of lines
• p - Paste the yanked lines from buffer to the
line below
• P - Paste the yanked lines from buffer to the
line above

Dr. Jatin Arora ITL CS110 - G25 21


Searching for Text in vi
The table describes the most important commands used when searching for text in vi.
The ENTER key should be pressed after typing the search pattern.

Command Usage
/pattern Search forward for pattern
?pattern Search backward for pattern

The table describes the most important keystrokes used when searching for text in vi.

Key Usage
n Move to next occurrence of search pattern
N Move to previous occurrence of search
pattern

Dr. Jatin Arora ITL CS110 - G25 22


Exit from vi

• :q <enter> is to exit, if you have not made any


changes to the file
• :q! <enter> is the forced quit, it will discard the
changes and quit
• :wq <enter> is for save and Exit
• :x <enter> is same as above command
• ZZ is for save and Exit (Note this command is
uppercase)
• The ! Character forces over writes, etc. :wq!

Dr. Jatin Arora ITL CS110 - G25 23


Using External Commands in Vi
• Typing : sh command opens an
external command shell. When
you exit the shell, you will resume
your vi editing session.

• Typing :!executes a command


from within vi. The command
follows the exclamation point.
This technique is best suited for
non-interactive commands, such
as : ! wc %. Typing this will run
the wc (word count) command on
the file; the
character % represents the file
currently being edited.

Dr. Jatin Arora ITL CS110 - G25 24


Vim
• Vim is an advanced and highly configurable text editor built to
enable efficient text editing. Vim text editor is developed by Bram
Moolenaar. It supports most file types and vim editor is also known
as a programmer’s editor. We can use with its plugin based on our
needs.
• If we try to open a file in vim then type a command vim filename
then you will have the following output

Dr. Jatin Arora ITL CS110 - G25 25


• To install vim on Debian based Linux like ubuntu run the command:
sudo apt install vim
• To create a file in vim type command vim filename
• To write data in a file we need to go in insert mode. To go into the
insert mode we have to type i. You will see the insert mode at the
bottom of the file

Dr. Jatin Arora ITL CS110 - G25 26


• We have written the data into a file now the task is to save and close
the file to do that first exit from insert mode by pressing the Esc key.
To write a command first type semicolon ( : ) and then type the
command wq! And then hit ENTER.
• Vim also comes with its own tutorial. You can see this tutorial by
command vimtutor into the terminal .

Dr. Jatin Arora ITL CS110 - G25 27


Command Mode
• Command mode
– Chosen automatically when starting vim
• Advanced navigational commands in vim

Dr. Jatin Arora ITL CS110 - G25 28


Command mode

Dr. Jatin Arora ITL CS110 - G25 29


Insert Mode
• Must be in insert mode to type text
• Change to insert mode with one of the commands

• vim editor displays -- INSERT -- at the lower left to indicate insert


mode
• Press the Esc key to change back to command mode

Dr. Jatin Arora ITL CS110 - G25 30


Text Modification in Vim
• Modify text using the keyboard instead of the mouse
• Commands for modifying text in vim in command mode

Dr. Jatin Arora ITL CS110 - G25 31


Modifying Text in Insert mode
• Modify text using the keyboard instead of the mouse
• Commands for modifying text in vim in insert mode

Dr. Jatin Arora ITL CS110 - G25 32


Cut, Yank, and Paste
• Cut text : Store text in a buffer
• Paste text: Move from buffer to cursor location
• Yank (copy) text: Keep in current location and copy to buffer

Dr. Jatin Arora ITL CS110 - G25 33


THANK YOU

Dr. Jatin Arora ITL CS110 - G25 34


Introduction to Linux (CS-110) Class
On
Searching File and Directory by using Find
and Locate and Wildcards in Linux

Department of Computer Science and Engineering,


Chitkara University, Punjab

Jatin Arora Introduction to Linux G25


Content

1. Wildcards (Globbing)
2. Find Command
3. Locate Command
4. Q/A
5. Activity based on Searching.

Jatin Arora Introduction to Linux G25


Wild card Characters
• Wildcards (also refered to as meta characters) are symbols
or special characters that represent other characters. You can
use them with any command such as ls command or rm
command to list or remove files matching a given criteria,
receptively.

• These wildcards are interpreted by the shell and the results


are returned to the command you run. There are three main
wildcards in Linux

Jatin Arora Introduction to Linux G25


Command-line Expansions

The Bash shell has multiple ways of expanding a command line


including pattern matching, home directory expansion, string expansion, and
variable substitution. Perhaps the most powerful of these is the path name-
matching capability, historically called globbing. The Bash globbing feature,
sometimes called “wildcards”, makes managing large numbers of files easier.
Using metacharacters that “expand” to match file and path names being
sought, commands perform on a focused set of files at once.

Pattern Matching
Globbing is a shell command-parsing operation that expands a wildcard
pattern into a list of matching path names. Command-line metacharacters are
replaced by the match list prior to command execution. Patterns that do not
return matches display the original pattern request as literal text.

Jatin Arora Introduction to Linux G25


Common metacharacters and pattern classes

Pattern Matches
* Any string of zero or more characters.
? Any single character.
[abc...] Any one character in the enclosed class (between the square brackets).
[!abc...] Any one character not in the enclosed class.
[^abc...] Any one character not in the enclosed class.
[[:alpha:]] Any alphabetic character.
[[:lower:]] Any lowercase character.
[[:upper:]] Any uppercase character.
[[:alnum:]] Any alphabetic character or digit.
[[:punct:]] Any printable character not a space or alphanumeric.
[[:digit:]] Any single digit from 0 to 9.
[[:space:]] Any single white space character. This may include tabs, newlines,
carriage returns, form feeds, or spaces.

Jatin Arora Introduction to Linux G25


Example of Pattern Class
[user@host ~]$ mkdir glob; cd glob
[user@host glob]$ touch alfa bravo charlie delta echo able baker cast dog easy
[user@host glob]$ ls able alfa baker bravo cast charlie delta dog easy echo

[user@host glob]$ ls a* able alfa


[user@host glob]$ ls *a* able alfa baker bravo cast charlie delta easy
[user@host glob]$ ls [ac]* able alfa cast charlie
[user@host glob]$ ls ???? able alfa cast easy echo
[user@host glob]$ ls ????? baker bravo delta

Jatin Arora Introduction to Linux G25


Examples of Wildcards
• To list all the files present in current directory
ls *
• To list all the files with any type of extension
ls *.*
• To list all the file start with a and can be of anu number of
character
ls a*
• To list all the file start with a and end on t
ls a*t
• To list all file of two character
ls ??
• List all the files where file name start with a or b or c andfollow
any number of character
Jatin Arora
ls [abc]*Introduction to Linux G25
Tilde Expansion
The tilde character (~), matches the current user's home directory. If it starts a
string of characters other than a slash (/), the shell will interpret the string up
to that slash as a user name, if one matches, and replace the string with the
absolute path to that user's home directory. If no user name matches, then an
actual tilde followed by the string of characters will be used instead.

In the following example the echo command is used to display the value of
the tilde character.
The echo command can also be used to display the values of brace and
variable expansion characters, and others.

[user@host glob]$ echo ~root /root


[user@host glob]$ echo ~user /home/user
[user@host glob]$ echo ~/glob /home/user/glob

Jatin Arora Introduction to Linux G25


Brace Expansion
Brace expansion is used to generate discretionary strings of characters. Braces
contain a comma-separated list of strings, or a sequence expression. The result
includes the text preceding or following the brace definition. Brace expansions may
be nested, one inside another.
Also double-dot syntax (..) expands to a sequence such that {m..p} will expand to m
n o p.

[user@host glob]$ echo {Sunday,Monday,Tuesday,Wednesday}.log


Sunday.log Monday.log Tuesday.log Wednesday.log
[user@host glob]$ echo file{1..3}.txt
file1.txt file2.txt file3.txt
[user@host glob]$ echo file{a..c}.txt
filea.txt fileb.txt filec.txt
[user@host glob]$ echo file{a,b}{1,2}.txt
filea1.txt filea2.txt fileb1.txt fileb2.txt
[user@host glob]$ echo file{a{1,2},b,c}.txt
filea1.txt filea2.txt fileb.txt filec.txt

Jatin Arora Introduction to Linux G25


Variable Expansion
A variable acts like a named container that can store a value in memory. Variables
make it easy to access and modify the stored data either from the command line or
within a shell script.

You can assign data as a value to a variable using the following syntax:

[user@host ~]$ VARIABLENAME=value

[user@host ~]$ USERNAME=operator


[user@host ~]$ echo $USERNAME
operator

Jatin Arora Introduction to Linux G25


Protecting Arguments from
Expansion

Many characters have special meaning in the Bash shell. To keep the shell from
performing shell expansions on parts of your command line, you can quote and
escape characters and strings.

The backslash (\) is an escape character in the Bash shell. It will protect the
character immediately following it from expansion.

[user@host glob]$ echo The value of $HOME is your home directory.


The value of /home/user is your home directory.

[user@host glob]$ echo The value of \$HOME is your home directory.


The value of $HOME is your home directory.

Jatin Arora Introduction to Linux G25


Find Command

• It is used to Locate/Search files on a Unix/Linux System. It will search


any set of directory, you specify for files that match the given criteria. We
can search file by name, owner, permission, group, type, etc. The search
is recursive i.e it will search all subdirectory also.
• Syntax of find command is
• find <path> <Search Crietria> <action>
• To find all file in current directory & all sub directory
• find or find .

• To find abc file in current Directory

• find . –name abc.txt


• To find
Jatinall .txt files in Directory
Arora abc
Introduction to Linux G25
Find Cont ..
• To find a file
• find . –type f –name abc.txt
• To find a Directory
• find . –type d –name abc.d
• To find file with name and ignoring case
• find . –type f –iname abc.txt
• To find file based on permission
• find . –type f –perm 0777 –print
• To find file without permission
• find . –type f !–perm 0777 –print

Jatin Arora Introduction to Linux G25


Find..
• To find all executable files
• find . –perm /a=x
• To find all Read only files
• find . –perm /a=r
• To find file with permission 777 and change it
• to 755
• find . –type f –perm 0777 –exec chmod 755 {} \;
• To find a single file and remove it
• find . –type f –name “abc.txt” –exec rm {} \;

Jatin Arora Introduction to Linux G25


Find ..

• To find a directory and remove it


• find . –type d –name “dir” –exec rm {} \;
• To find files modified n days back
• find . –mtimes -10
• To find files modified in last 1 minute
• find / –mmin 1
• To find file with size
• find home/jatin –size 1M

Jatin Arora Introduction to Linux G25


Activity based on Find
1. Move to the Desktop and create a folder set.

2. Make 100 directory in set folder.

3. Create 100 file in each directory.

4. Create a file hello.txt in any of the directory(out of 100) randomly.

5. Search out the hello.txt file by using find command.

6. Move the hello.txt file to the Desktop by using find command. Remove

all the files in directory set by using find command.

Jatin Arora Introduction to Linux G25


Find: Always Remember
1. Find always search in the file system

2. Find provide more accurate result

3. Find is slow

4. Find comprises more options

5. There is a way to use search directly for

6. some other command using exec with find

7. We can reduce the search depth by using find

8. In find we can search by using name, depth, owner, permission user,


group and many more parameters

Jatin Arora Introduction to Linux G25


Locate
• While find is no doubt one of the most popular as well as powerful command-line
utilities for file searching in Linux, it not fast enough for situations where-in you need
instantaneous results. If you want to search a file on your system through the command
line, and speed is the topmost priority, then there's another command that you can
use Locate

• The reason locate is so fast is because it doesn't read the file system for the searched
file or directory name. It actually refers to a database (prepared by the
command updatedb) to find what user is looking for and based on that search,
produces its output.

• While this is a good approach, it has its share of drawbacks. The main issue is that after
every new file or directory is created on the system, you need to update the tool's
database for it to work correctly. Otherwise, the command will not be able to find
files/directories that are created after the last database update.
Jatin Arora Introduction to Linux G25
Locate ..
• Syntax of Locate Command is

• locate “filename”

• If locate command is not working then use

• sudo apt-get install mlocate

• The command locate filename (no asterisks) will also do as

• locate implicitly replaces the name you pass (say filename)

• with *filename* or “filename”.

• Command locate filename, locate “filename” and locate

• *filename* will produce the same output

• If you create a file touch ac.txt and then use locate ac.txt

• then it will not work. First we have to use sudo updatedb and

• then locate ac.txt will work

Jatin Arora Introduction to Linux G25


Locate ..
There are various options comprises with locate
command
• If we want to print the count of matched file then use
• option –c
locate –c “*sample.txt*” [Print total no of sample file]

locate –c “*.txt*” [Print total no of txt file]

• if a file is removed from the system, then until you update


• the locate database again, the command will keep showing
• that filename in output. For this specific case, however, you
• can skip updating the database, and still have correct results
• in output using the -e command line option.
locate –e sample.txt
Jatin Arora Introduction to Linux G25
Locate ..

• By default, the search operation the locate command


• performs is case sensitive. But you can force the tool
• to ignore case distinctions using the -i command line
• option.

locate –i “*sample*” [Print both sample and SAMPLE]

• By default, the output entries that the locate command


• produces are separated using newline (\n) character.
Jatin Arora Introduction to Linux G25
• But if you want, you can change the separator, and
Locate….
• In case you want to know which database locate is using, as
• well as other statistics about the database, use the -
• S command-line option.
locate –S
• By default, when you search for a filename using locate, then
• the name you pass let filename is implicitly replaced by
• *filename*. So all names matching *filename* are produced
• in the output. But what if the requirement is to search files
• with names exactly matching ‘filename'? Well, in this case,
• you'll have to use regular expressions, which can be
• enabled using the -r command-line option
locate -r /filename$

Jatin Arora Introduction to Linux G25


Locate….
• If you want to limit the number of search results
• -n<number> will do the trick. However,remember that
• you need to put the option at the end of the command
• line.
locate sample.txt –n 2 [Print only 2 file with
name sample]
• We can also use wildcards with locate
locate *.txt [Will print all txt file]
locate –n 5 *.txt [Will print only 5 txt file]

Jatin Arora Introduction to Linux G25


Locate: Always Remember
1. Locate search in the database

2. Results may not be accurate

3. Locate is very fast as compared to find

4. We can not reduce the search depth

5. We can use locate with name and permission

6. Always remember to use sudo updatedb after creating a file or folder

7. Always remember to use sudo updatedb after deleting a file or folder

Jatin Arora Introduction to Linux G25


Practice Time
•List all the files those can start with any number of character but type is txt

•List out all the files whose name start from a to z and file name can be of

any number of characters

•List all the files where file name does not start with a or b or c and follow any

number of characters

•List all the files those start with lowercase letter and follow any number of

characters

• List all the files those start with any digit and follow any number of characters

•List all the files those first letter is uppercase alphabet, second letter is is a

digit and third letter is a lowercase symbol

•List all the files those start with special symbol.

•List all the file with extension .java or .py

•Copy all the file those start with digit to a directory

Jatin Arora Introduction to Linux G25


Thank You

Jatin Arora Introduction to Linux G25


Introduction to Linux (CS-110) Class
On

User Management in Linux

Ms. Ritu Rathee

Department of Computer Science and Engineering


Chitkara University, Punjab
Ms. Ritu Rathee
ITL CS110 1
Manage Users and Group

• Linux user accounts


• Linux groups

Ms. Ritu Rathee


ITL CS110 2
Linux User

There are Three basic types of Linux user


accounts:
• Super User or Root User
• System User
• Normal User

Ms. Ritu Rathee


ITL CS110 3
Super User

The Linux administrative root account is


automatically created when you install Linux,
The root account is also known as super user’s
account. A special kind of user account which
holds all kinds of permissions to do any
alteration in programs or in service of Linux.
Especially this kind of user account is used for
system administration. Take care while logged
in as Root.
Ms. Ritu Rathee
ITL CS110 4
System User

It is created by default by the OS. Services


such as Apache, Squid, mail, ftp, and printing
have their own individual service accounts.
These accounts exist to allow each of these
services to interact with your computer.
System users have no home directory and no
login capability . They have UID below then
500.

Ms. Ritu Rathee


ITL CS110 5
6
Normal User

• These are the users which have been created by the Root
and has limited access to the resources and need
permission from Root to access any secure resources and
services
• Normal users have the necessary privileges to perform
standard tasks on a Linux computer such as running word
processors, databases, and Web browsers.
• They can store files in their own home directories. Since
regular users do not normally have administrative
privileges, they cannot accidentally delete critical
operating system configuration files.

ITL CS110 Ms. Ritu Rathee 6


Users in Linux

• Each user on a Linux system is assigned a unique


user identification number, also known as a UID.
• Root User got the UID ‘0’ . As root user is prime and
supreme authoritative, so he has the UID ‘0’
• UID for System User lies with in the range of 1 to
499.
• UID for Normal User lies with in the range of > 500
<6000.

Ms. Ritu Rathee


ITL CS110 7
Users in Linux….
• To create password for root we can use the
command
sudo passwd root
• After this command we can use all the
privillage of root by typing
su or su –
• If we use su then your current directory will
remain same
• If we use su – then the current directory is root
Ms. Ritu Rathee
ITL CS110 8
Linux User Accounts

 How Linux user accounts work


 Where Linux user accounts are stored
 Creating and managing user accounts from the
command line

Ms. Ritu Rathee


ITL CS110 9
How Linux User Accounts Work

• Username
• Password
• By default, all user home directories are
created and maintained in the /home
directory.
• However, the root user’s home directory is
/root

Ms. Ritu Rathee


ITL CS110 10
• To view information about the user_name account on my
Linux system, you would enter
finger user_name
• The following information about the user account:
 Login This is the username that is used to authenticate to the
system.
 Name This is the user’s full name.
 Directory This is the user’s home directory.

 Shell This is the default shell that will be provided to the user.

 Last Login This displays the last time the user logged in and
where from

Ms. Ritu Rathee


ITL CS110 11
• In addition to having a home directory and default shell assigned,
each user account is also assigned a unique user ID (UID) number
when they are created. No two user accounts on the system will have
the same UID. To view the UID for a given user account, you can
use the id username command from the shell prompt.
• On a SUSE Linux system, the first regular user account created on
the system is always assigned a UID of 1000. The next user account
will be assigned a UID of 1001...
• Other distributions may use a different numbering scheme for the
UID, however. For example, UIDs on a Fedora system start at 500
instead of 1000.
• The root user account is always assigned a UID of 0 on most Linux
distributions

Ms. Ritu Rathee


ITL CS110 12
Where Linux User Accounts Are
Stored

• /etc/passwd This file contains the user account


information for your system.
• /etc/shadow This file contains passwords for
your user accounts.
• /etc/group This file contains your system’s
groups.

Ms. Ritu Rathee


ITL CS110 13
The /etc/passwd File
Username:Password:UID:GID:Full_Name:Home_Directory:Def
ault_Shell
• Username: The Username field simply identifies the username the user
will supply when logging in to the system
• Password: This is a legacy field. At one time, the user’s password was
stored in encrypted form in this field in the passwd file. However, for
security reasons, the password has been moved from /etc/passwd to
/etc/shadow
• UID This is the user ID for the user account
• GID This field references the group ID number of the user
• Full_Name This field contains the user’s full name
• Home_Directory This field contains the path to the user home directory.
• Default_Shell This field specifies the shell that will be used by default

Ms. Ritu Rathee


ITL CS110 14
The /etc/shadow File

Username:Password:Last_Modified:Min_Days:Max_Days:D
ays_Warn:Disabled_Days:Expire
• Username This is the user’s login name from /etc/passwd.
• Password This is the user’s password in encrypted format
• Last_Modified This field displays the number of days since January
1, 1970 that the password was last changed
• Min_Days This field displays the minimum number of days required
before a password can be changed.
• Max_Days This field displays the maximum number of days before
a password must be changed.
• Days_Warn This field displays the number of days prior to password
expiration that the user will be warned of the pending expiration..

Ms. Ritu Rathee


ITL CS110 15
Creating and Managing User Accounts
from the Command Line

• Using useradd
• Using passwd
• Using usermod
• Using userdel

Ms. Ritu Rathee


ITL CS110 16
Using Useradd
Syntax: useradd options username
useradd ram or sudo useradd ram
ram account is created using the default parameters
contained in the following configuration files:
/etc/default/useradd
/etc/login.defs is the file contains values that can be used
for the GID and UID parameters when creating an account
with useradd. It also contains defaults for creating
passwords in /etc/shadow. To see the details about user ram
we can use the command cat /etc/passwd
•Practices to add user and check the details of the user
Ms. Ritu Rathee
ITL CS110 17
Using Password

• The passwd utility is used to change an existing user’s password


• You can find out this information using the –S option with
passwd. For example, we could enter
passwd –S ram at the shell prompt.
Locks the user’s account. This option invalidates the user’s
password.
–u Unlocks a user’s account.
–d Removes a user’s password.
–n Sets the minimum number of days required before a password
can be changed.
–x Sets the maximum number of days before a password must be
changed.

Ms. Ritu Rathee


ITL CS110 18
Using usermod

• From time to time, you will need to modify an existing user


account. The syntax for usermod is very similar to that used by
useradd.
Syntax: usermod options username
• All the Information about the user is stored in the /etc/passwd file
and when we execute this command then it provide the information
of all the users.
• If we change the information of a user and we want to see the
updated information then we can use grep username /etc/passwd
command to see the information of single user

Ms. Ritu Rathee


ITL CS110 19
Using usermod

• To Change the Description of a user


usermod –c “new Description” username
• To change the name of the user
usermod –l new name old name
• To change the home Directory of the User
usermod –d New path username
• To change the Expiery date of the user account
usermod –e Expiery Date username
To check the expiery date of a user account use chage –l username

Ms. Ritu Rathee


ITL CS110 20
Using usermod

• To Lock the user account


usermod –L username
• To Unlock the user account
usermod –U username
• To provide unencrypted password for the user
usermod –p password username
• To Change the UID of the user
usermod –u UID username
• To Change the GID of the user
usermod –g UID username
• To Change the Shell of the user
usermod –s new shell username

Ms. Ritu Rathee


ITL CS110 21
Using userdel
Syntax:
userdel username
• It’s important to note that, by default, userdel will not remove the user’s
home directory from the file system. If you do want to remove the home
directory when you delete the user, you need to use the –r option in the
command line. For example, entering userdel –r ram will remove the
account and delete her home directory.

ITL CS110 Dr. Anupam Baliyan - G26 22


Linux Group

• How Linux groups work


• Managing groups from the command line

ITL CS110 Dr. Anupam Baliyan - G26 23


How Linux Groups Work
• If your Linux system has been configured to use local
authentication, your groups are defined in the /etc/group
file. Each record is composed of the following four ields:
Group:Password:GID:Users
• Group Specifies the name of the group. In the example
above, the name of the group is video.
• Password Specifies the group password.
• GID Specifies the group ID (GID) number of the group.
• Users Lists the members of the group.

ITL CS110 Dr. Anupam Baliyan - G26 24


Working With Group
• There are two types of Groups in Linux. Primary Group and
the Secondary group.
• When we add a user then a group is also created at the same
time and the name of group is same as of user. This is called
the Primary Group.
• Secondary Group is any group you are a member of other then
the primary group.
• All Groups are added with groupadd command and found in
/etc/group file.
• We can add the user to the Group with usermod command
• Users can change their current Group by using usermod
Command
• Users can check their current Group by using id Command
Ms. Ritu Rathee
ITL CS110 25
Group…….
• Always Remember –G option is used for Secondary Group and –g option
is used for primary group.
• To add a Group : groupadd groupname
• To see the Group : cat /etc/group
• To add a new user to a group : useradd –G groupname username
• To add a existing user to a group: useradd –G groupname username
• To change the group of a user : usermod –G groupname username
• To add a user to ore then one group:
usermod –G Group1 Group2 username
• To check the primary group : id groupname
• To change the Primary Group of a user :
usermod –g groupname username
• To remove a group ; groupdel groupname
Ms. Ritu Rathee
ITL CS110 26
Using Groupmod

• To modify a group, including adding users to the group


membership, you use the groupmod utility.
• Syntax:
groupmod options group
• Options:
–g Changes the group’s GID number.
–p Changes the group’s password.
–n Change the name of a Groiup.
-o Allow to use a duplicate GID
-p Change the password to encrypted password

Ms. Ritu Rathee


ITL CS110 27
26
Practice Time

• Add a user banana


• Add groups fruits and vegetable.
• Check the groups and user
• Add a new user apple to group fruits
• Add existing user banana to group fruits
• Change the secondary group of banana
• Add banana to more then one group
• Check the primary group of the banana
• Change the primary group of the banana
• Remove the vegetable group

Ms. Ritu Rathee


ITL CS110 28
Quiz Time
• User’s Primary Group id is listed in which file, at the time of creation of the
user
a) /etc/passwd
b) /etc/groups
c) /etc/login
d) /etc/profile
• The encrypted password of a user is stored in
a) /etc/shadow
b) /etc/enpasswwd
c) /etc/.passwd
d) /etc/passwd
• Which of the following files need to be referred for user’s secondary
group?
a) /etc/passwd
b) /etc/shadow
c) /etc/group
d) /etc/profile
Ms. Ritu Rathee
ITL CS110 29
• Which command changes anupam's home directory from its default
location to /usr/local directory?
a) usermod -d /usr/local anupam
b) usermod -g /usr/local anupam
c) usermod -l /usr/local anupam
d) usermod -a /usr/local anupam
• Which command and options are used to delete a user’s account and home
directory?
a) userdel –r
b) usermod –f
c) passwd –e
d) usermod -e

Ms. Ritu Rathee


ITL CS110 30
Introduction to Linux (CS-110) Class
On

Local Security Principles in Linux

Dr. Vidhu Baggan


Department Of Computer Science and Engineering
Chitkara University Punjab

Ritu Rathee Introduction to Linux CS110 G28,


G29
Local Security Principles

• Understanding Linux Security


• Understanding the Uses of root
• Difference b/w su and sudo command
• Assigning sudo privileges to new user
• Working with Passwords
• Permissions modification using chmod, chown and chgrp

Ritu Rathee Introduction to Linux CS-110 2


User Accounts
The Linux kernel allows properly authenticated users to access files and applications. Each
user has a unique UID. Upon account creation, new user information is added to the user
database and the user's home directory must be created and populated with some essential
files. Command line programs such as useradd and userdel as well as GUI tools are used for
creating and removing accounts.
For each user, the following seven fields are maintained in the /etc/passwd file:
Field Name Details Remarks

Username User login name Should be between 1 and 32 characters long

Password User password (or the character x if the password Is never shown in Linux when it is being typed; this stops prying eyes
is stored in the /etc/shadow file) in encrypted
format

User ID (UID) Every user must have a user id (UID) •UID 0 is reserved for root user
•UID's ranging from 1-99 are reserved for other predefined accounts
•UID's ranging from 100-999 are reserved for system accounts and
groups
•Normal users have UID's of 1000 or greater
Group ID (GID) The primary Group ID (GID); Group Identification Is covered in detail in the chapter on Processes
Number stored in the /etc/group file

User Info This field is optional and allows insertion of extra For example: Rufus T. Firefly
information about the user such as their name

Home Directory The absolute path location of user's home For example: /home/rtfirefly
directory
Shell The absolute location of a user's default shell For example: /bin/bash
Ritu Rathee
Introduction to Linux CS-110 3
User’s Password File

The SHA-512 algorithm is widely used for security applications and protocols. These
security applications and protocols include TLS, SSL, PHP, SSH, S/MIME and
IPSec. SHA-512 is one of the most tested hashing algorithms.

Ritu Rathee
Introduction to Linux CS-110 4
Types of Accounts

Linux has four types of accounts:

• Root
• System
• Normal
• Network
For a safe working environment, it is advised to grant the minimum
privileges possible and necessary to accounts, and remove inactive
accounts. The last utility, which shows the last time each user logged
into the system, can be used to help identify potentially inactive
accounts which are candidates for system removal.

Ritu Rathee
Introduction to Linux CS-110 5
Understanding the root Account
• root is the most privileged account on a Linux/UNIX system. This account has the
ability to carry out all facets of system administration, including adding accounts,
changing user passwords, examining log files, installing software, etc. Utmost care
must be taken when using this account. It has no security restrictions imposed upon
it.
• When you are signed in as, or acting as root, the shell prompt displays '#' (if you
are using bash and you haven’t customized the prompt as we discuss elsewhere in
this course). This convention is intended to serve as a warning to you of the
absolute power of this account.
Operations that require root Privileges

Ritu Rathee
Introduction to Linux CS-110 6
Creating a New User Account

1. At the command prompt, as root type useradd <username>and press


the ENTER key.
2. To set the initial password, type passwd <username> and press the
ENTER key. . The New password: prompt is displayed.

3. Enter the password and press the ENTER key. To confirm the password,
the prompt Retype new password: is displayed.
4. Enter the password again and press the ENTER key.
The message passwd: all authentication tokens updated successfully. is
displayed.

Ritu Rathee
Introduction to Linux CS-110 7
Comparing su and sudo
su sudo
1 When elevating privilege, you need When elevating privilege, you need to enter the
to enter the root password. user’s password and not the root password.

2 Once a user elevates to the root Offers more features and is considered more
account using su, the user can do secure and more configurable.
anything that the root user can do.

3 The command has limited logging The command has detailed logging features.
features

sudo has the ability to keep track of unsuccessful attempts at gaining root
access. Users' authorization for using sudo is based on configuration
information stored in the /etc/sudoers file and in
the /etc/sudoers.d directory.
A message such as the following would appear in a system log file
(usually /var/log/secure) when trying to execute sudo bash without
successfully authenticating the user:
Ritu Rathee
Introduction to Linux CS-110 8
sudoers File

• Whenever sudo is invoked, a trigger will look at /etc/sudoers and


the files in /etc/sudoers.d to determine if the user has the right to
use sudo and what the scope of their privilege is. Unknown user
requests and requests to do operations not allowed to the user even
with sudo are reported. You can edit the sudoers file by
using visudo, which ensures that only one person is editing the file
at a time, has the proper permissions, and refuses to write out the
file and exit if there is an error in the changes made.
• The basic structure of an entry is:

who where = (as_whom) what

Ritu Rathee
Introduction to Linux CS-110 9
Sudo logging

By default, sudo commands and any failures are logged


in /var/log/auth.log under the Debian distribution family, and
in /var/log/messages and/or /var/log/secure on other systems. This is
an important safeguard to allow for tracking and accountability
of sudo use. A typical entry of the message contains:
• Calling username
• Terminal info
• Working directory
• User account invoked
• Command with arguments.

Ritu Rathee
Introduction to Linux CS-110 10
MulTi-Tasking and MulTi-User
System

Ritu Rathee
Introduction to Linux CS-110 11
Permissions

Ritu Rathee
Introduction to Linux CS-110 12
chmod

The chmod command is used to change the permissions of a file or


directory

Specifying the permissions :


• rwx rwx rwx = 111 111 111
• rw- rw- rw- = 110 110 110
• rwx --- --- = 111 000 000
and so on...
• rwx = 111 in binary = 7
• rw- = 110 in binary = 6
• r-x = 101 in binary = 5
• r-- = 100 in binary = 4

Ritu Rathee
Introduction to Linux CS-110 13
File Permissions

Value Meaning

777 rwxrwxrwx

755 rwxr-xr-x

700 rwx------

666 rw-rw-rw-

644 rw-r--r--

600 rw-------

Ritu Rathee
Introduction to Linux CS-110 14
If we wanted to set some_file to have read and write permission for the
owner, but wanted to keep the file private from others, we would:

[me@linuxbox me]$ chmod 600 some_file

Ritu Rathee
Introduction to Linux CS-110 15
Directory permissions

777 rwxrwxrwx

755 rwxr-xr-x

700 rwx------

Ritu Rathee
Introduction to Linux CS-110 16
File permissions

To see the permission settings for a file, we can use the ls command as
follows:
[me@linuxbox me]$ ls -l some_file
-rw-rw-r-- 1 me we 1097374 Apr 27 18:48 some_file

• The file "some_file" is owned by user "me"


• User "me" has the right to read and write this file
• The file is owned by the group “we"
• Members of the group “we" can also read and write this file
• Everybody else can read this file

Ritu Rathee
Introduction to Linux CS-110 17
• Let's try another example. We will look at the bash program which
is located in the /bin directory:

[me@linuxbox me]$ ls -l /bin/bash -rwxr-xr-x


1 root root 316848 Apr 27 2020 /bin/bash

Dr. Vidhu Baggan Introduction to Linux CS-110 18


Becoming the superuser for a short
while

• su

[me@linuxbox me]$ su
Password:
[root@linuxbox me]#

sudo
[me@linuxbox me]$ sudo some_command Password: [me@linuxbox
me]$

Dr. Vidhu Baggan Introduction to Linux CS-110 19


chown command

• Changing file ownership


example: Suppose I wanted to change the owner of some_file from
"me" to "you".
[me@linuxbox me]$ su Password:
[root@linuxbox me]# chown you some_file
[root@linuxbox me]# exit [me@linuxbox me]$

chown works the same way on directories as it does on files.

Ritu Rathee
Introduction to Linux CS-110 20
Chgrp and Chown

Changing group ownership

[me@linuxbox me]$ chgrp new_group some_file

chown USER:GROUP FILE


chown linuxize:users file1
If you omit the group name after the colon (:) the group of the file is
changed to the specified user’s login group:
chown linuxize: file1
chown :GROUP FILE
chown -R USER:GROUP DIRECTORY

Ritu Rathee
Introduction to Linux CS-110 21
Activity

• Create a new user, using useradd, and give the user an initial password
with passwd.
• Configure this user to be able to use sudo.
• Login as or switch to this new user and make sure you can execute a
command that requires root privilege.
• For example, a trivial command requiring root privilege could be:
$ ls /root
• Click the link below to view a solution to the Lab exercise.

Ritu Rathee
Introduction to Linux CS-110 22
Activity Solution

• sudo useradd newuser sudo passwd newuser (give the password


for this user when prompted)
• With root privilege, (use sudo visudo) add this line
to /etc/sudoers:newuser ALL=(ALL) ALLAlternatively, create a
file named /etc/sudoers.d/newuser with just that one line as
content.
• You can login by doing:sudo su newuserorssh
newuser@localhostwhich will require giving newuser's password,
and is probably a better solution. Instead of localhost you can give
your hostname, IP address or 127.0.0.1. Then as newuser just
type:sudo ls /root

Ritu Rathee
Introduction to Linux CS-110 23
Questions

Ritu Rathee
Introduction to Linux CS-110 24
rea

Ritu Rathee
Introduction to Linux CS-110 25
Introduction to Linux (CS-110) Class
On

Filters in Linux

Dr. Vidhu Baggan


Department Of Computer Science and Engineering
Chitkara University Punjab

Dr. Jatin Arora ITL CS110-G25 1


Redirectional Operator
• Whenever you execute a program/command at the terminal, 3 files
are always open, viz., standard input: < , standard output: >>,> ,
standard error >.

Dr. Jatin Arora ITL CS110-G25 2


What is Redirection?

Dr. Jatin Arora ITL CS110-G25 3


Output Redirection

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

Note: 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.

Dr. Jatin Arora ITL CS110-G25 4


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.

Dr. Jatin Arora ITL CS110-G25 5


Input redirection

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

Dr. Jatin Arora ITL CS110-G25 6


Error Redirection?

The file descriptor for standard error is 2.


Using "2>" we re-direct the error output to a file named "errorfile"

Dr. Jatin Arora ITL CS110-G25 7


Example Cont..

Dr. Jatin Arora ITL CS110-G25 8


Pipelines

• The most useful and powerful thing you can do with I/O redirection is
to connect multiple commands together with what are called pipelines.
With pipelines, the standard output of one command is fed into the
standard input of another.

Dr. Jatin Arora ITL CS110-G25 9


Summary : Redirection operators

Dr. Jatin Arora ITL CS110-G25 10


Filters Concept
Filters??
• A Filter is a Unix Command Line Utility that has the following properties:
• It takes standard Input.
• It performs some processing on the data it reads
• It produces output based upon that input.
For example:
• wc is filter, The processing it performs is counting lines, words and characters
,however ls is not a filter, as it does not take a input

Dr. Jatin Arora ITL CS110-G25 11


List of Filters
1. head
2. more
3. tail
4. less
5. tr
6. nl
7. tee
8. cut
9. paste
10. uniq
11. sort
12. wc

Dr. Jatin Arora ITL CS110-G25 12


wc
• wc stands for word count. As the name implies, it is mainly used for counting
purpose.
• It is used to find out number of lines, word count, byte and characters count in
the files specified in the file arguments. ( l, w, c)
$ cat capital.txt
Hyderabad
Itanagar
Dispur
Patna
Raipur
wc capital.txt
5 5 45 capital.txt
wc -c state.txt capital.txt
63 state.txt
45 capital.txt
108 total

Dr. Jatin Arora ITL CS110-G25 13


Head/tail
head is used to display the first parts of a file, it outputs the first 10 lines by
default. You can use the -n num flag to specify the number of lines to be
displayed:
head -n 5 /var/log/auth.log
tail outputs the last parts (10 lines by default) of a file. Use the -n num
switch to specify the number of lines to be displayed.
tail -n 5 /var/log/auth.log
sort domains.list linuxsay.txt

Dr. Jatin Arora ITL CS110-G25 14


uniq
• uniq command is used to report or omit repeated lines, it filters lines
from standard input and writes the outcome to standard output.
• After running sort on an input stream, you can remove repeated
lines with uniq as in the example below.
• To indicate the number of occurrences of a line, use the -c option
and ignore differences in case while comparing by including
the -i option:
sort domains.list | uniq –c

Dr. Jatin Arora ITL CS110-G25 15


More/Less
more command is a useful file perusal filter created basically for certificate viewing. It
shows file content in a page like format, where users can press [Enter] to view more
information
more /etc/passwd
Or
cat /etc/passwd | more
less is the opposite of more command above but it offers extra features and it’s a little
faster with large files
less /etc/passwd
Or
cat /etc/passwd | less

Dr. Jatin Arora ITL CS110-G25 16


tee
• The tee command reads standard input (stdin) and writes it to both
standard output (stdout) and one or more files. tee is usually part of
a pipeline, and any number of commands can precede or follow it
[command] | tee -a [filename]

Dr. Jatin Arora ITL CS110-G25 17


Tee Cont..

Write to Multiple Files

Dr. Jatin Arora ITL CS110-G25 18


paste

• paste is a command that allows you to merge lines of files


horizontally. It outputs lines consisting of the sequentially
corresponding lines of each file specified as an argument, separated
by tabs.
paste file1 file2
paste -d '_' file1 file2

paste -s file1 file2 # Serially merge the files

Dr. Jatin Arora ITL CS110-G25 19


tr
tr (short for translate) is a useful command line utility that translates
and/or deletes characters from stdin input, and writes to stdout. It is a
useful program for manipulating text on the command line.
cat linux.txt

linux is my life
linux has changed my life
linux is best and everthing to me..:)

cat domains.txt | tr [:lower:] [:upper:]

LINUX IS MY LIFE
LINUX HAS CHANGED MY LIFE
LINUX IS BEST AND EVERTHING TO ME..:)
cat linux.txt | tr [a-z] [A-Z]
cat domains.txt | tr -d '‘ # Remove the spaces

Dr. Jatin Arora ITL CS110-G25 20


nl

NL is number lines. It’s main purpose is to display line numbers of a


file
[savona@putor tmp]$ nl /etc/passwd

[savona@putor tmp]$ nl -s:" " /etc/passwd


1: root:x:0:0:root:/root:/bin/bash
2: bin:x:1:1:bin:/bin:/sbin/nologin
3: daemon:x:2:2:daemon:/sbin:/sbin/nologin
4: adm:x:3:4:adm:/var/adm:/sbin/nologin
5: lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6: sync:x:5:0:sync:/sbin:/bin/sync
..OUTPUT TRUNCATED.

Dr. Jatin Arora ITL CS110-G25 21


nl styles

Using Styles ( -b )
• There are three output styles to choose from:
• a = Number all lines
• t = Number only nonempty lines
• n = Number no Lines
• p = Number only lines that contain a match for the basic regular expression
[savona@putor tmp]$ nl -ba test
1 This is a test file
2
3
4 Above are some blank lines
5
6
7 Maybe they will will be numbered, maybe not.
8 This is the end. Beautiful friend, the end.

Dr. Jatin Arora ITL CS110-G25 22


cut

• Linux cut command is useful for selecting a specific column of a


file. It is used to cut a specific sections by byte position, character,
and field and writes them to standard output. It cuts a line and
extracts the text data. It is necessary to pass an argument with it;
otherwise, it will throw an error message.
• To cut a specific section, it is necessary to specify the delimiter. A
delimiter will decide how the sections are separated in a text file.
Delimiters can be a space (' '), a hyphen (-), a slash (/), or anything
else. After '-f' option, the column number is mentioned.

Dr. Jatin Arora ITL CS110-G25 23


Dr. Jatin Arora ITL CS110-G25 24
Dr. Jatin Arora ITL CS110-G25 25
Dr. Jatin Arora ITL CS110-G25 26
Add Practice work/MCQs

Dr. Jatin Arora ITL CS110-G25 27


Thank you

Dr. Jatin Arora ITL CS110-G25 28


Introduction to Linux (CS-110) Class
On

Filters using Regular Expressions


Pattern Matching (grep), egrep, fgrep, sed (-n, -e, -f, context addressing,
writing selected lines to a file, deleting lines, substitution).

Dr. Poonam Panwar


Department of Computer Science and Engineering,
Chitkara University, Punjab
Introduction to Linux Dr. Poonam Panwar G32, G33
Index

1. The Sample Database


2. grep: Searching for a Pattern
3. Extended Regular Expressions (ERE) and egrep
4. fgrep
5. sed
6. Context addressing
7. substitution

Introduction to Linux Dr. Poonam Panwar G32, G33


The Sample Database

In this lecture we’ll often refer to the file emp.lst. Let’s take a close look at the file and
understand the organization:

Download here : http://codingitright.blogspot.com/2013/07/emplst.html

Introduction to Linux Dr. Poonam Panwar G32, G33


grep: Searching for a Pattern

grep scans its input for a pat- tern, and can display the selected pattern, the line
numbers, or the filenames where the pattern occurs. The command uses the
following syntax:

grep options pattern filename(s)

grep searches for pattern in one or more filename(s), or the standard input if no
filename is specified. The first argument (barring the option) is the pattern, and
the ones remain- ing are filenames. Let’s use grep to display lines containing the
string sales from the sample database, emp.lst:

Introduction to Linux Dr. Poonam Panwar G32, G33


Introduction to Linux Dr. Poonam Panwar G32, G33
Because grep is also a filter, it can search its standard input for the pattern and store the
output in a file:

who | grep henry > foo

When grep is used with multiple filenames, it displays the filenames along with the
output. In the next example, grep searches two files. Don’t bother about what they
contain; just observe how each line is preceded by the filename:

Introduction to Linux Dr. Poonam Panwar G32, G33


To suppress the filenames, you can use cut to select all but the first field using grep as
its input. Alternatively, you can also make grep ignorant of the source of its input by
using cat emp[12].lst | grep “director”.

Introduction to Linux Dr. Poonam Panwar G32, G33


Quoting in grep
Though we have used the pattern both with (“sales”) and without quotes (henry), it’s always safe to
quote the pattern. Quoting is essential if the search string consists of more than one word or uses any
of the shell’s characters like *, $, and so on. Let’s use a two-word string both within and without
quotes:

Introduction to Linux Dr. Poonam Panwar G32, G33


When quoting patterns, the shells of the Bourne family issue a > if the closing quote is absent in
the line. The C shell simply outputs the error message Unmatched ‘.

Quote the pattern used with grep if it contains multiple words or special characters that can be
interpreted otherwise by the shell. You can generally use either single or double quotes. However,
if the special characters in the pattern require command substitution or variable evaluation to be
performed, you must use double quotes.

Introduction to Linux Dr. Poonam Panwar G32, G33


When grep Fails
Like cmp, grep can also behave silently. It simply returns the prompt when the pattern can’t be
located:
$ grep president emp.lst
$_ /*president not found */

The command failed because the string president couldn’t be located. Though the feature of scanning
a file for a pattern is available in both sed and awk, these commands are not considered to fail if
they can’t locate a pattern. find also doesn’t fail if no file is found.
Don’t, however, draw the wrong conclusion from the preceding behavioral pattern of grep. The
silent return of the shell prompt is no evidence of failure. In fact, the silent behavior of cmp denotes
success. Success or failure is denoted by the exit status (7.5) that is stored in a special variable ($?)
when a command has finished execution. The if conditional and while loop test this exit status to
control the flow of execution.

Introduction to Linux Dr. Poonam Panwar G32, G33


grep Options

Introduction to Linux Dr. Poonam Panwar G32, G33


$ grep -i ‘WILCOX’ emp.lst
Note
2345:james wilcox :g.m. :marketing :03/12/45:110000

Deleting Lines (-v) grep can also play an inverse role; the -v (inverse) option selects all lines except
those containing the pattern. Thus, you can create a file otherlist containing all but directors:

$ grep -v ‘director’ emp.lst > otherlist $ wc -l otherlist

11 otherlist There were four directors initially

More often than not, when we use grep -v, we also redirect its output to a file as a means of getting
rid of unwanted lines. Obviously, the lines haven’t been deleted from the original file as such.
The -v option removes lines from grep’s output, but it doesn’t actually change the argument file.
This option is frequently used with redirection.
Introduction to Linux Dr. Poonam Panwar G32, G33
Displaying Filenames (-l)
Programmers often use the -l (list) option to locate files where a variable or system call has been
used. You can easily find out the C programs that use the fork system call:

$ grep -l fork *.c

fork.c: printf(“Before fork\n”);


fork.c: pid = fork(); /* Replicates current process */
orphan.c: if ((pid = fork()) > 0) /* Parent */
wait.c: switch(fork()) {

Assuming that the pattern can occur multiple times in a file, can you sort this file list in order of their
modification or access time?

Introduction to Linux Dr. Poonam Panwar G32, G33


Matching Multiple Patterns (-e)

The -e option has two functions—to match multiple patterns and patterns beginning with a hyphen.
Linux supports both functions, but Solaris offers this option only with the XPG4 version. This is
how you match multiple patterns by using -e multiple times:

$ grep -e woodhouse -e wood -e woodcock emp.lst

2365:john woodcock :director :personnel :05/11/47:120000 5423:barry wood :chairman :admin


:08/30/56:160000 1265:p.j. woodhouse :manager :sales :09/12/63: 90000

grep supports sophisticated pattern matching techniques that can display the same lines but with a
single expression. This is the ideal forum for regular expressions to make their entry.

Introduction to Linux Dr. Poonam Panwar G32, G33


Patterns Beginning with a - (-e)
What happens when you look for a pattern that begins with a hyphen?

$ grep “-mtime” /var/spool/cron/crontabs/* grep: illegal option -- m

grep: illegal option -- t


grep: illegal option -- m
grep: illegal option -- e
Usage: grep -hblcnsviw pattern file . . .
grep treats -mtime as a combination of five options of which only one is legitimate (-i); the others
are “illegal.” To locate such patterns, you must use the -e option:

$ grep -e “-mtime” /var/spool/cron/crontabs/* romeo:55 17 * * 4 find / -name core -mtime +30


-print

Introduction to Linux Dr. Poonam Panwar G32, G33


Extended Regular Expressions (ERE) and
egrep

Extended regular expressions (ERE) make it possible to match dissimilar patterns with a single
expression. This set uses some additional characters and POSIX-compliant versions of grep use them
with the -E option. Linux grep supports this option, but Solaris users must use /usr/xpg4/bin/grep to
use EREs. If your version of grep doesn’t support this option, then use egrep but without the -E
option.

Introduction to Linux Dr. Poonam Panwar G32, G33


Introduction to Linux Dr. Poonam Panwar G32, G33
Matching Multiple Patterns (|, ( and ))
The | is the delimiter of multiple patterns. Using it, we can locate both woodhouse and woodcock without
using the -e option twice:
Note
All EREs can also be placed in a file in exactly the same way they are used in the command line. You then
have to use grep both with the -E and -f options to take the patterns from the file.
If grep doesn’t support the -E option on your machine, use egrep without the -E option for all examples
considered in this section.
$ grep -E ‘woodhouse|woodcock’ emp.lst
2365:john woodcock :director :personnel :05/11/47:120000 1265:p.j. woodhouse :manager :sales :09/12/63:
90000
The ERE thus handles the problem easily, but offers an even better alternative. The characters ( and ) let you
group patterns, and when you use the | inside the parentheses, you can frame an even more compact pattern:
$ grep -E ‘wood(house|cock)’ emp.lst
2365:john woodcock :director :personnel :05/11/47:120000 1265:p.j. woodhouse :manager :sales :09/12/63:
90000

Introduction to Linux Dr. Poonam Panwar G32, G33


Introduction to Linux Dr. Poonam Panwar G32, G33
Fgrep

Fgrep or the Fixed grep or grep -F is yet another version of grep which is fast in searching when it
comes to search for the entire string instead of regular expression as it doesn‘t recognize the regular
expressions, neither any meta-characters. For searching any direct string, this is the version of grep
which should be selected.
Fgrep searches for complete string and doesn‘t even recognize special characters as part of regular
expression even if escaped or not escaped. Options used in grep are also can be used with fgrep.

Introduction to Linux Dr. Poonam Panwar G32, G33


You can see in the above example the fgrep donot support regular expressions.

Introduction to Linux Dr. Poonam Panwar G32, G33


In this example you can see fgrep supports various options that are used
in grep and egrep.

Introduction to Linux Dr. Poonam Panwar G32, G33


sed:The Stream Editor

sed is a multipurpose tool that combines the work of several filters. It is derived from ed, the original
UNIX editor (not discussed in this text). sed performs noninteractive operations on a data
stream—hence its name. It uses very few options but has a host of features that allow you to select
lines and run instructions on them. Learning sed will prepare you well for perl, which uses many of
these features.
sed uses instructions to act on text. An instruction combines an address for selecting lines, with an
action to be taken on them, as shown by the syntax:
sed options ‘address action’ file(s)

Introduction to Linux Dr. Poonam Panwar G32, G33


The address and action are enclosed within single quotes. Addressing in sed is done
in two ways:
• By one or two line numbers (like 3,7).
• By specifying a /-enclosed pattern which occurs in a line (like /From:/).
In the first form, address specifies either one line number to select a single line or a set of two (3,7)
to select a group of contiguous lines. Likewise, the second form uses one or two patterns. The action
component is drawn from sed’s family of internal commands. It can either be a simple display (print)
or an editing function like insertion, deletion, or substitution of text.
sed processes several instructions in a sequential manner. Each instruction operates on the output of
the previous instruction. In this context, two options are relevant, and most likely they are the only
ones you’ll use with sed—the -e option that lets you use multiple instructions and the -f option to
take instructions from a file. Both options are used by grep in an identical manner.

Introduction to Linux Dr. Poonam Panwar G32, G33


Internal Commands Used by sed

Introduction to Linux Dr. Poonam Panwar G32, G33


Components of a sed Instruction

Introduction to Linux Dr. Poonam Panwar G32, G33


Context Addressing

This form of addressing lets you specify a pattern (or two) rather than line numbers. This is known
as context addressing where the pattern has a / on either side.
Searching a pattern:

Introduction to Linux Dr. Poonam Panwar G32, G33


For Ignoring the case use I option, as shown below:

Searching using regular expression. See below:

Introduction to Linux Dr. Poonam Panwar G32, G33


You can also specify a comma-separated pair of context addresses to select agroup of contiguous
lines.
sed -n /director/,/manager/Ip‘ emp.lst
Writing Selected Lines to a File (w): Irrespective of the way you select lines (by line or context
addressing), you can use the w (write) command to write the selected lines to a separate file. In the
below example the director pattern is searched and that lines where the pattern found are saved in a
separate file named as asd.

Introduction to Linux Dr. Poonam Panwar G32, G33


Writing Multiple Selected Lines to a File

Sed –e ‘/<FORM>/,/\/FORM>/W’ forms.html


/<FRAME>/,/<\FRAME>/w frames.html
/<TABLE>/,/<\/TABLE>/w tables.html’ pricelist.html

Introduction to Linux Dr. Poonam Panwar G32, G33


Substitution (s)

Substitution is easily the most important feature of sed, and this is one job that sed does
exceedingly well. It lets you replace a pattern in its input with something else. The use of regular
expressions enhances our pattern matching capabilities, and in this chapter we feature some more
regular expression characters that make the use of sed so compelling.

$ sed ‘s/:/|/’ emp.lst | head -n 2


2233|charles harris :g.m. :sales :12/12/52: 90000 9876|bill johnson :director
:production:03/12/50:130000

Introduction to Linux Dr. Poonam Panwar G32, G33


Just look at what sed has done; only the first (leftmost) instance of the : in a line has been replaced.
You need to use the g (global) flag to replace all the colons:
$ sed ‘s/:/|/g’ emp.lst | head -n 2

2233|charles harris |g.m. |sales |12/12/52| 90000 9876|bill johnson |director


|production|03/12/50|130000

Introduction to Linux Dr. Poonam Panwar G32, G33


We used global substitution to replace all colons with pipes. Here, the command s is the same as 1,$s.

Though we see two lines here, the substitution has been carried out for the entire file. Also,

substitution is not restricted to a single character; it can be any string:

sed ‘s/<I>/<EM>/g’ foo.html

You can also limit the vertical boundaries by specifying an address:

sed ‘1,3s/:/|/g’ emp.lst First three lines only And you can remove the source string altogether by using

a null string between the last

two /s:

sed ‘s/<I>//’ foo.html Deletes first occurrence of <I> in every line

sed ‘1,3s/://g’ emp.lst Deletes all occurrences of : in first three lines

When a g is used at the end of a substitution instruction, the change is performed globally

along the line. Without it, only the leftmost occurrence is replaced.

Introduction to Linux Dr. Poonam Panwar G32, G33


Performing Multiple Substitutions
You can perform multiple substitutions with one invocation of sed. Simply press [Enter] at the end
of each instruction, and then close the quote at the end. This is how you replace three HTML tags:
$ sed ‘s/<I>/<EM>/g Can also specify in a single line > s/<B>/<STRONG>/g with ; as the
delimiter of commands > s/<U>/<EM>/g’ form.html
sed is a stream editor; it works on a data stream. This means that an instruction processes the output
of the previous one. This is something users often forget; they don’t get the sequence right. Note that
the following sequence finally converts all <I> tags to <STRONG>:
$ sed ‘s/<I>/<EM>/g
> s/<EM>/<STRONG>/g’ form.html
When you have a group of instructions to execute, place them in a file and use sed with the -f option.

Introduction to Linux Dr. Poonam Panwar G32, G33


MCQ
1. Which one of the following command is used for searching for a pattern in one or more file(s)?
a) cd
b) cp
c) paste
d) grep
2. Which one of the following is the correct syntax for grep command?
a) grep options filename(s)
b) grep options pattern
c) grep pattern filename
d) grep options pattern filename(s)
3. Which of the following commands is known as stream editor?
a) sed
b) grep
c) grep
d) tr
Introduction to Linux Dr. Poonam Panwar G32, G33
4. Which one of the following is the correct syntax for performing substitution using sed?
a) sed [address]s /expr1/ expr2
b) sed [address]s /expr1 expr2
c) sed [address]s /expr1/ expr2/ flags
d) sed [address]s

Introduction to Linux Dr. Poonam Panwar G32, G33


Student Activity

1. Write a command to print the lines that has the the pattern "july" in all the files in a
particular directory?

2. Write a command to select only those lines containing "july" as a whole word?

Introduction to Linux Dr. Poonam Panwar G32, G33


Introduction to Linux (CS-110) Class
On
Process Oriented Commands

Dr. Poonam Panwar


Department of Computer Science and Engineering,
Chitkara University, Punjab

Introduction to Linux Dr. Poonam Panwar G32,G33


Index

1. Process
2. Shell and init
3. ps
4. Top
5. kill
6. killall

Introduction to Linux Dr. Poonam Panwar G32,G33


Process
A process is simply an instance of a running program. It is said to be born when the
program starts execution and remains alive as long as the program is active. After
execution is complete, the process is said to die. However, there are some attributes that
are not inherited and are allocated by the kernel when a process is born:

• The Process-id (PID) Each process is identified by a unique integer called the
Process-id (PID).
• The Parent PID (PPID) When several processes have the same PPID, it often makes
sense to kill the parent rather than all of its children separately.

Introduction to Linux Dr. Poonam Panwar G32,G33


The Shell and init

When you log in, the process representing the shell starts running at your terminal. This
process may be sh, ksh, csh, or bash. The shell maintains a set of environment variables,
and you have already encountered some of them like PATH and HOME. The shell’s own
pathname is stored in SHELL, but its PID is stored in a special “variable”, $$. To know the
PID of your current shell, type

$ echo $$ /*The PID of the current shell 1078*/

The PID of your login shell obviously can’t change as long as you are logged in. When you
log out and log in again, your login shell will be assigned a different PID. Knowledge of the
PID is often necessary to control the activities at your terminal, especially when things go
wrong.

Introduction to Linux Dr. Poonam Panwar G32,G33


ps: Displaying Process Attributes

By default, ps displays the processes owned by the user invoking the command:

$ ps

PID TTY TIME CMD


1078 pts/4 0:00 bash

ps presents a snapshot of the process table. This picture gets outdated by the time it is
displayed. On some systems, you might see ps itself in the output. ps is a highly variant
command; its actual output varies across different LINUX flavors.

Introduction to Linux Dr. Poonam Panwar G32,G33


Options to ps

Introduction to Linux Dr. Poonam Panwar G32,G33


Displaying the PPID (-f)

Since knowing the parentage is often important, the -f option displays a fuller listing that
includes the PPID:

$ ps -f

UID PID PPID C STIME TTY TIME


sumit 1081 1078 0 19:03:39 pts/4 0:00 sumit 1082 1081 0 19:03:41 pts/4 0:00 sumit 1078 1
0 19:01:53 pts/4 0:00
CMD
vi create_user.sh /usr/bin/bash -i -bash

Introduction to Linux Dr. Poonam Panwar G32,G33


Other Options ps -u followed by a user-id displays the processes owned by
the user-id. The -a option displays processes run by all users, irrespective of
their owner- ship. We’ll discuss two important options (-e and -l) after we have
studied the process creation mechanism.

Introduction to Linux Dr. Poonam Panwar G32,G33


System Processes and init
Even though no one may be using the system, a number of system processes keep
running all the time. They are spawned during system startup by init (PID 1), the parent of
the login shell. The ps -e command lists them all.

System processes that have no controlling terminal are easily identified by the ? in the
TTY column. A process that is disassociated from the terminal can neither write to the
terminal nor read from it. You can’t press [Ctrl-c] to interrupt the process either. Such
processes are also known as daemons. Many of these daemons are actually sleep- ing (a
process state) and wake up only when they receive input.

Introduction to Linux Dr. Poonam Panwar G32,G33


The ps -e Output

Introduction to Linux Dr. Poonam Panwar G32,G33


Displaying Process Ancestry (ps f)
Locating ancestry by matching PIDs and PPIDs can be a grueling affair; a visual representation of the
process tree is what Linux ps offers with the f option. Here’s a section of the output obtained by using
the U option also:

$ ps f U sumit

PID TTY
936 pts/0 14833 pts/0
STAT TIME COMMAND
S 0:00 \_ /bin/bash
S 0:00 | \_ vim yrunix07
938 pts/2 S 14835 pts/2 R 945 pts/4 S 14831 pts/4 S 1047 ? S 14579 ? S
0:00 \_ /bin/bash
0:00 | \_psf-usumit 0:00 \_ /bin/bash
0:00 \_ rlogin arka
22:08 /usr/lib/mozilla-1.0.1/mozilla-bin
0:22 | | \_ /usr/lib/acroread/Reader/intellinux/bin/a

Introduction to Linux Dr. Poonam Panwar G32,G33


System Processes (ps ax)

A typical Linux system shows a host of system processes, but Linux uses the ax option to
display them. Here’s a vastly censored display:

Introduction to Linux Dr. Poonam Panwar G32,G33


Full Listing (ps u)

Introduction to Linux Dr. Poonam Panwar G32,G33


The top Command
Apart from ps, the top command also shows CPU usage in a more humanly readable form.
This command also shows ps-like output, but its first five lines make most interesting
reading:
There’s a whole lot of information here: the free and used memory of the system and the
state of the CPU. Most of the memory is used up (1536K out of 30,628K avail- able), but
the CPU is idling 98.8 percent of the time. This is a very useful command for the system
administrator.

Introduction to Linux Dr. Poonam Panwar G32,G33


The Process Creation Mechanism
A process can only be created by another process, and the creation mechanism involves
three phases. We call them fork, exec, and wait, mostly named after system calls of the
same name. The three phases work as follows:
• Fork Forking creates a process by creating a copy of the existing process.

• Exec Forking creates a process, but it is not enough to run a new program. To do that,
the forked child needs to overwrite its own image with the code and data of the new
program.

• Wait While the child is executing a new program, the parent normally waits for the child
to die. It then picks up the exit status of the child (explained shortly) before it does
something else.

Introduction to Linux Dr. Poonam Panwar G32,G33


ps -l: Detailed Process Listing

The ps -l command (ps aux in Linux) provides an informative listing of processes.


Apart from the usual attributes that we are familiar with, the command also displays the state,
priority, and size of the process in memory:

Introduction to Linux Dr. Poonam Panwar G32,G33


kill: Premature Termination of a Process
The kill command sends a signal usually with the intention of killing the process. kill is an
internal command in most shells; the external /bin/kill is executed only when the shell lacks
the kill capability. The command uses one or more PIDs as its arguments, and by default
uses the SIGTERM (15) signal. Thus,

kill 105 /*It’s like using kill -s TERM 1058*/

terminates the job having PID 105. To facilitate premature termination, the & operator
(7.10.1) displays the PID of the process that’s run in the background. If you don’t re-
member the PID, use the ps command to find out and then use kill.

Introduction to Linux Dr. Poonam Panwar G32,G33


If you run more than one job—either in the background or in different windows in the X
Window system—you can kill them all with a single kill statement. Just specify all of their
PIDs with kill:

kill 121 122 125 132 138 144

If all of these processes have the same parent, you may simply kill the parent to kill all its
children. However, when you use nohup (7.10.2) with a set of commands and log out, you
can’t kill the parent as init acquires their parentage. You then have to kill the processes
individually because you can’t kill init.

Introduction to Linux Dr. Poonam Panwar G32,G33


killall command
The primary contrast between the kill and killall command is that the “kill” ends process
cycles dependent on Process ID number (PID), while the killall orders end running cycles
dependent on their names and different attributes. Normal users can end/kill their own
cycles(processes), however not those that have a place with different users, while the root
client can end all cycles.

General Syntax:
killall [ -Z CONTEXT ] [ -u USER ] [ -y TIME ] [ -o TIME ] [ -eIgiqrvw ]

$ sudo killall yes

Introduction to Linux Dr. Poonam Panwar G32,G33


pstree Command
Pstree command in Linux that shows the running processes as a tree which is a more
convenient way to display the processes hierarchy and makes the output more visually
appealing. The root of the tree is either init or the process with the given pid. Pstree can also be
installed in other Unix systems.
Syntax:
pstree [options] [pid or username]

Introduction to Linux Dr. Poonam Panwar G32,G33


Option Use
Pstree To display process tree
pstree –a To include command line arguments in output

pstree –p To display PIDs


pstree –c To force pstree to expand identical subtrees in output.
pstree -n To sort processes
pstree -u To see who is the owner/user of a process
pstree –h To highlight the current process or any other process
pstree –g To show process group IDs in output
pstree khushi To make pstree display process tree specific to a user.
pstree -V To display version information.

Introduction to Linux Dr. Poonam Panwar G32,G33


MCQ
1. 1. Which command shows some attributes of a process?
a) pid
b) $$
c) ps
d) HOME
2. Which of the following attribute is not shown by ps command?
a) PID
b) PPID
c) tty
d) size
3. What will the output of the following command?
$ ps -t dev/console
a) processes running on terminal named console
b) undefined output
c) erroneous
d) processes running on the current terminal

Introduction to Linux Dr. Poonam Panwar G32,G33


Student Activity

• Create a job that writes the date to an output file thrice, with a gap of 60 seconds and
180 seconds. Check whether the job is running and bring it to foreground job. Stop the
foreground job and make it run in the background. Finally, kill the background job and
verify its status.

Introduction to Linux Dr. Poonam Panwar G32,G33


Introduction to Linux (CS-110) Class
On
Process scheduling

Dr. Poonam Panwar


Department of Computer Science and Engineering,
Chitkara University, Punjab

Introduction to Linux Dr. Poonam Panwar G32,G33


Index

1. Process Scheduling
2. at
3. batch
4. Cron
5. Running jobs in background
6. No Logging Out
7. Nohup
8. Process Priority

Introduction to Linux Dr. Poonam Panwar G32,G33


Process scheduling

Linux provides sophisticated facilities to schedule a job to run at a specified time of day. If the
system load varies greatly throughout the day, it makes sense to schedule less urgent jobs at a time
when the system overheads are low. The at and batch commands make such scheduling possible.

Introduction to Linux Dr. Poonam Panwar G32,G33


at: One-Time Execution
The at command allows you to specify a time when the Linux system will run a script. The at command
submits a job to a queue with directions on when the shell should run the job. Another command, atd,
runs in the background and checks the job queue for jobs to run. Most Linux distributions start this
automatically at boot time.

The basic at command format is pretty simple:


at [-f filename] time

• Besides specifying the time to run the job, you can also include a specific date, using a few different
date formats:
• A standard date format, such as MMDDYY, MM/DD/YY, or DD.MM.YY
• A text date, such as Jul 4 or Dec 25, with or without the year
• You can also specify a time increment:
■ Now + 25 minutes
■ 10:15PM tomorrow
■ 10:15 + 7 days

Introduction to Linux Dr. Poonam Panwar G32,G33


Listing pending jobs

The atq command allows you to view what jobs are pending on the system:
$ at -f test5 10:15
warning: commands will be executed using /bin/sh job 7 at 2007-11-04 10:15
$ at -f test5 4PM
warning: commands will be executed using /bin/sh job 8 at 2007-11-03 16:00
$ at -f test5 1PM tomorrow
warning: commands will be executed using /bin/sh job 9 at 2007-11-04 13:00
$ atq
7 2007-11-04 10:15 a
8 2007-11-03 16:00 a
9 2007-11-04 13:00 a
$
The job listing shows the job number, the date and time the system will run the job, and the job
queue the job is stored in.

Introduction to Linux Dr. Poonam Panwar G32,G33


Introduction to Linux Dr. Poonam Panwar G32,G33
Removing jobs
Once you know the information about what jobs are pending in the job queues, you can use the
atrm command to remove a pending job:
$ atrm 8
$ atq
7 2007-11-04 10:15 a 9 2007-11-04 13:00 a $
Just specify the job number you want to remove. You can only remove jobs that you submit for
execution. You can’t remove jobs submitted by others.

Introduction to Linux Dr. Poonam Panwar G32,G33


batch: Execute in Batch Queue

• The batch command is a little different from the at command. Instead of scheduling a script to
run at a preset time, you use the batch command to schedule a script to run when the system is at
a lower usage level. This is a great feature for servers that may experience different load levels at
various times of the day and night.
• The batch command checks the current load average of the Linux system. If the load average is
below 0.8, it runs any jobs waiting in the job queue.
• The command format for the batch command is:
batch [-f filename] [time]
• Similarly to the at command, by default the batch command reads commands from STDIN. You
can use the -f parameter to specify a file to read commands from. You can also optionally specify
the earliest time that the batch command should try running the job.

Introduction to Linux Dr. Poonam Panwar G32,G33


Restricting Use of at and batch

• Not all users may be able to use the at and batch commands. The access to these com- mands is
restricted and controlled by the files at.allow and at.deny. The locations are system-dependent; look up
the FILES section of the man page of at for the location of the files. If they exist at all, they could be in
/etc, /etc/cron.d or /usr/lib/cron. They can only be edited by the superuser.

• at.allow controls the primary level of security. If it is present, only the users listed in the file are
permitted to use at and batch. If it is not present, the system checks at.deny for a list of users who are
barred from using these commands. If neither file is present, only the system administrator is permitted
to invoke at and batch.

Introduction to Linux Dr. Poonam Panwar G32,G33


The cron table

• The cron table uses a special format for allowing you to specify when a job should be run. The
format for the cron table is:
min hour dayofmonth month dayofweek command
• The cron table allows you to specify entries as specific values, ranges of values (such as 1–5) or as
a wildcard character (the asterisk). For example, if you want to run a command at 10:15 on every
day, you would use the cron table entry of:
15 10 * * * command
• The wildcard character used in the dayofmonth, month, and dayofweek fields indicates that cron
will execute the command every day of every month at 10:15. To specify a command to run at
4:15PM every Monday, you would use:
15 16 * * 1 command
• You can specify the day of week entry as either a three-character text value (mon, tue, wed, thu,
fri, sat, sun) or as a numeric value, with 0 being Sunday and 6 being Saturday.

Introduction to Linux Dr. Poonam Panwar G32,G33


The Components of a crontab Entry

Introduction to Linux Dr. Poonam Panwar G32,G33


Building the cron table

Each system user can have their own cron table (including the root user) for running scheduled jobs.
Linux provides the crontab command for handling the cron table. To list an existing cron table, use the
-l parameter:

$ crontab -l
no crontab for rich
$

By default, each user’s cron table file doesn’t exist. To add entries to your cron table, use the -e
parameter.

Introduction to Linux Dr. Poonam Panwar G32,G33


To create a crontab entry, first use your vi editor to create a file foo with an entry of your choice.
Then use the crontab command

crontab foo

to place the entry in the directory /var/spool/cron/crontabs. You can see the contents
of your crontab file with crontab -l and remove it with
crontab -r.

If you use crontab - to provide input through the standard input and then decide to abort it, you
should terminate it with the interrupt key applicable to your terminal, rather than [Ctrl-d]. If you
forget to do that, you’ll remove all entries from your existing crontab file!

Introduction to Linux Dr. Poonam Panwar G32,G33


Sample crontab Entries

Introduction to Linux Dr. Poonam Panwar G32,G33


• cron is mainly used by the system administrator to perform housekeeping chores, like removing
outdated files or collecting data on system performance. It’s also extremely useful to periodically dial
up to an Internet mail server to send and retrieve mail.

• anacron cron assumes that the machine is run continuously, so if the machine is not up when a job is
scheduled to run, cron makes no amends for the missed op- portunity. The job will have to wait for its
next scheduled run. The anacron com- mand is often more suitable than cron. anacron periodically
inspects its control file (/etc/anacrontab) to see if there’s a job which has “missed the bus.” If it finds
one, it executes the job.

Introduction to Linux Dr. Poonam Panwar G32,G33


Running Jobs in Background
Running a shell script in background mode is a fairly easy thing to do. To run a shell script in background
mode from the command line interface, just place an ampersand symbol after the command:
$ ./test1 &
[1] 19555
$ This is test program
Loop #1
Loop #2 $

ls -l
total 8
-rwxr--r-- 1 rich rich 219 Nov 2 11:27 test1* $ Loop #3

When you place the ampersand symbol after a command it separates the command from the bash shell and
runs it as a separate background process on the system. The first thing that displays is the line:
[1] 19555
The number in the square brackets is the job number assigned to the background process by the shell. The
next number is the PID the Linux system assigns to the process. Every process running on the Linux system
must have a unique PID.

Introduction to Linux Dr. Poonam Panwar G32,G33


When the background process finishes, it displays a message on the terminal:
[1]+ Done ./test1
This shows the job number and the status of the job (Done), along with the command used
to start the job.

Introduction to Linux Dr. Poonam Panwar G32,G33


Running multiple background jobs

You can start any number of background jobs at the same time from the command line prompt:

Introduction to Linux Dr. Poonam Panwar G32,G33


fg command
You can now use the fg command to bring this job to the foreground:

Introduction to Linux Dr. Poonam Panwar G32,G33


Job Control Commands

Introduction to Linux Dr. Poonam Panwar G32,G33


nohup: Log Out Safely
When a command is run with nohup (no hangup), the process continues to run even after the user has
logged out. This feature is not required in the Bash and C shells because background processes in these
shells continue to run even after the user has logged out, but it is required for the Bourne and Korn
shells. You must use the & with it as well:

$ nohup sort emp.lst &


586 PID of this job
Sending output to nohup.out

Introduction to Linux Dr. Poonam Panwar G32,G33


You can now safely log out of the system without aborting the command. If you are running the
command from a window, then close the window. Log in again or run ps from another window or
terminal to notice something quite significant:
$ ps -f -u Romeo
UID PID PPID C STIME TTY TIME COMMAND
sumit 586 1 45 14:52:09 01 0:13 sort emp.lst

The shell died on logging out but its child (sort) didn’t; it turned into an orphan.

Unlike the &, which needs to be affixed only to the end of the command line, nohup needs to be used
with each command in a pipeline:
nohup grep ‘director’ emp.lst & | nohup sort &

Introduction to Linux Dr. Poonam Panwar G32,G33


Process Priority

• In a multitasking operating system (which Linux is), the kernel is responsible for assigning CPU time
for each process running on the system. Only one process at a time can actually be running in the CPU,
so the kernel assigns CPU time to each process in turn.
• By default, all processes started from the shell have the same scheduling priority on the Linux system.
The scheduling priority is the amount of CPU time the kernel assigns to the process relative to the other
processes.
• The scheduling priority is an integer value, from −20 (the highest priority) to +20 (the lowest priority).
By default, the bash shell starts all processes with a priority of 0.
• This means that a simple script that only requires a little bit of processing time gets the same CPU time
slices as a complex mathematical algorithm that can take hours to run.
• Sometimes you want to change the priority of a specific command, either lowering its priority so that it
doesn’t take as much processing power from the CPU or giving it a higher priority so that it gets more
processing time. You can do this by using the nice command.

Introduction to Linux Dr. Poonam Panwar G32,G33


The nice command

Introduction to Linux Dr. Poonam Panwar G32,G33


The renice command

Introduction to Linux Dr. Poonam Panwar G32,G33


The renice command automatically updates the scheduling priority of the running process. Just as with
the nice command, the renice command has some limitations:
• You can only renice processes that you own.
• You can only renice your processes to a lower priority.
• The root user can renice any process to any priority.

If you want to fully control running processes, you must be logged in as the root account.

Introduction to Linux Dr. Poonam Panwar G32,G33


Change the Scheduling Priority

As we mentioned before, Linux allows dynamic priority-based scheduling. Therefore, if a program is


already running, you can change its priority with the renice command in this form:
$ renice -n -12 -p 1055
$ renice -n -2 -u apache

From the sample top output below, the niceness of the teamspe+ with PID 1055 is now -12 and for all
processes owned by user apache is -2.

Introduction to Linux Dr. Poonam Panwar G32,G33


MCQ

1. Which command is used for running jobs in the background?


a) nice
b) ps
c) nohup
d) exec

2. nice command is a ______ command in C shell.


a) internal
b) external
c) built-in
d) directory

3. Jobs scheduled using batch command goes to a special at queue.


a) True
b) False

Introduction to Linux Dr. Poonam Panwar G32,G33


Student Activity

• Create job using at to back up files in one directory to another 10 minutes from now.
• Set up a cron job to backup the files in one directory to another every day at 10 am. Put the
commands in file called mycron.

Introduction to Linux Dr. Poonam Panwar G32,G33


Introduction to Linux (CS-110) Class
On

Bash Shell Scripting

Ms. Ritu Rathee

Department of Computer Science and Engineering


Chitkara University, Punjab
Ms. Ritu Rathee Introduction to Linux CS110 - G25 1
Content

• The Shell’s interpretive cycle,


• Shell variables
• Environmental variables
• Types of Shell
• Changing one shell to other
• Shell builtins

Ms. Ritu Rathee Introduction to Linux CS110 - G25 2


Introduction to Scripting

• Suppose you want to look up a filename, check if the associated file


exists, and then respond accordingly, displaying a message
confirming or not confirming the file's existence.
• If you only need to do it once, you can just type a sequence of
commands at a terminal. However, if you need to do this multiple
times, automation is the way to go.
• In order to automate sets of commands, you will need to learn how
to write shell scripts, the most common of which are used with bash.

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 3
Bash Shell
• A shell is a command line interpreter which provides the user interface
for terminal windows. It can also be used to run scripts, even in non-
interactive sessions without a terminal window, as if the commands were
being directly typed in.
• For example, typing: find . -name "*.c" -ls at the command line
accomplishes the same thing as executing a script file containing the
lines:
#!/bin/bash
find . -name "*.c" –ls
• The #!/bin/bash in the first line should be recognized by anyone who
has developed any kind of script in UNIX environments. The first line of
the script, that starts with #!, contains the full path of the command
interpreter (in this case /bin/bash) that is to be used on the file.

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 4
Features of Shell Scripts

Ms. Ritu Rathee Introduction to Linux CS110 - G25 5


Command Shell Choices
• The command interpreter is tasked with executing statements.
commonly used interpreters
include: /usr/bin/perl, /bin/bash, /bin/csh, /usr/bin/python and /bin/sh.
• Typing a long sequence of commands at a terminal window can be
complicated, time consuming, and error prone. By deploying shell scripts,
using the command-line becomes an efficient and quick way to launch
complex sequences of steps.
• Linux provides a wide choice of shells; exactly what is available on the
system is listed in /etc/shells. Typical choices are:
• /bin/sh /bin/bash /bin/tcsh
/bin/csh /bin/ksh

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 6
Changing Shell

• A user can change their own shell to any thing: which, however must be
listed in the /etc/shells file.
• Only root can run a shell not listed in /etc/shells file.
• If an account has a restricted login shell, then only root can change that
user’s shell.
usermod --shell /bin/bash username
chsh --shell /bin/sh username
vi /etc/passwd

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 7
Basic Scripts
• Let's write a simple bash script that displays a one line message on the
screen. Either type
• $ cat > hello.sh
#!/bin/bash
echo "Hello Linux Foundation Student"

and press ENTER and CTRL-D to save the file, or just create hello.sh in
your favorite text editor. Then, type chmod +x hello.sh to make the file
executable by all users.
$ ./hello.sh or $ bash hello.sh
Hello Linux Foundation Student
• Note if you use the second form, you do not have to make the file
executable.

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 8
• How to write a script
• Editor filename.sh --- > gedit demo.sh -- > editor window
• #!/bin/bash
• Echo “ first script in linux”
• Echo “the value of var is $var”
• Save
• ,.,,,,,,,,,, Terminal window
• Var = 5

Dr. Jatin Arora Introduction to Linux CS110 - G25 9


Interactive bash Scripts

• The user will be prompted to enter a value, which is then displayed


on the screen. The value is stored in a temporary variable, name. We
can reference the value of a shell variable by using a $ in front of the
variable name, such as $name. To create this script, you need to
create a file named getname.sh in your favorite editor with the
following content:
• #!/bin/bash
# Interactive reading of a variable
echo "ENTER YOUR NAME"
read name
# Display variable input
echo The name given was :$name
• Once again, make it executable by doing chmod +x getname.sh.

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 10
Script Return Value
All shell scripts generate a return value upon finishing execution, the
value can be set with the exit statement. Return values permit a process
to monitor the exit state of another process, often in a parent-child
relationship. This helps to determine how this process terminated and
take any appropriate steps necessary, contingent on success or failure.

An easy way to demonstrate success and failure completion is to


execute ls on a file that exists and one that does not, the return value is
stored in the environment variable represented by $?:
$ ls /etc/logrotate.conf
/etc/logrotate.conf
$ echo $?
Ms. Ritu Rathee
Introduction to Linux CS110 - G25 11
Using Exit Codes Within a Script
[user@host bin]$ cat hello
#!/bin/bash
echo "Hello, world"
exit 0

[user@host bin]$ ./hello


Hello, world

[user@host bin]$ echo $?


0

Testing Script Inputs

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 12
Variables in Shell Script
• Legal Rules of Naming Variables in Bash
• The variable name must be in the upper case as it is considered good practice in
bash scripting.
• Insert the dollar sign “$” before the variable name.
• Don’t use spaces after the initialization of the variable name and its value.
• A variable name can have letter/s.
• A variable name can have numbers, underscores, and digits.
• Illegal Rules of Name Variables in Bash
• The variable name having lower case letters.
• No dollar sign “$” inserted while printing it.
• Adding spaces after the initialization of the variable name and its value.
• Start the variable name with number, digit, or special symbols.
• The variable name having space in it.
• Use of keywords to name the variables, e.g., if, else, for, while, int, float, etc.
Ms. Ritu Rathee
Introduction to Linux CS110 - G25 13
NAME=“John"
readonly NAME

unset variable_name
Display all the System variable by running the command:env

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 14
Dr. Jatin Arora Introduction to Linux CS110 - G25 15
‘set alias‘ for any command and the alias command will work fine on
the interactive shell, whereas aliasing doesn’t work inside the script.
# alias ls1='ls -lrt'
# ls1
total 0
-rw-r--r-- 1 root root 0 Oct 12 12:14 file1
-rw-r--r-- 1 root root 0 Oct 12 12:14 file2

# cat script.sh
#!/bin/bash
# Script to check the alias output
alias ls1='ls -lrt'
ls1
Ms. Ritu Rathee
Introduction to Linux CS110 - G25 16
Alias Example

• alias name="value“
• unalias [alias name]
• -p option : This option prints all the defined aliases is resuable
format.Syntax:
alias –p
• alias CD="cd Desktop"

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 17
EXPR in shell script

• expr is a command line Unix utility which evaluates an expression


and outputs the corresponding value. expr evaluates integer or string
expressions, including pattern matching regular expressions. Most of
the challenge posed in writing expressions is preventing the
invoking command line shell from acting on characters intended for
expr to process. The operators available for integers: addition,
subtraction, multiplication, division and modulus for strings: find
regular expression, find a set of characters in a string; in some
versions: find substring, length of string for either: comparison
(equal, not equal, less than, etc.)

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 18
Example
a=7
b=10
expr $a + $b // output: 17
c=`expr $a – 4` //result 3 is stored in variable ‘c’
echo `expr $b % $a` //remainder 3 is printed

Note: Notice space on either side of the ‘+’ operator .


1. To access the value of value of ‘a’ and ‘b’ variable use ‘$’ symbol as prefix to variables.
2. Use back tick ( ` ) for command substitution i.e using one command in the other. (Back
tick is found above the tab key on your PC keyboard)
3. Supported arithmetic operators are +, - , * , /, %
4. The operator ‘*’ must be preceded by a ‘\’backslash character else it will be considered
as a wildcard character.
expr $a \* $b
5. Using brackets ( ) in expr command gives an error.

Dr. Jatin Arora Introduction to Linux CS110 - G25 19


Alternative Way of arithmetic expression

• Another way to calculate the arithmetic expression is to enclose


the expression in $((…))
• This way of evaluation let us make use of brackets as in normal
mathematics
• Here there is no space on either side of the operator
• To access the value of variables no need of using ‘$’ sign separately
again and again for each variable
• ‘*’ operator does not need to be preceded with backslash
• Double brackets preceded with ‘$’ symbol are must i.e $((…)) .

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 20
Test Command
• Like all commands, the test command produces an exit code upon
completion, which is stored as the value $?. To see the conclusion of a
test, display the value of $? immediately following the execution of the
test command. Again, an exit status value of 0 indicates the test
succeeded, and nonzero values indicate the test failed.
• Tests are performed using a variety of operators. Operators can be used
to determine if a number is greater than, greater than or equal to, less
than, less than or equal to, or equal to another number. They can be used
to test if a string of text is the same or not the same as another string of
text. Operators can also be used to evaluate if a variable has a value or
not.
[user@host ~]$ test 1 -gt 0 ; echo $?
0
[user@host ~]$ test 0 -gt 1 ; echo $?
1

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 21
Test Expression

1. Tests can be performed using the Bash test command syntax,


[ <TESTEXPRESSION> ].
2. They can also be performed using Bash's newer extended test
command syntax,
[[ <TESTEXPRESSION> ]], which has been available since Bash
version 2.02 and provides features such as glob pattern matching
and regex pattern matching.

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 22
Test Command Expression
The following examples demonstrate the use of Bash's test command
syntax and Bash's numeric comparison operators.
[user@host ~]$ [ 1 -eq 1 ]; echo $?
0
[user@host ~]$ [ 1 -ne 1 ]; echo $?
1
[user@host ~]$ [ 8 -gt 2 ]; echo $?
0
[user@host ~]$ [ 2 -ge 2 ]; echo $?
0
[user@host ~]$ [ 2 -lt 2 ]; echo $?
1
[user@host ~]$ [ 1 -lt 2 ]; echo $?
0

Dr. Jatin Arora Introduction to Linux CS110 - G25 23


Bash's string comparison operators
The following examples demonstrate the use of Bash's string
comparison operators.

[user@host ~]$ [ abc = abc ]; echo $?


0
[user@host ~]$ [ abc == def ]; echo $?
1
[user@host ~]$ [ abc != def ]; echo $?
0
[user@host ~]$ STRING=''; [ -z "$STRING" ]; echo $?
0
[user@host ~]$ STRING='abc'; [ -n "$STRING" ]; echo $?
0

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 24
Quoting Special Characters

• A number of characters and words have special meaning to the Bash


shell. However, occasionally you will want to use these characters
for their literal values, rather than for their special meanings. To do
this, use one of three tools to remove (or escape) the special
meaning: the backslash (\), single quotes (''), or double quotes ("").
• The backslash escape character removes the special meaning of the
single character immediately following it. For example, to display
the literal string # not a comment with the echo command, the # sign
must not be interpreted by Bash as having special meaning. Place
the backslash character in front of the # sign.
[user@host ~]$ echo # not a comment
[user@host ~]$ echo \# not a comment # not a comment

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 25
Quoting multiple Characters
When you need to escape more than one character in a text string, either use
the escape character multiple times or employ single quotes (''). Single
quotes preserve the literal meaning of all characters they enclose. Observe
the escape character and single quotes in action:
[user@host ~]$ echo # not a comment #
[user@host ~]$ echo \# not a comment #
# not a comment
[user@host ~]$ echo \# not a comment \#
# not a comment #
[user@host ~]$ echo '# not a comment #'
# not a comment #
Use double quotation marks to suppress globbing and shell expansion, but still allow
command and variable substitution. Variable substitution is conceptually identical to
command substitution, but may use optional brace syntax.
Use single quotation marks to interpret all text literally. Besides suppressing globbing and
shell expansion, quotations direct the shell to additionally suppress command and variable
substitution. The question mark (?) is a meta-character that also needs protection from
expansion.
Ms. Ritu Rathee
Introduction to Linux CS110 - G25 26
Worked Out Example
[user@host ~]$ var=$(hostname -s); echo $var
host
[user@host ~]$ echo "***** hostname is ${var} *****"
***** hostname is host *****
[user@host ~]$ echo Your username variable is \$USER.
Your username variable is $USER.
[user@host ~]$ echo "Will variable $var evaluate to $(hostname -s)?"
Will variable host evaluate to host?
[user@host ~]$ echo 'Will variable $var evaluate to $(hostname -s)?'
Will variable $var evaluate to $(hostname -s)?
[user@host ~]$ echo "\"Hello, world\""
"Hello, world"
[user@host ~]$ echo '"Hello, world"'
"Hello, world"
Dr. Jatin Arora Introduction to Linux CS110 - G25 27
Conditional Structures
Simple shell scripts represent a collection of commands that are executed from
beginning to end. Conditional structures allow users to incorporate decision making
into shell scripts, so that certain portions of the script are executed only when
certain conditions are met.
Using the if/then Construct
The simplest of the conditional structures in Bash is the if/then construct, which has
the following syntax.
if <CONDITION>;
Then
<STATEMENT> ... <STATEMENT>
fi
With this construct, if a given condition is met, one or more actions are taken. If the given
condition is not met, then no action is taken. The numeric, string, and file tests previously
demonstrated are frequently utilized for testing the conditions in if/then statements. The fi
statement at the end closes the if/then construct. The following code section demonstrates
the use of an if/then construct to start the psacct service if it is not active.

Dr. Jatin Arora Introduction to Linux CS110 - G25 28


Using the if/then/else Construct
The if/then construct can be further expanded so that different sets of
actions can be taken depending on whether a condition is met. This is
accomplished with the if/then/else construct.

if <CONDITION>; then
<STATEMENT>
...
<STATEMENT>
else
<STATEMENT>
...
<STATEMENT>
fi

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 29
Using the if/then/elif/then/else Construct

Lastly, the if/then/else construct can be further expanded to test more than one
condition, executing a different set of actions when a condition is met. The construct
for this is shown in the following example:

if <CONDITION>; then
<STATEMENT>
...
<STATEMENT>
elif <CONDITION>; then
<STATEMENT>
...
<STATEMENT>
else
<STATEMENT>
...
<STATEMENT>
fi
Ms. Ritu Rathee
Introduction to Linux CS110 - G25 30
Case
The case statement saves going through a whole set of if .. then ..
else statements. Its syntax is really quite simple:
#!/bin/sh
echo "Please talk to me ..."
while :
do
read INPUT_STRING
case $INPUT_STRING in
hello)
echo "Hello yourself!"
;;
bye)
echo "See you again!"
break
;;
*)
echo "Sorry, I don't understand"
;;
esac
done
Ms. Ritu Rathee
Introduction to Linux CS110 - G25 31
Using Loops to Iterate Commands
System administrators often encounter repetitive tasks in their day-to-day
activities. Repetitive tasks can take the form of executing an action
multiple times on a target, such as checking a process every minute for 10
minutes to see if it has completed. Task repetition can also take the form of
executing an action once across multiple targets, such as backing up each
database on a system. The for loop is one of the multiple shell looping
constructs offered by Bash, and can be used for task iterations.

Bash's for loop construct uses the following syntax.


for VARIABLE in LIST;
do
COMMAND VARIABLE
done

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 32
[user@host ~]$ for HOST in host1 host2 host3; do echo $HOST; done
host1
host2
host3
[user@host ~]$ for HOST in host{1,2,3}; do echo $HOST; done
host1
host2
host3
[user@host ~]$ for HOST in host{1..3}; do echo $HOST; done
host1
host2
host3
[user@host ~]$ for FILE in file*; do ls $FILE; done
filea
fileb
filec
[user@host ~]$ for FILE in file{a..c}; do ls $FILE; done
filea
fileb
filec

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 33
File Comparison
test -e filename
[ -e filename ]

test -f filename
[ -f filename ]
-b FILE FILE exists and is block special
-c FILE FILE exists and is character special
-d FILE FILE exists and is a directory
-e FILE FILE exists
-f FILE FILE exists and is a regular file
-g FILE FILE exists and is set-group-ID
-G FILE FILE exists and is owned by the effective group ID
-h FILE FILE exists and is a symbolic link (same as -L)
-k FILE FILE exists and has its sticky bit set

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 34
-L FILE FILE exists and is a symbolic link (same as -h)
-O FILE FILE exists and is owned by the effective user ID
-p FILE FILE exists and is a named pipe
-r FILE FILE exists and read permission is granted
-s FILE FILE exists and has a size greater than zero
-S FILE FILE exists and is a socket
-t FD file descriptor FD is opened on a terminal
-u FILE FILE exists and its set-user-ID bit is set
-w FILE FILE exists and write permission is granted
-x FILE FILE exists and execute (or search) permission is granted

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 35
Array Initialization and Usage
• With newer versions of bash, it supports one-dimensional arrays. An
array can be explicitly declared by the declare shell-builtin.
• declare -a var But it is not necessary to declare array variables as
above. We can insert individual elements to array directly as
follows.
• var[XX]=<value> where ‘XX’ denotes the array index. To
dereference array elements use the curly bracket syntax, i.e.
• ${var[XX]}

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 36
Array Indexing

Note: Array indexing always start with 0.


• Another convenient way of initializing an entire array is by using
the pair of parenthesis as shown below.
• var=( element1 element2 element3 . . . elementN ) There is yet
another way of assigning values to arrays. This way of initialization
is a sub-category of the previously explained method.
• array=( [XX]=<value> [XX]=<value> . . . ) We can also read/assign
values to array during the execution time using the read shell-
builtin.
• read -a array

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 37
Array example
To traverse through the array elements we can also use for loop.
for i in “${array[@]}”
do
#access each element as $i. . .
done
The following script summarizes the contents of this particular
section.
#!/bin/bash
array1[0]=one echo ${array2[0]}
array1[1]=1 echo ${array2[2]}
array3=( [9]=nine [11]=11 )
echo ${array1[0]} echo ${array3[9]}
echo ${array1[1]} echo ${array3[11]}
array2=( one two three )

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 38
Practice Time

1. Write a script which:


• Checks for the existence of file and directory
2 Write a script which: prints the factorial of a number
3 Write a script which: is used as calculator

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 39
Quiz Time
1. Which command is used to make some_script.sh executable?
a) chmod a some_script.sh
b) chmod +x some_script.sh
c) ./some_script.sh some_command
d) cat ./somescript.sh
2. Which of the following commands is used to manually pass a text file,
script.sh, to the shell interpreter?
a) bash script.sh
b) script.sh bash
c) ./script.sh
d) .sh ./scrip
t

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 40
• Which command changes anupam's home directory from its default
location to /usr/local directory?
a) usermod -d /usr/local anupam
b) usermod -g /usr/local anupam
c) usermod -l /usr/local anupam
d) usermod -a /usr/local anupam
• Which command and options are used to delete a user’s account and home
directory?
a) userdel –r
b) usermod –f
c) passwd –e
d) usermod -e

Ms. Ritu Rathee


Introduction to Linux CS110 - G25 41
Introduction to Linux (CS-110) Class
On

Bash Shell Scripting

Dr. Jatin Arora

Department of Computer Science and Engineering


Chitkara University, Punjab
Dr. Jatin Arora Introduction to Linux CS110 - G25 1
Content

• Bash Shell Scripting: Syntax,


• Constructs using string
• Simulating an array using eval.

Dr. Jatin Arora Introduction to Linux CS110 - G25 2


String Variable
• A string variable contains a sequence of text characters. It can
include letters, numbers, symbols and punctuation marks. Some
examples: abcde, 123, abcde 123, abcde-123, &acbde=%123
• String operators include those that do comparison, sorting, and
finding the length. The following table demonstrates the use of some
basic string operators:

Operator Meaning

[[ string1 > string2 ]] Compares the sorting order of string1 and string2.

[[ string1 == string2 ]] Compares the characters in string1 with the


characters in string2.

myLen1=${#string1} Saves the length of string1 in the variable myLen1.

Dr. Jatin Arora Introduction to Linux CS110 - G25 3


String Manipulation

Dr. Jatin Arora Introduction to Linux CS110 - G25 4


String Manipulation Cont..

Dr. Jatin Arora Introduction to Linux CS110 - G25 5


Sub String Operations

• To extract the first n characters of a string we can


specify: ${string:0:n}. Here, 0 is the offset in the string (i.e., which
character to begin from) where the extraction needs to start and n is
the number of characters to be extracted.
• To extract all characters in a string after a dot (.), use the following
expression: ${string#*.}

Dr. Jatin Arora Introduction to Linux CS110 - G25 6


Practice Time

1. Write a script which:


– Does ls for a non-existent file, and then displays the resulting exit
status.
– Creates a file and does ls for it, and then once again displays the
resulting exit status.

Dr. Jatin Arora Introduction to Linux CS110 - G25 7


Quiz Time
Select the correct syntax to find the length of the string abc.
a) $#[abc]
b) ${abc#}
c) $#{abc}
d) ${#abc}

Dr. Jatin Arora Introduction to Linux CS110 - G25 8


• Which command changes anupam's home directory from its default
location to /usr/local directory?
a) usermod -d /usr/local anupam
b) usermod -g /usr/local anupam
c) usermod -l /usr/local anupam
d) usermod -a /usr/local anupam
• Which command and options are used to delete a user’s account and home
directory?
a) userdel –r
b) usermod –f
c) passwd –e
d) usermod -e

Dr. Jatin Arora Introduction to Linux CS110 - G25 9


Introduction to Linux (CS-110) Class
On
Command line arguments

Dated:
Department of Computer Science and
Engineering,
Chitkara University, Punjab
Command Line Arguments in Linux Shell Scripting

Command line arguments (also known as positional parameters) are the


arguments specified at the command prompt with a command or script
to be executed. The locations at the command prompt of the arguments
as well as the location of the command, or the script itself, are stored in
corresponding variables. These variables are special shell variables.
Below picture will help you understand them.
Passing command line arguments to shell
script.
how to work with command line
parameters.

While running a command, the user can pass a variable number of parameters
in the command line.
Within the command script, the passed parameters are accessible using
‘positional parameters’. These range from $0 to $9, where $0 refers to the
name of the command itself, and $1 to $9 are the first through to the ninth
parameter, depending on how many parameters were actually passed.

Example:
$ sh hello how to do you do
Here $0 would be assigned sh
$1 would be assigned hello
$2 would be assigned how
And so on …
We will now look at some additional
commands to process these parameters.
Command line arguments cont…
Command line arguments cont…
Command line arguments cont…

• In the shell script, $0 is the name of the command run


(usually the name of the shell script file); $1 is the first
argument, $2 is the second argument, $3 is the third
argument, etc...
• The commands echo $* and echo $@ both print the same
thing, the list of all command line arguments, but “$*” is one
string, and “$@” is a list of separate strings for each
parameter.
• As an example the shell script file is ran with three simple
command line arguments as :
./myscript a b c.
Example Script : ./myscript a b c.
Steps for writing & executing shell
script

• Go to default shell/Terminal
• vi myScript.sh
• vi editor will get opened with file name myScript.sh
• Write the script
• Press -> Escape : wq (to save and exit)
• Execute the programme by typing
□ chmod x ger.sh
□ ./ger.sh
Print all Arguments with script name and
total number of arguments passed in it
Command line arguments: Suppose you are in your home directory which
has sub-directories dir-1, dir-2, dir-3, dir-4 and dir-5. Write a shell script
which uses touch command in it to create files file-i in dir-i for i taking values
from 1 to 5 using command line arguments.

Writing your First Script


#!/bin/bash
for i in $@
do
touch dir-$i/file-$i
done
Introduction to Linux (CS-110) Class
On
Advanced Shell Programming

Dated:
Department of Computer Science and
Engineering,
Chitkara University, Punjab

8/17/2021 Vivek Bhardwaj 1


Basics of Shell Programming

To see what shell you have, run: echo $SHELL.

In Linux, the dollar sign ($) stands for a shell variable.

The ‘echo‘ command just returns whatever you type in.

#!/bin/bash – It is called shebang. It is written at the top of a shell script and it


passes the instruction to the program /bin/bash.

8/17/2021 Vivek Bhardwaj 2


About shell Script

Shell script is just a simple text file with “.sh” extension, having executable
permission.
1
Process of writing and executing a script
Open terminal. Navigate to the place where you want to create script
using ‘cd‘ command.
2 touch hello.sh (Here we named the script as hello, remember the ‘.sh‘
extension is compulsory).
3 vi hello.sh (gedit hello.sh) [You can use your favourite editor, to edit the
script]. chmod 777 hello.sh (making the script executable).
4 bash hello.sh or ./hello.sh (running the script)

8/17/2021 Vivek Bhardwaj 3


Writing Your own shell script

1 Writing your First Script


#!/bin/bash
# My first script
echo "Hello World!"
Writing your Second Script
2
#! /bin/bash
echo "Hey what's Your First Name?";
read a;
echo "welcome Mr./Mrs. $a, would you like to tell us, Your
Last Name";
read b;
echo "Thanks Mr./Mrs. $a $b for telling us your name";
echo "*******************"
echo "Mr./Mrs. $b, it's time to say you good bye"

8/17/2021 Vivek Bhardwaj 4


Quotes in Linux shell

Quotes Name Meaning

"Double Quotes" - Anything enclose in double quotes remove meaning of that characters
(except \ and $).
Double
"
Quotes eg a=10
echo “Value of a is $a”
output is Value of a is 10
'Single quotes' - Single quotes won’t interpolate anything

' Single quotes eg a=10


echo ‘Value of a is $a’
output is Value of a is $a

` Back quote `Back quote` - To execute command

~$echo “Present Working directory is `pwd`”

8/17/2021 Vivek Bhardwaj 5


Math in Shell Scripts

Shell script variables are by default treated as strings, not numbers


1. declare
The following example illustrates that a declared integer is not
treated as a string.

$ n=6/3
$ echo $n
6/3
$ declare -i n
$ n=6/3
$ echo $n
2

When you do not need the declare statement is when you will use a
When
program or you do not
built-in command needa math
to evaluate the declare statement is when you w
statement.
command to evaluate a math statement.

8/17/2021 Vivek Bhardwaj 6


Math in Shell Scripts

2. expr
• An old Unix program that can evaluate math is expr. expr became popular in the days
of the Bourne Shell, which did not support math. With Bash and Korn shell, it is
generally not needed.
• Spaces Plays important role expr 20 %3 - Remainder read as 20 mod 3 and remainder is 2.
• Need of dollar in front of variable expr 10 \* 3 - Multiplication use \* and not * since its wild
card.
$ z=5
$ z=`expr $z+1` ---- Need spaces around + sign.
$ echo $z
5+1
$ z=`expr $z + 1`
$ echo $z
6

8/17/2021 Vivek Bhardwaj 7


Math in Shell Scripts

3. let
• It is also a little picky about spaces, but it wants the opposite of what expr
wanted.
• No need of $ in front of variables to be read.

$ let z=5
$ echo $z
5
$ let z=$z+1
$ echo $z
6
$ let z=$z + 1 # --- Spaces around + sign are bad with let
-bash: let: +: syntax error: operand expected (error token is "+")

$let z=z+1 # --- look Mom, no $ to read a variable.


$echo $z
7

8/17/2021 Vivek Bhardwaj 8


Math in Shell Scripts

3. BASH Arithmetic (())


With the BASH shell, whole arithmetic expressions may be placed inside double
parenthesis. This form is more forgiving about spaces.

$ ((e=5))
$ echo $e
5
$ (( e = e + 3 ))
$ echo $e
8
$ (( e=e+4 )) # -- spaces or no spaces, it doesn't matter
$ echo $e
12

8/17/2021 Vivek Bhardwaj 9


Eg of Math in shell script

• Create a script file for eg math.sh


• Open this file using any editor and start typing your script

8/17/2021 Vivek Bhardwaj 10


Bash Calculator bc

• What if you want to do math with floating point numbers or you have some fairly
complicated math to do?
• Neither form of let, supports floating point numbers.
• Then bc command is needed.
• But you have to treat the variables as strings.
$r=3.5
$s=`echo "$r + 2.2" | bc`
$echo $s a=10.10
5.7 b=20
$ z = `echo $z + 1 | bc` echo addition of two numbers using expr
-bash: z: command not found c=`echo $a / $b | bc -l`
# -- spaces around = sign are bad
(shell thing, not bc) echo expr sum is $c
$ z=`echo "$z + 1" | bc`
$ echo $z
8
$ z=`echo "$z+1" | bc` -- spaces don't matter with bc
$ echo $z
9

8/17/2021 Vivek Bhardwaj 11


Operators

1. Arithmetic Operators

2. Relational Operators

3. Boolean Operators

4.String Operators

5. File Test Operators

8/17/2021 Vivek Bhardwaj 12


Operators - Arithmetic Operators

The following arithmetic operators are supported by Bourne Shell.


It is very important to understand that all the conditional expressions should be inside square braces with spaces
around them, for example [ $a == $b ] is correct whereas, [$a==$b] is incorrect.
Assume variable a holds 10 and variable b holds 20 then −

8/17/2021 Vivek Bhardwaj 13


Operators - Relational Operators

Bourne Shell supports the following relational operators that are specific to numeric values. These operators do not work for
string values unless their value is numeric.
For example, following operators will work to check a relation between 10 and 20 as well as in between "10" and "20" but not in
between "ten" and "twenty".
Assume variable a holds 10 and variable b holds 20 then −

8/17/2021 Vivek Bhardwaj 14


Operators - String Operators

Assume variable a holds "abc" and variable b holds "efg" then −

Operator Description Example


= Checks if the value of two operands are equal or not; if yes, [ $a = $b ] is not true.
then the condition becomes true.

!= Checks if the value of two operands are equal or not; if [ $a != $b ] is true.


values are not equal then the condition becomes true.

-z Checks if the given string operand size is zero; if it is zero [ -z $a ] is not true.
length, then it returns true.

-n Checks if the given string operand size is non-zero; if it is [ -n $a ] is not false.


nonzero length, then it returns true.

str Checks if str is not the empty string; if it is empty, then it [ $a ] is not false.
returns false.

8/17/2021 Vivek Bhardwaj 15


Operators - Boolean Operators

Assume variable a holds 10 and variable b holds 20 then −

Operator Description Example


! This is logical negation. This inverts
a true condition into false and vice [ ! false ] is true.
versa.

-o This is logical OR. If one of the [ $a -lt 20 -o $b -gt


operands is true, then the condition 100 ] is true.
becomes true.

-a This is logical AND. If both the


operands are true, then the [ $a -lt 20 -a $b -gt
condition becomes true otherwise 100 ] is false.
false.

8/17/2021 Vivek Bhardwaj 16


Operators - File Test Operators

We have a few operators that can be used to test various properties associated with a Unix file.
Assume a variable file holds an existing file name "test" the size of which is 100 bytes and
has read, write and execute permission on −

8/17/2021 Vivek Bhardwaj 17


Shell Programming Assignment

Write a Shell program to find the largest among three numbers.

PROGRAM

echo “Enter the first number:” read a

echo “Enter the second number:” read b

echo “Enter the third number:” read c

if [ $a -gt $b -a $a -gt $c ] then

echo “$s is greater” elif [ $b -gt $c ]

then

echo “$b is greater”

else

echo “$c is greater”

fi

8/17/2021 Vivek Bhardwaj 18


Shell script to find factorial of a number

A shell script to find the factorial of a number A shell script to find the factorial of a number
Using for loop Using while loop

#!/bin/bash
# A shell script to find the factorial of a number

read -p "Enter a number" num


fact=1
while [ $num -gt 1 ]
do
(( fact=fact*num))
((num=num-1))
done
echo $fact

8/17/2021 Vivek Bhardwaj 19


Bash program to check if the Number is a Palindrome
#! /bin/bash
echo “Enter a number:” Second Logic
read n while [ $num -gt 0 ]
t=$n do
s=0 # Get Remainder
while [ $n -gt 0 ] s=$(( $num % 10 ))
do
# Get Remainder # Get next digit
((r=n % 10)) num=$(( $num / 10 ))
((s=r + s * 10))
((n=n / 10)) # Store previous number and
done # current digit in reverse
if [ $s -eq $t ] rev=$( echo ${rev}${s} )
then done
echo “The given number and its reverse are same”
else
echo “The given number and its reverse are not same”
fi

8/17/2021 Vivek Bhardwaj 20


Shell Programming Assignment

Write a Shell program to find the largest among ‘n’ different numbers.
PROGRAM

Write a Shell program to find the sum of odd and even numbers from a
set of numbers.

8/17/2021 Vivek Bhardwaj 21


Introduction to Linux (CS-110) Class
On

Basic Math Operations

Ms Sangeetha Annam
Department of Computer Science and Engineering
Chitkara University, Punjab
Ms Sangeetha Annam ITL CS110 - G27 1
Arithmetic Expansion in Bash Shell

Arithmetic expansion and evaluation is done by placing an integer expression


using the following format:
$((expression)
$((n1+n2))
$((n1/n2))
$((n1-n2))

Ms Sangeetha Annam ITL-CS110 G27


Mathematical Operators With Integers

Operator Description Example Evaluates To


+ Addition echo $(( 20 + 5 )) 25
- Subtraction echo $(( 20 - 5 )) 15
/ Division echo $(( 20 / 5 )) 4
* Multiplication echo $(( 20 * 5 )) 100
% Modulus echo $(( 20 % 3 )) 2
x=5
post-increment (add 5
++ echo $(( x++ ))
variable value by 1) 6
echo $(( x++ ))

post-decrement
x=5
-- (subtract variable 4
echo $(( x-- ))
value by 1)

x=2
** Exponentiation y=3 8
echo $(( x ** y ))

Ms Sangeetha Annam ITL-CS110 G27


The gawk Mathematical
Functions

Function Description
atan2(x, y) - The arctangent of x / y, with x and y specified in radians.
cos(x) - The cosine of x, with x specified in radians.
exp(x) - The exponential of x.
int(x) - The integer part of x, truncated toward 0.
log(x) - The natural logarithm of x.
rand() - A random floating point value larger than 0 and less than 1.
sin(x) - The sine of x, with x specified in radians.
sqrt(x) - The square root of x.
srand(x) -Specify a seed value for calculating random numbers.

Ms Sangeetha Annam ITL-CS110 G27


Rand function

• The rand() function is great for creating random numbers.


• The rand() function returns a random number, but only between the
values 0 and 1 (not including 0 or 1).
• A common method for producing larger integer random numbers is to create
an algorithm that uses the rand() function, along with the int() function:
x = int(10 * rand())
This returns a random integer value between (and including) 0 and 9.

Ms Sangeetha Annam ITL-CS110 G27


Using ‘expr’ command
Create a bash file and add the various ‘expr’ commands.
#!/bin/bash

# Works as string
expr '10 + 30'
# Works as string
expr 10+30
#Perform the addition
expr 10 + 30
#Find out the remainder value
expr 30 % 9
#Using expr with backtick
myVal1=`expr 30 / 10`
echo $myVal1
#Using expr within command substitute
myVal2=$( expr 30 - 10 )
echo $myVal2

Ms Sangeetha Annam ITL-CS110 G27


Using ‘let’ command
‘let’ is another built-in command to do arithmetic operations in bash. ‘let’ command
can’t print the output to the terminal without storing the value in a variable.
But ‘let’ command can be used to remove the other limitations of
‘expr’ command.
#!/bin/bash
the
# Multiplying 9 by 8
let val1=9*3
echo $val1

# Dividing 8 by 3
let "val2 = 8 / 3"
echo $val2

# Subtracting 3 from 9
let val3=9-3
echo $val3

# Applying increment
let val4=7
let val4++
echo $val4

# Using argument value in arithmetic operation


let "val5=50+$1"
echo $val5
Ms Sangeetha Annam ITL-CS110 G27
Using double brackets
Double brackets are used to do the arithmetic tasks and using double bracket for
executing mathematical expressions is more flexible than commands
like ‘expr’ or ‘let’.
#!/bin/bash

# Calculate the mathematical expression


val1=$((10*5+15))
echo $val1
# Using post or pre increment/decrement operator
((val1++))
echo $val1
val2=41
((--val2))
echo $val2
# Using shorthand operator
(( val2 += 60 ))
echo $val2
# Dividing 40 by 6
(( val3 = 40/6 ))
echo $val3
Ms Sangeetha Annam ITL-CS110 G27
Using ‘bc’ command for float or
double numbers
• One of the major limitations of the above ways of doing arithmetic operations
in bash is that ‘expr’ or ‘let’ or double brackets expression are not able to
produce floating point or double numbers.

• The output of division operations of the above examples are


integers. ‘bc’ command can be used to solve this problem and it works as a
basic calculator for the Linux operating system.

• Create a bash file and add the following code to check the use
of ‘bc’ command in arithmetic operations.

Ms Sangeetha Annam ITL-CS110 G27


Examples

#!/bin/bash

# Dividing 55 by 3 with bc only


echo "55/3" | bc

# Dividing 55 by 3 with bc and -l option


echo "55/3" | bc -l

# Dividing 55 by 3 with bc and scale value


echo "scale=2; 55/3" | bc

Ms Sangeetha Annam ITL-CS110 G27


MCQ
Q1. Which built-in command performs integer arithmetic in bash shell?
a. Let
b. Get
c. Set
d. None of the above

Q2. Which expression use the value of the enclosed arithmetic expression?
a. $(())
b. $()
c. ${}
d. $[]

Q3. If a and b are 2 variables then the meaning of a<<=b is


a. b = a<<b
b. a = a<<b
c. b = b<<a
d. a = a<<b

Ms Sangeetha Annam ITL-CS110 G27


MCQ…
Q4. What is the output of this program?
#!/bin/bash
a=10
b=$(($a<0&&a<100))
echo $b
exit 0

a. 10
b. 0
c. 1
d. None of the above

Q5. Which of the following can be operands of arithmetic operators?


a. Numeric
b. Boolean
c. Characters
d. Both Numeric & Characters

Ms Sangeetha Annam ITL-CS110 G27


THANK YOU

Ms Sangeetha Annam ITL-CS110 G27

You might also like