0% found this document useful (0 votes)
21 views126 pages

Understanding DevOps: Key Concepts & Benefits

DevOps is a collaborative software development approach that integrates development and operations teams to enhance the software delivery lifecycle through automation and continuous delivery. It aims to improve speed, quality, and customer satisfaction while reducing costs and errors, contrasting with traditional IT practices. The DevOps lifecycle includes phases such as planning, coding, testing, and monitoring, and emphasizes continuous improvement and feedback to adapt to changing business needs.
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)
21 views126 pages

Understanding DevOps: Key Concepts & Benefits

DevOps is a collaborative software development approach that integrates development and operations teams to enhance the software delivery lifecycle through automation and continuous delivery. It aims to improve speed, quality, and customer satisfaction while reducing costs and errors, contrasting with traditional IT practices. The DevOps lifecycle includes phases such as planning, coding, testing, and monitoring, and emphasizes continuous improvement and feedback to adapt to changing business needs.
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

DevOps is a collection of two words, “Development” and “Operations,” representing a cultural

approach that emphasizes collaboration between development and operations teams to


streamline the entire software delivery lifecycle.
What is DevOps?
DevOps is a software development approach emphasizing collaboration, automation, and
continuous delivery to provide high-quality products to customers quickly and efficiently. DevOps
breaks down silos between development and operations teams to enable seamless
communication, faster time-to-market, and improved customer satisfaction. It allows a team to
handle the complete application lifecycle, from development to testing, operations, and
deployment. It shows cooperation between Development and Operations groups to deploy code
to production quickly in an automated and repeatable manner.

Every phase of the software development lifecycle, including planning, coding, testing,
deployment, and monitoring, is heavily automated in DevOps. This improves productivity,
ensures consistency, and lowers error rates in the development process. A culture of continuous
improvement is also promoted by DevOps, where feedback loops are incorporated into the
procedure to facilitate quicker iteration and better decision-making. Organizations can increase
their agility, lower costs, and speed up innovation by adopting DevOps.

Why DevOps?
The goal of DevOps is to increase an organization’s speed when it comes to delivering
applications and services. Many companies have successfully implemented DevOps to enhance
their user experience including Amazon, Netflix, etc.

Facebook’s mobile app which is updated every two weeks effectively tells users you can have
what you want and you can have it. Now ever wondered how Facebook was able to do social
smoothing? It’s the DevOps philosophy that helps Facebook ensure that apps aren’t outdated
and that users get the best experience on Facebook. Facebook accomplishes this true code
ownership model that makes its developers responsible that includes testing and supporting
through production and delivery for each kernel of code. They write and update their true
policies like this but Facebook has developed a DevOps culture and has successfully
accelerated its development lifecycle.

Industries have started to gear up for digital transformation by shifting their means to weeks and
months instead of years while maintaining high quality as a result. The solution to all this is-
DevOps.
How DevOps is different from Traditional IT?
Traditional IT has 1000s lines of code and is created by different teams with different standards
whereas DevOps is created by one team with intimate knowledge of the product. Traditional IT
is complex to understand and DevOps is easily understandable.

DevOps Lifecycle
DevOps lifecycle is the methodology where professional development teams come together to
bring products to market more efficiently and quickly. The structure of the DevOps lifecycle
consists of Plan, Code, Building, Test, Releasing, Deploying, Operating, and Monitoring.
Plan: Determining the commercial needs and gathering the opinions of end-user by
professionals in this level of the DevOps lifecycle.
Code: At this level, the code for the same is developed and in order to simplify the design, the
team of developers uses tools and extensions that take care of security problems.
Build: After the coding part, programmers use various tools for the submission of the code to
the common code source.
Test: This level is very important to assure software integrity. Various sorts of tests are done
such as user acceptability testing, safety testing, speed testing, and many more.
Release: At this level, everything is ready to be deployed in the operational environment.
Deploy: In this level, Infrastructure-as-Code assists in creating the operational infrastructure
and subsequently publishes the build using various DevOps lifecycle tools.
Operate: At this level, the available version is ready for users to use. Here, the department
looks after the server configuration and deployment.
Monitor: The observation is done at this level that depends on the data which is gathered from
consumer behavior, the efficiency of applications, and from various other sources.
Best Practices to follow:
Implement automated dashboard
Keep the entire team together
Allow DevOps to be a cultural change
Be patient with the developers
Maintain a centralized unit
Build a flexible infrastructure
Advantages:
Faster Delivery: DevOps enables organizations to release new products and updates faster and
more frequently, which can lead to a competitive advantage.
Improved Collaboration: DevOps promotes collaboration between development and operations
teams, resulting in better communication, increased efficiency, and reduced friction.
Improved Quality: DevOps emphasizes automated testing and continuous integration, which
helps to catch bugs early in the development process and improve the overall quality of
software.
Increased Automation: DevOps enables organizations to automate many manual processes,
freeing up time for more strategic work and reducing the risk of human error.
Better Scalability: DevOps enables organizations to quickly and efficiently scale their
infrastructure to meet changing demands, improving the ability to respond to business needs.
Increased Customer Satisfaction: DevOps helps organizations to deliver new features and
updates more quickly, which can result in increased customer satisfaction and loyalty.
Improved Security: DevOps promotes security best practices, such as continuous testing and
monitoring, which can help to reduce the risk of security breaches and improve the overall
security of an organization’s systems.
Better Resource Utilization: DevOps enables organizations to optimize their use of resources,
including hardware, software, and personnel, which can result in cost savings and improved
efficiency.
Disadvantages:
High Initial Investment: Implementing DevOps can be a complex and costly process, requiring
significant investment in technology, infrastructure, and personnel.
Skills Shortage: Finding qualified DevOps professionals can be a challenge, and organizations
may need to invest in training and development programs to build the necessary skills within
their teams.
Resistance to Change: Some employees may resist the cultural and organizational changes
required for successful DevOps adoption, which can result in resistance, resistance to
collaboration, and reduced efficiency.
Lack of Standardization: DevOps is still a relatively new field, and there is a lack of
standardization in terms of methodologies, tools, and processes. This can make it difficult for
organizations to determine the best approach for their specific needs.
Increased Complexity: DevOps can increase the complexity of software delivery, requiring
organizations to manage a larger number of moving parts and integrate multiple systems and
tools.
Dependency on Technology: DevOps relies heavily on technology, and organizations may need
to invest in a variety of tools and platforms to support the DevOps process.
Need for Continuous Improvement: DevOps requires ongoing improvement and adaptation, as
new technologies and best practices emerge. Organizations must be prepared to continuously
adapt and evolve their DevOps practices to remain competitive.
DevOps Lifecycle is the set of phases that includes DevOps for taking part in Development and
Operation group duties for quicker software program delivery. DevOps follows positive
techniques that consist of code, building, testing, releasing, deploying, operating, displaying,
and planning. DevOps lifecycle follows a range of phases such as non-stop development, non-
stop integration, non-stop testing, non-stop monitoring, and non-stop feedback. Each segment
of the DevOps lifecycle is related to some equipment and applied sciences to obtain the
process. Some of the frequently used tools are open source and are carried out primarily based
on commercial enterprise requirements. DevOps lifecycle is effortless to manipulate and it helps
satisfactory delivery.

7 Cs of DevOps
Continuous Development
Continuous Integration
Continuous Testing
Continuous Deployment/Continuous Delivery
Continuous Monitoring
Continuous Feedback
Continuous Operations

1. Continuous Development
In Continuous Development code is written in small, continuous bits rather than all at once,
Continuous Development is important in DevOps because this improves efficiency every time a
piece of code is created, it is tested, built, and deployed into production. Continuous
Development raises the standard of the code and streamlines the process of repairing flaws,
vulnerabilities, and defects. It facilitates developers’ ability to concentrate on creating high-
quality code.

2. Continuous Integration
Continuous Integration can be explained mainly in 4 stages in DevOps. They are as follows:
Getting the SourceCode from SCM
Building the code
Code quality review
Storing the build artifacts
The stages mentioned above are the flow of Continuous Integration and we can use any of the
tools that suit our requirement in each stage and of the most popular tools are GitHub for source
code management(SCM) when the developer develops the code on his local machine he
pushes it to the remote repository which is GitHub from here who is having the access can Pull,
clone and can make required changes to the code. From there by using Maven we can build
them into the required package (war, jar, ear) and can test the Junit [Link] performs
code quality reviews where it will measure the quality of source code and generates a report in
the form of HTML or PDF format. Nexus for storing the build artifacts will help us to store the
artifacts that are build by using Maven and this whole process is achieved by using a
Continuous Integration tool Jenkins.
3. Continuous Testing
Any firm can deploy continuous testing with the use of the agile and DevOps methodologies.
Depending on our needs, we can perform continuous testing using automation testing tools
such as Testsigma, Selenium, LambdaTest, etc. With these tools, we can test our code and
prevent problems and code smells, as well as test more quickly and intelligently. With the aid of
a continuous integration platform like Jenkins, the entire process can be automated, which is
another added benefit.

4. Continuous Deployment/ Continuous Delivery


Continuous Deployment: Continuous Deployment is the process of automatically deploying an
application into the production environment when it has completed testing and the build stages.
Here, we’ll automate everything from obtaining the application’s source code to deploying it.

Continuous Delivery: Continuous Delivery is the process of deploying an application into


production servers manually when it has completed testing and the build stages. Here, we’ll
automate the continuous integration processes, however, manual involvement is still required
for deploying it to the production environment.

5. Continuous Monitoring
DevOps lifecycle is incomplete if there was no Continuous Monitoring. Continuous Monitoring
can be achieved with the help of Prometheus and Grafana we can continuously monitor and can
get notified before anything goes wrong with the help of Prometheus we can gather many
performance measures, including CPU and memory utilization, network traffic, application
response times, error rates, and others. Grafana makes it possible to visually represent and
keep track of data from time series, such as CPU and memory utilization.

6. Continuous Feedback
Once the application is released into the market the end users will use the application and they
will give us feedback about the performance of the application and any glitches affecting the
user experience after getting multiple feedback from the end users’ the DevOps team will
analyze the feedbacks given by end users and they will reach out to the developer team tries to
rectify the mistakes they are performed in that piece of code by this we can reduce the errors or
bugs that which we are currently developing and can produce much more effective results for
the end users also we reduce any unnecessary steps to deploy the application. Continuous
Feedback can increase the performance of the application and reduce bugs in the code making
it smooth for end users to use the application.

7. Continuous Operations
We will sustain the higher application uptime by implementing continuous operation, which will
assist us to cut down on the maintenance downtime that will negatively impact end users’
experiences. More output, lower manufacturing costs, and better quality control are benefits of
continuous operations.

Different Phases of the DevOps Lifecycle


Plan: Professionals determine the commercial need and gather end-user opinions throughout
this level. In this step, they design a project plan to optimize business impact and produce the
intended result.
Code – During this point, the code is being developed. To simplify the design process, the
developer team employs lifecycle DevOps tools and extensions like Git that assist them in
preventing safety problems and bad coding standards.
Build – After programmers have completed their tasks, they use tools such as Maven and
Gradle to submit the code to the common code source.
Test – To assure software integrity, the product is first delivered to the test platform to execute
various sorts of screening such as user acceptability testing, safety testing, integration checking,
speed testing, and so on, utilizing tools such as JUnit, Selenium, etc.
Release – At this point, the build is prepared to be deployed in the operational environment. The
DevOps department prepares updates or sends several versions to production when the build
satisfies all checks based on the organizational demands.
Deploy – At this point, Infrastructure-as-Code assists in creating the operational infrastructure
and subsequently publishes the build using various DevOps lifecycle tools.
Operate – This version is now convenient for users to utilize. With tools including Chef, the
management department take care of server configuration and deployment at this point.
Monitor – The DevOps workflow is observed at this level depending on data gathered from
consumer behavior, application efficiency, and other sources. The ability to observe the
complete surroundings aids teams in identifying bottlenecks affecting the production and
operations teams’ performance.

The Evolution of DevOps – 3 Major Trends for Future


DevOps originated in the mid-2000s when organizations started to recognize the need for a
more efficient and effective way of delivering software. The traditional approach to software
development, where development and operations were separate departments, was proving to
be too slow and cumbersome. The siloed nature of this approach led to poor communication
and collaboration, resulting in long lead times, slow feedback cycles, and low-quality software.
In response to these challenges, a new approach was needed, and DevOps was born. The goal
of DevOps was to bring together software development and operations to work collaboratively,
improving communication, collaboration, and integration. The focus was on automating manual
processes, reducing lead times, and increasing the speed of delivery.
Early Adoption and Growth of DevOps
The early adopters of DevOps were organizations in the tech industry, particularly web-based
companies. These organizations faced unique challenges in delivering software, such as
frequent releases, short development cycles, and a large number of users. They saw the
benefits of DevOps and were quick to adopt it to improve their software delivery process.
One of the first organizations to embrace DevOps was Amazon, which implemented the
approach in the early 2000s. Amazon’s DevOps team was responsible for automating manual
processes and reducing the lead time for software delivery. This resulted in faster feedback
cycles and improved software quality, which helped Amazon stay ahead of the competition.
As the benefits of DevOps became more apparent, more and more organizations started to
adopt it. DevOps became a key differentiator for organizations, and those that embraced it were
able to deliver software faster, with higher quality, and at a lower cost. The adoption of DevOps
was also driven by the rise of cloud computing, which provided the infrastructure and tools
needed to support DevOps practices.
In the early 2010s, DevOps started to gain mainstream popularity, and its adoption accelerated.
The rise of DevOps was also driven by the increasing importance of software in all aspects of
business, from product development to customer engagement. Organizations realized that they
needed to adopt DevOps to remain competitive, and the demand for DevOps skills and
expertise skyrocketed.
Over recent years, the DevOps wave has swept through the entire software development world
unhindered. This revolutionary approach has proved itself what is indeed needed for businesses
to scale development faster and more efficiently. The year 2020, with its main feature being a
global pandemic, has appeared to be a turning point for DevOps, one that will determine the
trajectory of software development for years to come, beginning from the next, 2021.
This article explores how COVID-19, the shift to the cloud, and its implications for security are
driving a new wave of demands for software development in the near future, from a DevOps
perspective.
1. Cloud-Native Technology in Production
As a Forrester report notes, the cloud takes center stage in pandemic recovery efforts, enabling
millions of workers to work from home, maintaining supply chains globally, and rapidly
transforming business models. In 2021, it estimates that the global public cloud infrastructure
market will grow by 35% to $120 billion, about $7 billion higher than its previous predictions,
which changed after reviewing the revenue growth of the big cloud companies, AWS, Azure,
Google Cloud, and Alibaba.
Next year, companies would be shifting to cloud-native computing to seize benefits such as
greater efficiency, lower costs, higher speed to delivery, and management ease. Cloud-native
applications make use of micro-services, containers, and an agile framework built on continuous
delivery, which enables businesses to build more reliable systems, more quicker. The
competitive nature of business means that the cloud-native approach, which improves the
speed and efficiency of application development is inevitable in the coming years.
Little wonder Accenture describes cloud-native computing as the latest wave of digital
disruption. Quoting the report, “cloud-native delivers scale, resiliency, and agility—both for the
business and developers—that are almost impossible to achieve with pre-cloud architectures.”
2. Integration of DevOps and Security
Security is defining the coming trends of DevOps significantly more than ever. The massive shift
to the cloud occasioned by the pandemic means there must now be greater emphasis on cloud-
based cybersecurity due to elevated security concerns. Amongst other lessons, the pandemic
has shown that cloud cybersecurity for remote workers is tougher than expected. This has
caused experts to suggest that the cyber pandemic might be next.
DevOps is tied strongly to security; teams can build more secure applications if they integrate
security earlier in the application development. With continuous testing and delivery, DevOps
ensures that nothing is left to chance. Apparently, according to the Puppet State of DevOps
retrospective, “integrating security throughout the software delivery lifecycle leads to faster
software delivery with fewer security issues.”
But this isn’t just about what companies should do; it’s about what companies are doing.
According to GitLab’s DevSecOps Landscape report 2020, the roles of security pros are
changing, with 28% of surveyed respondents reporting being part of a cross-functional team
focused on security. In addition, 27% have seen themselves more involved in regular
development activities. Perhaps more importantly, 65% of these security pros also reported that
security is shifting left, that is, being introduced into development earlier. Typically, security
testing occurs at later stages since there are fewer issues early on. However, we’re witnessing a
change in the trends. This is discussed in the next and final point.
span
3. Testing Shifts Further Left
Not only is security shifting left; testing, as a whole, will shift much further left in the coming
years. Fewer bugs at the early stages mean that developers can detect and fix them easier.
Vulnerabilities discovered at the production stage can cost up to around $7600 to fix whereas
the same vulnerabilities discovered at the early stages of development could be fixed for only
$80-$85 then. Hence, the need for shift-left testing. And developers are responding
appropriately as evidenced by the rising trend.
One of the consequences of shifting left is that traditional testing teams are being stripped of
testing exclusivity. That’s according to Capgemini’s Continuous Testing Report 2020. The report
identifies the driving frameworks behind the increasing popularity of shift left as Model-based
Testing (MBT), Test-driven Development (TDD), and predictive analytics/machine learning.
Shifting left comes with numerous advantages, including increased collaboration and
productivity among workers. Also, it enables the creation of applications that are secure and
compliant in broader environments. After all, it is one thing for a cloud service to be secure in
isolation and it is another to remain secure when integrated with other industry tools.
Conclusion: While there seems to be no end in sight yet for the raging pandemic, 2021 is
already shaping up to be a promising year. The impacts of COVID-19 on business and
development are obviously long-term, and trends that dominate next year will pretty much
determine the future of work, operations, and development. What we have learned so far is that:
There will be many more cloud-native applications,
 Security will be a more important consideration in DevOps, and
 Testing will start appearing much earlier in the development life cycle.
Linux
Linux is one of the most popular operating systems for servers and cloud-based infrastructures.
It gives access to a robust CLI, a scripting environment, essential tools and utilities, strong
security features, and powerful diagnostic tools for troubleshooting. In order to master the art of
delivering high-quality software and infrastructure, it is required for a DevOps Engineer to
master Linux.
Linux is a Unix-Like operating system. All the Linux/Unix commands are run in the terminal
provided by the Linux system. This terminal is just like the command prompt of Windows OS.
Linux/Unix commands are case-sensitive. The terminal can be used to accomplish all
Administrative tasks. This includes package installation, file manipulation, and user
management. Linux terminal is user-interactive. The terminal outputs the results of commands
which are specified by the user itself. Execution of typed command is done only after you press
the Enter key.

Commands Description

Used to check whether the calling program has access to a specified


access file. It can be used to check whether a file exists or not

Used to turn on or turn off the process for accounting or change info
accton process accounting file

aclocal Used to automatically generate aclocal.m4 files from [Link] file

acpi Used to display the battery status and other ACPI information

Tests whether ACPI (Advanced Configuration and Power Interface)


acpi_available subsystem is available or not

It provides intelligent power management on a system and is used to


acpid notify the user-space programs about the ACPI events

addr2line Used to convert addresses into file names and line numbers

It is a Linux version of getty, which is a Unix program running on a host


computer that manages physical or virtual terminals to allow multi-user
agetty access

Instructs the shell to replace one string with another string while
alias executing the commands

It is a command-line mixer for ALSA(Advanced Linux Sound


amixer Architecture) sound-card driver
It is a command-line audio player for ALSA(Advanced Linux Sound
aplay Architecture) sound card drivers.

Used to play standard MIDI(Musical Instrument Digital Interface) files, by


sending the content of a MIDI file to an ALSA(Advanced Linux Sound
aplaymidi Architecture) MIDI port

It helps the user when they don’t remember the exact command but
knows a few keywords related to the command that define its uses or
apropos functionality

Provides a high-level CLI (Command Line Interface) for the package


management system and is intended as an interface for the end user
which enables some options better suited for interactive usage by
default compared to more specialized APT tools like apt-cache and apt-
apt get

apt-get It is a command-line tool which helps in handling packages in Linux

Opens up a highly built-in interface to interact with the package


aptitude manager of the machine

ar Used to create, modify and extract the files from the archives

arch Used to print the computer architecture

It manipulates the System’s ARP cache. It also allows a complete dump


arp of the ARP cache

aspell Used as a spell checker in Linux

atd It is a job scheduler daemon that runs jobs scheduled for later execution

Used to remove the specified jobs. To remove a job, its job number is
atrm passed in the command

atq It displays the list of pending jobs which are scheduled by the user

autoconf Used in Linux to generate configuration scripts

Used to create a template file of C “#define” or any other template


autoheader header for configure to use

Used for automatically generating [Link] files compliant with the set
automake GNU Coding Standards
Used to create automatically buildable source code for Unix-like
autoreconf systems

Used to update [Link] file in our Linux system to a newer


autoupdate Autoconf.

It is a scripting language used for manipulating data and generating


awk reports

banner Used to print the ASCII character string in large letter to standard output

It strips directory information and suffixes from file names i.e. it prints the file
basename
name NAME with any leading directory components removed

Used to read commands from standard input or a specified file and execute
batch them when system load levels permit i.e. when the load average drops
below 1.5

bc Used for command line calculator

bg Used to place foreground jobs in background

A mail notification system for unix that notifies the user at the command line
biff
when new mail arrives and tells from whom it is

bind Used to set Readline key bindings and variables

bison It is basically a parser generator similar to yacc

break Used to terminate the execution of for loop, while loop and until loop

Used to run a shell builtin, passing it arguments(args), and also to get the
builtin
exit status
bzcmp Used to invoke the cmp utility on bzip2 compressed files

bzdiff Used to compare the bzip2 compressed files

Used to search for a pattern or an expression but inside a bzip2-


bzgrep
compressed file

bzip2 Used to compress and decompress the files

It does not have to read the entire input file before starting, so with a large
bzless
file, it starts up faster

Used as a filter for CRT viewing of bzip2 compressed files, which are saved
bzmore
with .bz2 suffix

Used to see the calendar of a specific month or a whole year. By default, it


cal
shows current month’s calendar as output

It is the best alternative when we had to use multiple if/elif on a single


case
variable

Reads data from file and gives their content as output. It helps us to create,
cat
view, concatenate files

cc It is used to compile the C language codes and create executables

ccrypt It is a command line tool for encryption and decryption of data

Known as change directory command. It is used to change current working


cd
directory
It displays or manipulates the disk partition table by providing a text-based
cfdisk
“graphical” interface

chage Used to view and change the user password expiry information

It is a file system command which is used for changing the attributes of a


chattr
file in a directory

It allows you to change a user’s name and other details easily. chfn stands
chfn
for Change finger

chgrp Used to change the group ownership of a file or directory

chkconfig Used to list all available services and view or update their run level settings

chmod Used to change the access mode of a file

chown Used to change the file Owner or group

chpasswd Used to change password for multiple users at a time

chroot Used to change the root directory

chrt Used for manipulating the real-time attributes of a process

chsh Used to change the user’s login shell(currently login shell)

Used to switch between the different TTY (TeleTYpewriter) terminals


chvt
available
Used to display a CRC(Cyclic Redundancy Check) value, the byte size of
cksum
the file and the name of the file to standard output

clear Used to clear the terminal screen

Used to compare the two files byte by byte and helps you to find out
cmp
whether the two files are identical or not

It is used to filter out reverse line feeds. The col utility simply reads from the
col
standard input and writes to standard output

Used to format the text processor output so that it can be viewed on


colcrt
Cathode Ray Tube displays

colrm Removes selected columns from a file

column Used to display the contents of a file in columns

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

Used to reduce the file size. After compression, the file will be available with
compress
an added .Z extension

continue Used to skip the current iteration in for, while and until loop

cp Used to copy files or group of files or directory

cpio stands for “copy in, copy out“. It is used for processing the archive files
cpio
like *.cpio or *.tar. This command can copy files to and from archives
It is automatically used by C compiler to transform your program before
cpp
compilation

A software utility, offered by Linux-like operating system which automates


cron
the scheduled task at a predetermined time

A list of commands that you want to run on a regular schedule, and also the
crontab
name of the command used to manage that list

csplit Used to split any file into many parts as required by the user

It allows quick access across the files (For example quickly seeing definition
ctags
of a function)

It is a type of scheduler for CUPS (Common Unit Printing System). It


cupsd
implements the printing system on the basis of the Internet Printing Protocol

A tool to transfer data to or from a server, using any of the supported


curl
protocols

For cutting out the sections from each line of files and writing the result to
cut
standard output

Used to store the history of a file. Whenever a file gets corrupted or


cvs anything goes wrong “cvs” help us to go back to the previous version and
restore our file

Used to display the system date and time. It is also used to set date and
date
time of the system

Used to evaluate arithmetic expressions. It evaluates expressions in the


dc
form of a postfix expression
It is a command-line utility for Unix and Unix-like operating systems whose
dd
primary purpose is to convert and copy files

Used to declare shell variables and functions, set their attributes and
declare
display their values

Used to generate a list of dependency description of kernel modules and its


depmod
associated map files

Used to display information related to file systems about total space and
df
available space

diff Used to display the differences in the files by comparing the files line by line

diff3 Used to compare the three files line by line

dir Used to list the contents of a directory

Used to remove the trailing forward slahes “/” from the NAME and prints the
dirname
remaining portion

dirs Used to display the list of currently remembered directories

disable Used to stop the printers or classes

Used to examine the kernel ring buffer and print the message buffer of
dmesg
kernel

Used when the user wants to retrieve system’s hardware related


dmidecode information such as Processor, RAM(DIMMs), BIOS detail, etc. of Linux
system in a readable format
Used to return the Network Information System (NIS) domain name of the
domainname
host

dos2unix Converts a DOS text file to UNIX format

dosfsck Diagnoses MS-DOS file system for problems and attempts to repair them

Used to retrieve information or statistics form components of the system


dstat
such as network connections, IO devices, or CPU, etc.

Used to track the files and directories which are consuming excessive
du
amount of space on hard disk drive

dump Used to backup the filesystem to some storage device

Used to print the super block and blocks group information for the
dumpe2fs
filesystem present on device

dumpkeys Used for the dump keyboard translation tables

echo Used to display line of text/string that are passed as an argument

Used for launching the ed text editor which is a line-based text editor with a
ed minimal interface which makes it less complex for working on text files i.e
creating, editing, displaying and manipulating files

It treats the pattern as an extended regular expression and prints out the lines
egrep
that match the pattern

It allows ejecting a removable media (typically a CD-ROM, floppy disk, tape, or


eject
JAZ or ZIP disk) using the software
It is a editor having simple user interface. Also, there is no insert mode in this
emacs
editor. It only have editing mode.

enable Used to start the printers or classes

Used to either print environment variables. It is also used to run a utility or


env
command in a custom environment

eval Built-in command used to execute arguments as a shell command

It is a text editor in Linux which is also termed as the line editor mode of the vi
ex
editor

exec Used to execute a command from the bash itself

exit Used to exit the shell where it is currently running

Allows you to convert tabs into spaces in a file and when no file is specified it
expand
reads from standard input

This command or scripting language works with scripts that expect user inputs. It
expect
automates the task by providing inputs

It is bash shell BUILTINS commands, which means it is part of the shell. It marks
export
an environment variables to be exported to child-processes

expr It evaluates a given expression and displays its corresponding output

Used to print the prime factors of the given numbers, either given from command
factor
line or read from standard input
Used to list, edit or re-execute the commands previously entered into an
fc
interactive shell

fc- It scans the font directories and build font cache for applications which use
cache fontconfig for their font handling

It is used to list the available fonts and font styles. Using the format option, the
fc-list
list of all fonts can be filtered and sorted out

Format disk is a dialog-driven command in Linux used for creating and


fdisk
manipulating disk partition table

fg Used to put a background job in foreground

fgrep Used to search for the fixed-character strings in a file

Used to determine the type of a file. .file type may be of human-readable(e.g.


file
‘ASCII text’) or MIME type(e.g. ‘text/plain; charset=us-ascii’)

find Used to find files and directories and perform subsequent operations on them

It is a user information lookup command which gives details of all the users
finger
logged in.

fmt Works as a formatter for simplifying and optimizing text files

It wraps each line in an input file to fit a specified width and prints it to the
fold
standard output

Used to repeatedly execute a set of command for every element present in the
for
list
Displays the total amount of free space available along with the amount of
free memory used and swap memory in the system, and also the buffers used by the
kernel

Fun Used to draw various type of patterns on the terminal

function Used to create functions or methods

Used for preprocessing, compilation, assembly and linking of source code to


g++
generate an executable file

gawk Used for pattern scanning and processing language

GNU Compiler Collections is used to compile mainly C and C++ language. It


gcc
can also be used to compile Objective C and Objective C++

GNU Debugger tool helps to debug the programs written in C, C++, Ada,
gdb
Fortran, etc.

getent Used to get the entries in a number of important text files called databases

gpasswd Used to administer the /etc/group and /etc/gshadow

Searches a file for a particular pattern of characters, and displays all lines
grep
that contain that pattern

groupadd Used to create a new user group

groupdel Used to delete a existing group

groupmod Used to modify or change the existing group on Linux system


Groups are the collection of users. Groups make it easy to manage users
groups
with the same security and access privileges

It verifies the integrity of the groups information. It checks that all entries in
grpck
/etc/group and /etc/gshadow have the proper format and contain valid data

It is used to convert to shadow groups. The grpconv command creates a


grpconv
gshadow from the group and an optionally existing gshadow

This command invokes Ghostscript, which is an interpreter of Adobe


gs
Systems PostScript and Portable Document Format(PDF) languages

gunzip Used to compress or expand a file or a list of files in Linux

Used to compress executable files and also used to automatically


gzexe
uncompress and execute the files

This command compresses files. Each single file is compressed into a


gzip
single file.

Used to instruct the hardware to stop all the CPU functions. Basically, it
halt
reboots or stops the system.

hash Used to maintain a hash table of recently executed programs

Used to get statistics about the hard disk, alter writing intervals, acoustic
hdparm
management, and DMA settings

Head Prints the top N number of data of the given input

help Displays information about shell built-in commands


Used to filter and display the specified files, or standard input in a human
hexdump
readable specified format

history Used to view the previously executed command

host Used for DNS (Domain Name System) lookup operations

hostid Used to displays the Host’s ID in hexadecimal format

Used to obtain the DNS(Domain Name System) name and set the system’s
hostname
hostname or NIS(Network Information System) domain name.

Provides a proper API used to control Linux system hostname and change
hostnamectl
its related settings

It is a command line utility that allows the user to interactively monitor the
htop
system’s vital resources or server’s processes in real time

hwclock Utility for accessing the hardware clock, also called Real Time Clock (RTC)

iconv Used to convert some text in one encoding into another encoding

Used to find out user and group names and numeric ID’s (UID or group ID) of
id
the current user or any other user in the server

if Used to execute commands based on conditions

ifconfig Used to configure the kernel-resident network interfaces.

It is a network analyzing tool used by system administrators to view the


iftop
bandwidth related stats
It basically brings the network interface up, allowing it to transmit and receive
ifup
data

Used for capturing a screenshot for any of the active pages we have and it
import
gives the output as an image file

Reads documentation in the info format. It will give detailed information for a
info
command when compared with the main page

insmod Used to insert modules into the kernel

install Used to copy files and set attributes

iostat Used for monitoring system input/output statistics for devices and partitions

Used to display and monitor the disk IO usage details and even gets a table
iotop
of existing IO utilization by the process

ip Used for performing several network administration tasks

Used to remove some IPC(Inter-Process Communication) resources. It


ipcrm eliminates the IPC objects and their associated data structure form the
system

Shows information on the inter-process communication facilities for which the


ipcs
calling process has read access

Used to set up and maintain tables for the Netfilter firewall for IPv4, included
iptables
in the Linux kernel

iptables- It will save the current iptables rules in a user specified file, that can be used
save later when the user wants
Used to display the parameters, and the wireless statistics which are
iwconfig
extracted from /proc/net/wireless

It is a command line utility for joining lines of two files based on a key field
join
present in both the files

journalctl Used to view systemd, kernel and journal logs

Used to terminate processes manually. kill command sends a signal to a process


kill
which terminates the process

Used to display the list of all the users logged in and out since the file /var/log/wtmp
last
was created

less Used to read contents of text file one page(one screen) per time

let Used to evaluate arithmetic expressions on shell variables

ln Used to create links between files

locate Used to find the files by name

look Shows the lines beginning with a given string

Used to display details about block devices and these block devices(Except ram
lsblk
disk) are basically those files that represent devices connected to the pc.

Used to generate the detailed information of the system’s hardware configuration


lshw
from various files in the /proc directory
Used to display the status of modules in the Linux kernel. It results in a list of
lsmod
loaded modules

lsof Provides a list of files that are opened

Used to display the information about USB buses and the devices connected to
lsusb
them

This command in Linux prints the mail queue i.e the list of messages that are
mailq
there in the mail queue

Used to display the user manual of any command that we can run on the
man
terminal

md5sum To verify data integrity using MD5 (Message Digest Algorithm 5)

Allows the user to create directories. This command can create multiple
mkdir
directories at once

modinfo Used to display the information about a Linux Kernel module

Used to view the text files in the command prompt, displaying one screen at a
more
time in case the file is large (For example log files)

Used to mount the filesystem found on a device to big tree structure(Linux


mount
filesystem) rooted at ‘/‘

mpstat Used to report processor related statistics.

Used to move one or more files or directories from one place to another in file
mv
system like UNIX
It is one of the powerful networking tool, security tool or network monitoring
nc(netcat)
tool.

Displays various network related information such as network connections,


netstat routing tables, interface statistics, masquerade connections, multicast
memberships, etc.

Used for controlling NetworkManager. nmcli command can also be used to


nmcli display network device status, create, edit, activate/deactivate, and delete
network connections

It is a network administration tool for querying the Domain Name System


nslookup (DNS) to obtain domain name or IP address mapping or any other specific
DNS record

Used to convert the content of input in different formats with octal format as the
od
default format

passwd Used to change the user account passwords

Used to join files horizontally (parallel merging) by outputting lines consisting of


paste lines from each file specified, separated by tab as delimiter, to the standard
output

pidof Used to find out the process IDs of a specific running program

ping Used to check the network connectivity between host and server/host

It is a user information lookup command which gives details of all the users
pinky logged in. Unlike finger, in the pinky, you may trim the information of your
interest.
Used to display the memory map of a process. A memory map indicates how
pmap
memory is spread out

poweroff Sends an ACPI signal which instructs the system to power down

Used to display the given string, number or any other format specifier on the
printf
terminal window

Used to list the currently running processes and their PIDs along with some
ps
other information depends on different options

pwd It prints the path of the working directory, starting from the root

ranlib Used to generate index to archive

rcp Used to copy files from one computer to another computer

Reads up the total number of bytes from the specified file descriptor into the
read
buffer

readelf Used to get information of ELF(Executable and Linkable Format) Files

readlink Used to print resolved symbolic links or canonical file names

reboot Instructs the system to restart or reboot

rename Used to rename the named files according to the regular expression perlexpr

Used to initialize the terminal. This is useful once a program dies leaving a
reset
terminal in an abnormal state
restore Used for restoring files from a backup created using dump

return Used to exit from a shell function.

rev Used to reverse the lines characterwise

Used to remove objects such as files, directories, symbolic links and so on


rm
from the file system like UNIX

rmdir Used to remove empty directories from the filesystem in Linux

rmmod Used to remove a module from the kernel

route Used when you want to work with the IP/kernel routing table

It is a software utility for Unix-Like systems that efficiently sync files and
rsync
directories between two hosts or machines

Used to monitor Linux system’s resources like CPU usage, Memory


sar
utilization, I/O devices consumption, etc.

scp Used to copy file(s) between servers in a secure way.

Provides the ability to launch and use multiple shell sessions from a single
screen
ssh session

script Used to make typescript or record all the terminal activities

Used to replay a typescript/terminal_activity stored in the log file that was


scriptreplay
recorded by the script command
Used to compare two files and then writes the results to standard output in a
sdiff
side-by-side format

Used for finding, filtering, text substitution, replacement and text


sed
manipulations like insertion, deletion search etc.

select Used to create a numbered menu from which a user can select an option

seq Used to generate numbers from FIRST to LAST in steps of INCREMENT

setsid Used to run a program in a new session

shift Shifts/moves the command line arguments to one position left.

prints to standard output either the scan codes or the key code or the `ascii’
showkey
code of each key pressed

shred Used in order to delete a file completely from hard disk

shutdown Used to shutdown the system in a safe way

sleep Used to create a dummy job. A dummy job helps in delaying the execution

Used to read and execute the content of a file(generally set of commands),


source
passed as an argument in the current shell script

sort Used to sort a file, arranging the records in a particular order

split Used to split large files into smaller files


ssh Protocol used to securely connect to a remote server/system

It is one of the most powerful process monitoring, diagnostic, instructional


strace
tool of Linux.

stty Used to change and print terminal line settings

sudo Used as a prefix of some command that only superuser are allowed to run

sum Used to find checksum and count the blocks in a file

sync Used to synchronize cached writes to persistent storage

Used to examine and control the state of “systemd” system and service
systemctl
manager

tac Used to concatenate and print files in reverse

Tail Prints the last N number of data of the given input

tar Used to create Archive and extract the Archive files

Reads the standard input and writes it to both the standard output and one or
tee
more files

Used to execute a command and prints a summary of real-time, user CPU


time
time and system CPU time spent by executing a command when it terminates

top Provides a dynamic real-time view of the running system


touch Used to create, change and modify timestamps of a file

tr It is a command line utility for translating or deleting characters

tracepath Used to traces path to destination discovering MTU along this path

traceroute Prints the route that a packet takes to reach the host

A recursive directory listing program that produces a depth-indented listing of


Tree
files

It displays the information related to terminal. It basically prints the file name of
tty
the terminal connected to standard input

type Used to describe how its argument would be translated if used as commands

uname Displays the information about the system

Converts each spaces into tabs writing the produced output to the standard
unexpand
output

uniq It is a command line utility that reports or filters out the repeated lines in a file

unix2dos Converts a Unix text file to DOS format

Used to execute a set of commands as long as the final command in the ‘until’
until
Commands has an exit status which is not zero

Uptime Used to find out how long the system is active (running)
useradd Used to add user accounts to your system

usermod Used to change the properties of a user in Linux through the command line

It provides a set of commands to fetch username and its configurations from


username
the Linux host

users Used to show the user names of users currently logged in to the current host

userdel Used to delete a user account and related files

It is the default editor that comes with the UNIX operating system is called visual
vi
editor.

It is a performance monitoring command of the system as it gives the information


vmstat
about processes, memory, paging, block IO, disk and CPU scheduling

Used by system administrators in order to monitor network parameters such as


vnstat
bandwidth consumption or maybe some traffic flowing in or out

w Used to show who is logged on and what they are doing

Displays a message, or the contents of a file, or otherwise its standard input, on


wall
the terminals of all currently logged in users

watch Used to execute a program periodically, showing output in fullscreen

Used to find out number of lines, word count, byte and characters count in the
wc
files specified in the file arguments
Used to download files from the server even when the user has not logged on to
Wget
the system and it can work in background without hindering the current process

whatis Used to get a one-line manual page descriptions

Used to locate the executable file associated with the given command by
which
searching it in the path environment variable

Used to repeatedly execute a set of command as long as the COMMAND returns


while
true

who Used to get information about currently logged in user on to system

whoami Displays the username of the current user when this command is invoked

Allows a user to communicate with other users, by copying lines from one user’s
write
terminal to others

Used to build and execute commands from standard input. It converts input
xargs
received from standard input into arguments of a command

xdg-
Used to open a file or URL in the user’s preferred application
open

Used to print a continuous output stream of given STRING. If STRING is not


yes
mentioned then it prints ‘y’

zdiff Used to invoke the diff program on files compressed via gzip

Used to print the current time in the specified zone or you can say prints the
zdump
current time in each zonename named on the command line
zgrep Used to search out expressions from a given a file even if it is compressed

It is a compression and file packaging utility for Unix. Each file is stored in single
zip
.zip {.zip-filename} file with the extension .zip

Introduction to Linux Shell and Shell Scripting


If we are using any major operating system, we are indirectly interacting with the shell. While
running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by
using the terminal. In this article we will discuss Linux shells and shell scripting so before
understanding shell scripting we have to get familiar with the following terminologies:
 Kernel
 Shell
 Terminal

What is Kernel?
The kernel is a computer program that is the core of a computer’s operating system, with
complete control over everything in the system. It manages the following resources of the Linux
system –
 File management
 Process management
 I/O management
 Memory management
 Device management etc.
It is often mistaken that Linus Torvalds has developed Linux OS, but actually, he is only
responsible for the development of the Linux kernel.
Complete Linux system = Kernel + GNU system utilities and libraries + other management
scripts + installation scripts.
What is Shell?
A shell is a special user program that provides an interface for the user to use operating system
services. Shell accepts human-readable commands from users and converts them into
something which the kernel can understand. It is a command language interpreter that executes
commands read from input devices such as keyboards or from files. The shell gets started when
the user logs in or starts the terminal.
Linux Shell
Shell is broadly classified into two categories –
 Command Line Shell
 Graphical shell
Command Line Shell
Shell can be accessed by users using a command line interface. A special program called
Terminal in Linux/macOS, or Command Prompt in Windows OS is provided to type in the
human-readable commands such as “cat”, “ls” etc. and then it is being executed. The result is
then displayed on the terminal to the user. A terminal in Ubuntu 16.4 system looks like this –
In the above screenshot “ls” command with “-l” option is executed. It will list all the files in the
current working directory in a long listing format.
Working with a command line shell is a bit difficult for beginners because it’s hard to memorize
so many commands. It is very powerful; it allows users to store commands in a file and execute
them together. This way any repetitive task can be easily automated. These files are usually
called batch files in Windows and Shell Scripts in Linux/macOS systems.
Graphical Shells
Graphical shells provide means for manipulating programs based on the graphical user
interface (GUI), by allowing for operations such as opening, closing, moving, and resizing
windows, as well as switching focus between windows. Window OS or Ubuntu OS can be
considered as a good example which provides GUI to the user for interacting with the program.
Users do not need to type in commands for every action. A typical GUI in the Ubuntu system –
There are several shells are available for Linux systems like –
 BASH (Bourne Again SHell) – It is the most widely used shell in Linux systems. It is used
as default login shell in Linux systems and in macOS. It can also be installed on
Windows OS.
 CSH (C SHell) – The C shell’s syntax and its usage are very similar to the C
programming language.
 KSH (Korn SHell) – The Korn Shell was also the base for the POSIX Shell standard
specifications etc.
Each shell does the same job but understands different commands and provides different built-
in functions.
What is a terminal?
A program which is responsible for providing an interface to a user so that he/she can access
the shell. It basically allows users to enter commands and see the output of those commands in
a text-based interface. Large scripts that are written to automate and perform complex tasks are
executed in the terminal.

To access the terminal, simply search in search box “terminal” and double-click it.
Shell Scripting
Usually, shells are interactive, which means they accept commands as input from users and
execute them. However, sometimes we want to execute a bunch of commands routinely, so we
have to type in all commands each time in the terminal.
As a shell can also take commands as input from file, we can write these commands in a file
and can execute them in shell to avoid this repetitive work. These files are called Shell
Scripts or Shell Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell
script is saved with `.sh` file extension e.g., [Link].
A shell script has syntax just like any other programming language. If you have any prior
experience with any programming language like Python, C/C++ etc. It would be very easy to get
started with it.
A shell script comprises the following elements –
 Shell Keywords – if, else, break etc.
 Shell commands – cd, ls, echo, pwd, touch etc.
 Functions
 Control flow – if..then..else, case and shell loops etc.
Why do we need shell scripts?
There are many reasons to write shell scripts:
 To avoid repetitive work and automation
 System admins use shell scripting for routine backups.
 System monitoring
 Adding new functionality to the shell etc.
Some Advantages of shell scripts
 The command and syntax are exactly the same as those directly entered in the
command line, so programmers do not need to switch to entirely different syntax
 Writing shell scripts are much quicker
 Quick start
 Interactive debugging etc.
Some Disadvantages of shell scripts
 Prone to costly errors, a single mistake can change the command which might be
harmful.
 Slow execution speed
 Design flaws within the language syntax or implementation
 Not well suited for large and complex task
 Provide minimal data structure unlike other scripting languages. etc.
Simple demo of shell scripting using Bash Shell
If you work on a terminal, something you traverse deep down in directories. Then for coming
few directories up in path we have to execute a command like this as shown below to get to the
“python” directory:

get to the “python” directory:


It is quite frustrating, so why not we can have a utility where we just have to type the name of
directory and we can directly jump to that without executing the “cd ../” command again and
again. Save the script as “[Link]”
 bash

# !/bin/bash

# A simple bash script to move up to desired directory


level directly

function jump()
{
# original value of Internal Field Separator
OLDIFS=$IFS

# setting field separator to "/"


IFS=/

# converting working path into array of directories in


path
# eg. /my/path/is/like/this
# into [, my, path, is, like, this]
path_arr=($PWD)

# setting IFS to original value


IFS=$OLDIFS

local pos=-1

# ${path_arr[@]} gives all the values in path_arr


for dir in "${path_arr[@]}"
do
# find the number of directories to move up to
# reach at target directory
pos=$[$pos+1]
if [ "$1" = "$dir" ];then

# length of the path_arr


dir_in_path=${#path_arr[@]}
#current working directory
cwd=$PWD
limit=$[$dir_in_path-$pos-1]
for ((i=0; i<limit; i++))
do
cwd=$cwd/..
done
cd $cwd
break
fi
done
}

For now, we cannot execute our shell script because it does not have permissions. We have to
make it executable by typing following command –
$ chmod +x path/to/our/file/[Link]
Now to make this available on every terminal session, we have to put this in “.bashrc” file.
“.bashrc” is a shell script that Bash shell runs whenever it is started interactively. The purpose
of a .bashrc file is to provide a place where you can set up variables, functions, and aliases,
define our prompt, and define other settings that we want to use whenever we open a new
terminal window.
Now open the terminal and type the following command:
$ echo “source ~/path/to/our/file/[Link]”>> ~/.bashrc
Now open your terminal and try out new “jump” functionality by typing following command-
$ jump dir_name
Basic Shell Commands in Linux
A shell is a special user program that provides an interface to the user to use operating system
services. Shell accepts human-readable commands from the user and converts them into
something which the kernel can understand. It is a command language interpreter that executes
commands read from input devices such as keyboards or from files. The shell gets started when
the user logs in or starts the terminal.

1). Displaying the file contents on the terminal:


 cat: It is generally used to concatenate the files. It gives the output on the standard
output.
 more: It is a filter for paging through text one screenful at a time.

 less: It is used to viewing the files instead of opening the [Link] to more command
but it allows backward as well as forward movement.
 head : Used to print the first N lines of a file. It accepts N as input and the default value
of N is 10.
 tail : Used to print the last N-1 lines of a file. It accepts N as input and the default value
of N is 10.

2). File and Directory Manipulation Commands:


 mkdir : Used to create a directory if not already exist. It accepts the directory name as
an input parameter.
 cp : This command will copy the files and directories from the source path to the
destination path. It can copy a file/directory with the new name to the destination path. It
accepts the source file/directory and destination file/directory.

 mv : Used to move the files or directories. This command’s working is almost similar
to cp command but it deletes a copy of the file or directory from the source path.
 rm : Used to remove files or directories.

 touch : Used to create or update a file.


3). Extract, sort, and filter data Commands:
 grep : This command is used to search for the specified text in a file.

 grep with Regular Expressions: Used to search for text using specific regular
expressions in file.
 sort : This command is used to sort the contents of files.

 wc : Used to count the number of characters, words in a file.


 cut : Used to cut a specified part of a file.

4). Basic Terminal Navigation Commands:


 ls : To get the list of all the files or folders.
 ls -l: Optional flags are added to ls to modify default behavior, listing contents in
extended form -l is used for “long” output
 ls -a: Lists of all files including the hidden files, add -a flag
 cd: Used to change the directory.
 du: Show disk usage.
 pwd: Show the present working directory.
 man: Used to show the manual of any command present in Linux.
 rmdir: It is used to delete a directory if it is empty.
 ln file1 file2: Creates a physical link.
 ln -s file1 file2: Creates a symbolic link.
 locate: It is used to locate a file in Linux System
 echo: This command helps us move some data, usually text into a file.
 df: It is used to see the available disk space in each of the partitions in your system.
 tar: Used to work with tarballs (or files compressed in a tarball archive)
5). File Permissions Commands: The chmod and chown commands are used to control
access to files in UNIX and Linux systems.
 chown : Used to change the owner of the file.
 chgrp : Used to change the group owner of the file.
 chmod : Used to modify the access/permission of a user.
Linux Directory Structure
In Linux/Unix operating system everything is a file even directories are files, files are files, and
devices like mouse, keyboard, printer, etc are also files. Here we are going to see the Directory
Structure in Linux.
Types of files in the Linux system.
1. General Files – It is also called ordinary files. It may be an image, video, program, or
simple text file. These types of files can be in ASCII or Binary format. It is the most
commonly used file in the Linux system.
2. Directory Files – These types of files are a warehouse for other file types. It may be a
directory file within a directory (subdirectory).
3. Device Files – In a Windows-like operating system, devices like CD-ROM, and hard
drives are represented as drive letters like F: G: H whereas in the Linux system devices
are represented as files. As for example, /dev/sda1, /dev/sda2, and so on.
We know that in a Windows-like operating system, files are stored in different folders on
different data drives like C: D: E: whereas in the Linux/Unix operating system files are stored in
a tree-like structure starting with the root directory as shown in the below diagram.
data storage in Linux/Unix operating systems
The Linux/Unix file system hierarchy base begins at the root and everything starts with the root
directory.
These are the common top-level directories associated with the root directory:

Directories Description

/bin binary or executable programs.

/etc system configuration files.

/home home directory. It is the default current directory.

/opt optional or third-party software.

/tmp temporary space, typically cleared on reboot.

/usr User related programs.

/var log files.

Some other directories in the Linux system:


Directories Description

It contains all the boot-related information files and folders such as conf, grub,
/boot etc.

/dev It is the location of the device files such as dev/sda1, dev/sda2, etc.

/lib It contains kernel modules and a shared library.

/lost+found It is used to find recovered bits of corrupted files.

/media It contains subdirectories where removal media devices are inserted.

/mnt It contains temporary mount directories for mounting the file system.

It is a virtual and pseudo-file system to contains info about the running


/proc processes with a specific process ID or PID.

/run It stores volatile runtime data.

/sbin binary executable programs for an administrator.

/srv It contains server-specific and server-related files.

It is a virtual file system for modern Linux distributions to store and allows
/sys modification of the devices connected to the system.

Exploring directories and their usability:


We know that Linux is a very complex system that requires an efficient way to start, stop,
maintain and reboot a system, unlike Windows operating system. In the Linux system some
well-defined configuration files, binaries, main pages information files are available for every
process.
Linux Kernel File:
 /boot/vmlinux – The Linux kernel file.
Device Files:
 /dev/hda – Device file for the first IDE HDD.
 /dev/hdc – A pseudo-device that output garbage output is redirected to /dev/null.
System Configuration Files:

Configuration
Files Description

/etc/bashrc It is used by bash shell that contains system defaults and aliases.

/etc/crontab A shell script to run specified commands on a predefined time interval.

/etc/exports It contains information on the file system available on the network.

/etc/fstab Information of the Disk Drive and their mount point.

/etc/group It is a text file to define Information of Security Group.

/etc/[Link] It is the grub bootloader configuration file.

/etc/init.d Service startup Script.

/etc/[Link] It contains lilo bootloader configuration file.

/etc/hosts Information of IP and corresponding hostnames

It contains a list of hosts allowed accessing services on the local


/etc/[Link] machine.

/etc/[Link] List of hosts denied accessing services on the local machine.


Configuration
Files Description

/etc/inittab INIT process and their interaction at the various run levels.

/etc/issue Allows editing the pre-login message.

/etc/[Link] It contains the configuration files for the system modules.

/etc/motd It contains the message of the day.

/etc/mtab Currently mounted blocks information.

/etc/passwd It contains username, password of the system, users in a shadow file.

/etc/printcap It contains printer Information.

/etc/profile Bash shell defaults.

/etc/profile.d It contains other scripts like application scripts, executed after login.

/etc/rc.d It avoids script duplication.

/etc/rc.d/init.d Run Level Initialisation Script.

/etc/[Link] DNS being used by System.

/etc/security It contains the name of terminals where root login is possible.


Configuration
Files Description

/etc/skel Script that initiates new user home directory.

/etc/termcap An ASCII file that defines the behavior of different types of the terminal.

/etc/X11 Directory tree contains all the conf files for the X-window System.

User Related Files:

User Related Files Descriptions

/usr/bin It contains most of the executable files.

/usr/bin/X11 Symbolic link of /usr/bin.

/usr/include It contains standard files used by C program.

/usr/share It contains architecture independent shareable text files.

/usr/lib It contains object files and libraries.

/usr/sbin It contains commands for Super User, for System Administration.

Virtual and Pseudo Process Related Files:

Virtual and Pseudo Process


Related Files Descriptions

/proc/cpuinfo CPU Information


Virtual and Pseudo Process
Related Files Descriptions

It keeps useful info about the processes that are


/proc/filesystems currently running.

it keeps the information about the number of interrupts


/proc/interrupts per IRQ.

Contains all the Input and Output addresses used by


/proc/ioports devices on the server

/proc/meminfo It reports the memory usage information.

/proc/modules Currently using kernel module.

/proc/mount Mounted File-system Information.

/proc/stat It displays the detailed statistics of the current system.

/proc/swaps It contains swap file information.

Version Information File:


 /version – It displays the Linux version information.
Log Files:

Log Files Descriptions

/var/log/lastlog It stores user’s last login info.

/var/log/messages It has all the global system messages


Log Files Descriptions

/var/log/wtmp It keeps a history of login and logout information.

To check the Linux directories, open the terminal and execute sudo -s followed by system
password to give root privilege. Then after changing the current home directory to the root
directory and check the list of all available directories in the base directory as shown below.
Input Output Redirection in Linux
In Linux, whenever an individual runs a command, it can take input, give output, or do both.
Redirection helps us redirect these input and output functionalities to the files or folders we
want, and we can use special commands or characters to do so.
For example, if we run the “date” command, it gives us output on the screen. However, if we
want to save the result in a file instead, we can use output redirection. This way, we can store
the output of the date command in a file and refer to it later. These redirections can come in
handy when we work with multiple and large outputs or inputs since we can use file data directly
as input and store results in files.
All this can be done easily on the terminal using some simple commands. In this article, we will
talk about different types of Redirections in linux and how to use Redirections in linux.
Types of Redirection
1. Overwrite Redirection:
Overwrite redirection is useful when you want to store/save the output of a command to a file
and replace all the existing content of that file. for example, if you run a command that gives a
report, and you want to save the report to the existing file of the previous report you can use
overwrite redirection to do this.
 “>” standard output
 “<” standard input
Implementation:
So whatever you will write after running this command, will be redirected and copied to the
“[Link]”. This is standard output redirection.
cat > [Link]
Now, this is standard input redirection, cat command will take the input from “[Link]” and print
it to the terminal screen. This line of code also shows the real working and meaning of the cat
command that is copy and paste. Many people have a misconception that the cat is used to
create a file, but it is not true, the main work of the cat is to copy the input and give the output to
the screen.
cat < [Link]

Let’s see an example to understand the real work of cat command


cat

Just type cat on the terminal and hit enter. It will ask for the input lines, you could write your
name and hit enter. You will see your input will be reprinted.
(base) [root@localhost ~]# cat
Hello this is GeeksForGeeks
Hello this is GeeksForGeeks
This is used when we want to append some lines to the existing content of the file. If you use
only a single angular bracket all the content of the file will be lost.
cat >> [Link]

2. Append Redirection:
With the help of this Redirection, you can append the output to the file without compromising the
existing data of the file.
 “>>” standard output
 “<<” standard input
Implementation:
A here-document is used to redirect input into an interactive shell script or program. You can run
any program within a shell script without user action by supplying the required input for the
interactive program, or interactive shell script.
The general form for a here document is −
Syntax:
command << delimiter
document
delimiter

(base) [root@localhost ~]# cat << [Link]


> Hello This is
> GeeksForGeeks
[Link]
Hello This is
GeeksForGeeks
(base) [root@localhost ~]#
Note: Here, [Link] is a delimiter.
The delimiter marks the ending point of the document. Without it, the shell continues to read the
input forever. The delimiter must be a single word that does not contain spaces or tabs.

3. Merge Redirection:
This allows you to redirect the output of a command or a program to a specific file descriptor
instead of standard output. the syntax for using this is “>&” operator followed by the file
descriptor number.
 “p >& q” Merges output from stream p with stream q
 “p <& q” Merges input from stream p with stream q
Implementation:
Error Redirection: Error redirection is transferring the errors generated by some false
commands to a file rather than STDOUT.
Whenever a program is executed at the terminal, 3 files are generated: standard input(0),
standard output(1), standard error(2). These files are always created whenever a program is
run. By default, an error stream is displayed on the screen.
Examples:
1. In the below-mentioned example, the file descriptor used above is 2(STDERR). Using “2>” re-
directs the error output to a file named “[Link]” and nothing is displayed on STDOUT.
$ somerandomcommand 2>[Link]

2. Here, 2>&1 means that STDERR redirects to the target of STDOUT. More formally, the error
message generated by “2” gets merged with the current output “1“.
$ ls GEEK GFG > [Link] 2>&1
In the above example, the directory GEEK is not present. The error output is merged with the
standard output which in turn is being re-directed to “[Link]“.
Shell Scripting – Shell Variables
A shell variable is a character string in a shell that stores some value. It could be an integer,
filename, string, or some shell command itself. Basically, it is a pointer to the actual data stored
in memory. We have a few rules that have to be followed while writing variables in the script
(which will be discussed in the article). Overall knowing the shell variable scripting leads us to
write strong and good shell scripts.
Rules for variable definition
A variable name could contain any alphabet (a-z, A-Z), any digits (0-9), and an underscore ( _ ).
However, a variable name must start with an alphabet or underscore. It can never start with a
number. Following are some examples of valid and invalid variable names:
 Valid Variable Names
ABC
_AV_3
AV232
 Invalid variable names
2_AN
!ABD
$ABC
&QAID
Note: It must be noted that no other special character except underscore can be used in a
variable name because all other special characters have special meanings in Shell Scripting.
Defining Variables
Syntax
variable_name = <variable data>
Example
num="1"
name="Devil"
These kinds of variables are scalar variables as they could hold one value at a time.
1) Accessing variable
Variable data could be accessed by appending the variable name with ‘$’ as follows:
#!/bin/bash

VAR_1="Devil"
VAR_2="OWL"

echo "$VAR_1$VAR_2"
Output:
DevilOWL
Example of Accessing variable
2) Unsetting Variables
The unset command directs a shell to delete a variable and its stored data from list of variables.
It can be used as follows:
#!/bin/bash

var1="Devil"
var2=23
echo $var1 $var2

unset var1

echo $var1 $var2


Output:
DEVIL 23
23
Example of Unsetting Variables
Note: The unset command could not be used to unset read-only variables.
3) Read only Variables.
These variables are read only i.e., their values could not be modified later in the script.
Following is an example:
#!/bin/bash
var1="Devil"
var2=23
readonly var1
echo $var1 $var2
var1=23
echo $var1 $var2
Output:
Devil 23
./bash1: line 8: var1: readonly variable
Devil 23

Example of Read only Variables.


Now let us see all the above codes in action together. Following is a shell script that includes all
the shell variables discussed above.
#!/bin/bash
#variable definitions
Var_name="Devil"
Var_age=23

# accessing the declared variables using $


echo "Name is $Var_name, and age is $Var_age."

# read-only variables
var_blood_group="O-"
readonly var_blood_group
echo "Blood group is $var_blood_group and read only."
echo "Error for read only variables, if trying to \
modify them."
echo
var_blood_group="B+"
echo

# unsetting variables
unset Var_age
echo "After unsetting var_age..."
echo
echo "Name is $Var_name, blood group is $var_blood_group\
and age is $Var_age..."
Output:

All outputs
Variable Types
We can discuss three main types of variables:
1) Local Variable:
Variables which are specific to the current instance of shell. They are basically used within the
shell, but not available for the program or other shells that are started from within the current
shell.
For example:
`name=Jayesh`
In this case the local variable is (name) with the value of Jayesh. Local variables is temporary
storage of data within a shell script.
2) Environment Variable:
These variables are commonly used to configure the behavior script and programs that are run
by shell. Environment variables are only created once, after which they can be used by any
user.
For example:
`export PATH=/usr/local/bin:$PATH` would add `/usr/local/bin` to the beginning of the shell’s
search path for executable programs.
3) Shell Variables:
Variables that are set by shell itself and help shell to work with functions correctly. It contains
both, which means it has both, some variables are Environment variable, and some are Local
Variables.
For example:
`$PWD` = Stores working directory
`$HOME` = Stores user’s home directory
`$SHELL` = Stores the path to the shell program that is being used.
Few more examples in Shell Scripting and Shell Variable
How to Store User Data in a Variable?
#!/bin/bash
echo "Enter the length of the rectangle"
read length
echo "Enter the width of the rectangle"
read width
area=$((length * width))
echo "The are of the rectangle is: $area"
In this example the variables ‘length’, ‘width’ and ‘area’ are used to store user input and
calculate the area of the rectangle.
Giving input to the variable
In this ‘echo’ is a command used to print the statement and ‘read’ is a command used to take
data from user and store it in a variable.
To Store and Display Message
We can write a script in which we will display a message to the user by looking at the time of the
day. In this we can use shell variable to store and display our message.
#!/bin/bash
time=$(date +%H)
if [ $time -lt 12];then
message = "Good Morning User"
elif [ $time -lt 18 ];then
message = "Good Afternoon User"
else
message = "Good Evening User"
fi
echo "$message"hours

Getting output based on the time of the day.


In this ‘time’ is a variable storing hours, ‘data’ is a command used to get the current time and
‘%H’ is used to extract only hour’s part. ‘-lt’ is an operator used for numerical comparison it is a
less than. ‘fi’ is used to mark the end pf ‘if’ statement.
What is Shell and Its Type?
It is a program that provides a user interface that is used to access operating system services.
Or we can say that it is an environment in which we can run our programs and shell scripts etc.
It is the core of the operating system.
There are several different types of shell available. Some common types of shells include:
1. Bourne Shell (sh): The original shell of UNIX operating system. It has been used for
scripting purposes and also to provide basic commands.
2. C Shell (csh): This is also a popular shell for UNIX operating system. As the name
suggests, its syntax is similar to C programming language.
3. Bourne-Again Shell (bash): It is a widely used shell for macOS and Linux operating
systems. It is more advanced than the original Bourne shell and also has many features
that are found in the Korn shell and C shell.
What is Shell Variable Used For?
Shell Variables are used to store data and information within a shell (terminal), and they are also
used for controlling the behavior of program and scripts. Some of the common uses are:
1. Setting environment variables.
2. Storing configuration data.
3. Storing temporary data.
4. Passing arguments to scripts.
What is Shell Variable and Shell Scripting?
Shell Variable is used in shell scripts for many functionalities like storing data and information,
taking input from users, printing values that are stored. They are also used for storing data
temporarily and storing output of commands.
Shell Scripting is a way of writing scripts of programs that are executed in a terminal or shell.
Basically, it is a program or script which is written with the help of variables mentioned in it. It is
powerful because it can automate tasks, and in this one can use programming constructs that
are available in shell, such as loops, conditionals and functions.
Variables are quite important in any script or program, so we need to understand what is the
convention to name these variables. There are certain rules and standards to keep in mind
while giving names to the variables in Shell scripting. In this article, we will discuss and list down
all the rules and conventions to declare a variable name in Shell/Bash scripting.
Rule 1: A variable name can have letters, numbers, and underscores
A variable name can be alphanumeric i.e alphabets and numbers along with an underscore. You
can use lowercase or uppercased alphabets in the name. We can use the underscore character
at any position in the variable name.
For example:
The variables var_name have an underscore and have lowercased characters. The variable X2
has an uppercase character and number. Finally, the last variable has uppercase as well as
lowercase characters, numbers, and underscores.
Note: Do not use _ as a variable name as when you try to access the variable _ with $_ it will, in
turn, give an error as it is a reserved command for accessing the large argument of the last
command.
Rule 2: There should be no whitespace on either side of the assignment operator(=)
The variable name should not be followed by any whitespace on either side of the assignment
operator. If you have any spaces before or after the assignment operator the shell interpreter
might give error messages about the variable name is a command in itself.
For example:
The variable assignment is considered illegal in the following cases:
name = "John"
name= "John"
name ="John"
The correct way to assign a variable in shell scripting is without any whitespace on either side of
the assignment operator(=) which is shown as follows:
name="John"

Rule 3: The variable name cannot have special characters


Apart from an underscore as discussed in the first rule, a variable name cannot have any
special character. As shell scripting uses certain special characters for some operational tasks
like $ for accessing the variable literal value, & as the and operator * and # as the array/string
extractor and so on. So, it is rather not possible to use any special characters in the variable
name in a shell script.
Rule 4: The first character of the variable name cannot be a number
Though we can use numbers in the variable name the only exception will arise when the
number is the first character in the variable name. So, any position is valid for the number
except the first character of the variable name.
Following are the illegal variable names as they have numbers as the first character.
2X
1st_name
6_gate
Rule 5: Variable names cannot be reserved words
It is not a good practice to use reserved keywords like if, else, while, for, until, case, etc as a
variable name. Though it doesn’t give any errors it is still a bad practice and can easily mess up
the readability of the shell script and cause certain bugs in some cases.
The reserved words in shell/bash scripting are as follows:
if
elif
else
then
while
do
done
for
until
case
esac
continue
break
function
Try to avoid using this as the variable names.
Rule 6: Variable name cannot have whitespace in between
We cannot have a space between the variable name like
first name="John"
This will generate an error as the shell interpreter will treat the word first as a command and will
return an error. So, we need to avoid using spaces in between variable names we can use _
instead to create multi-word variable names for better readability.
So these are the rules to be kept in mind while declaring variable names in a shell scripting
environment.
Bash Scripting – Introduction to Bash and Bash Scripting
Bash is a command-line interpreter or Unix Shell and it is widely used in GNU/Linux Operating
System. It is written by Brian Jhan Fox. It is used as a default login shell for most Linux
distributions. Scripting is used to automate the execution of the tasks so that humans do not
need to perform them individually. Bash scripting is a great way to automate different types of
tasks in a system. Developers can avoid doing repetitive tasks using bash scripting.
Bash scripting supports variables, conditional statements, and loops just like programming
languages.
Applications of Bash Scripts:
 Manipulating files
 Executing routine tasks like Backup operation
 Automation
The advantages of using Bash Scripts are given below –
Advantages of Bash Scripts:
 It is simple.
 It helps to avoid doing repetitive tasks
 Easy to use
 Frequently performed tasks can be automated
 A sequence of commands can be run as a single command.
The disadvantages of the Bash Scripts are given below –
Disadvantages of Bash Scripts:
 Any mistake while writing can be costly.
 A new process launched for almost every shell command executed
 Slow execution speed
 Compatibility problems between different platforms
How to Write Bash Scripts?
To write a Bash Script we will follow the steps –
 First, we will create a file with the .sh extension.
 Next, we will write down the bash scripts within it
 After that, we will provide execution permission to it.
To create and write a file with the .sh extension we can use gedit text editor. The command for it
will be –
gedit [Link]
The first line of our script file will be –
#!/bin/bash
This will tell, the system to use Bash for execution. Then we can write our own scripts.
Let’s write down just a simple script that will print some lines in the terminal. The code for it will
be –
#!/bin/bash

echo "Hello, GeeksforGeeks"


Now we will save the file and provide the execution permission to it. To do so use the following
command –
chmod +x [Link]
Next to execute the following script we will use the following command –
./[Link]
Here is the terminal shell pictorial depiction after executing the above commands as follows:

Here the script file name is [Link].


Now we can also write more complicated commands using Bash Scripts. Here is an example
below in which we are using a condition statement –
Example Script:
#!/bin/bash
Age=17
if [ "$Age" -ge 18 ]; then
echo "You can vote"
else
echo "You cannot vote"
fi
Output:
You cannot vote
Here is the terminal shell pictorial depiction after executing the above script as follows:
In the above way, we can execute multiple Bash commands all at once.
Now Let’s look into the other important concepts related to Bash Scripting.
File Names and Permissions
In the above example, we have saved the file using [Link] name and also provided execute
permission using chmod command. Now, let’s understand why we have done that.
While writing bash scripts we should save our file with the .sh extension, so that the Linux
system can execute it. When we first create a file with the .sh extension, it doesn’t have any
execute permission and without the execute permission the script will not work. So, we should
provide execute permission to it using the chmod command.
The filename of a bash script can be anything but by convention, it is recommended to use
snake case ( my_bash_script.sh ) or hyphens ( [Link] ) for naming a script file.
Variables
We can use variables in bash scripting. Below is a sample program to understand the usage of
variables in Bash scripts.
Example Script:
Name="SATYAJIT GHOSH"
Age=20

echo "The name is $Name and Age is $Age"


Output of Variables:
The name is SATYAJIT GHOSH and Age is 20
So, here is have declared two variables Name and another one is Age. These variables are
accessible using $Name and $Age. That means, we can declare a variable in a bash script
using VariableName=Value and can access it using $VariableName. Here is the terminal shell
pictorial depiction after executing the above script as follows:
There are two types of variables present within Bash Scripting. Conventionally, If a variable, is
declared inside a function then it is generally a local variable and if it is declared outside then it
is a global variable. In the case of a bash script, this concept is a little bit different, here any
variable whether it is written inside a function or outside a function by default is a global
variable. If we want to make a local variable then we need to use the keyword “local”.
Note: It is best practice to always use a local variable inside a function to avoid any
unnecessary confusion.
An example of the same is given below –
Example Script:
#!/bin/bash

var1="Apple" #global variable


myfun(){
local var2="Banana" #local variable
var3="Cherry" #global variable
echo "The name of first fruit is $var1"
echo "The name of second fruit is $var2"
}
myfun #calling function

echo "The name of first fruit is $var1"


#trying to access local variable
echo "The name of second fruit is $var2"
echo "The name of third fruit is $var3"
Output of local and global variables:
The name of first fruit is Apple
The name of second fruit is Banana
The name of first fruit is Apple
The name of second fruit is
The name of third fruit is Cherry
Here in this above example, var2 is a local variable, so when we are accessing it from the
function it is doing fine but when we are trying to access it outside the function, it is giving us an
empty result in the output.
On the other hand, unlike programming languages, even though var3 is defined inside a
function still it is acting as a global variable and it can be accessed outside the function. Below
is the terminal shell depiction after executing the script –

Input and Output


Input & output are fundamental concepts for shell scripting. A script can take one or more inputs
and can also produce zero or many outputs. It may even produce some errors. Let’s
understand this with an example –
Example Script:
echo "Enter filename"
read filename

if [ -e $filename ]
then
echo "$filename is exits on the directory"
cat $filename

else
cat > $filename
echo "File created"
fi
Output of Input & Output:
First time:
Enter filename
[Link]
Hello Geek
File created
Second time:
Enter filename
[Link]
[Link] is exits on the directory
Hello Geek
So, in this above example the first time, the script could not find any file with that file name, and
the else block gets executed. It created the file and put some data into that file. When we run it
a second time with the same file name, then it finds the file. So, is the if block gets executed and
that displays the contents of the file. Reading the file contents is input and on the first time
putting data into the file is considered to be output. Here we have used > for storing the content
in a file. The > notation is used to redirect stdout to a file. On the other hand, we can
use 2> notation to redirect stderr, and &> to redirect both stdout and stderr.
Below is the terminal shell pictorial depiction after executing the following script –
Functions
In programming, A function is a block of code that performs some tasks and it can be called
multiple times for performing tasks. The simplest example of the use of function in Bash
scripting can be given as –
Example Script:
#!/bin/bash
#It is a function
myFunction () {
echo Hello World from GeeksforGeeks
}

#function call
myFunction
Output of Functions:
Hello World from GeeksforGeeks
The above example shows a function that prints something when called.
So, the basic syntax for writing functions within a Bash Script will be –
Syntax of Functions:
#for defining
function_name(){
commands
.....
}

function_name # for calling


Besides this, we can also have functions with passing arguments and with return values.
Decision Making
In programming, Decision Making is one of the important concepts. The programmer provides
one or more conditions for the execution of a block of code. If the conditions are satisfied then
those block of codes only gets executed.
Two types of decision-making statements are used within shell scripting. They are –
1. If-else statement:
If else statement is a conditional statement. It can be used to execute two different codes based
on whether the given condition is satisfied or not.
There are a couple of varieties present within the if-else statement. They are –
 if-fi
 if-else-fi
 if-elif-else-fi
 nested if-else
The syntax for the simplest one will be –
Syntax of If-else statement:
if [ expression ]; then

statements

fi
Example Script:
Name="Satyajit"
if [ "$Name" = "Satyajit" ]; then
echo "His name is Satyajit. It is true."
fi
Output of if-else statement:
His name is Satyajit. It is true.
In the above example, during the condition checking the name matches and the condition
becomes true. Hence, the block of code present within the if block gets executed. In case the
name doesn’t match then will not have an output. Below is the terminal shell pictorial depiction
after executing the following script –

2. case-sac statement:
case-sac is basically working the same as switch statement in programming. Sometimes if we
have to check multiple conditions, then it may get complicated using if statements. At those
moments we can use a case-sac statement. The syntax will be –
Syntax of case-sac statement:
case $var in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
esac
Example Script:
Name="Satyajit"
case "$Name" in
#case 1
"Rajib") echo "Profession : Software Engineer" ;;

#case 2
"Vikas") echo "Profession : Web Developer" ;;
#case 3
"Satyajit") echo "Profession : Technical Content Writer" ;;
esac
Output of case-sac statement:
Profession : Technical Content Writer
In the above example, the case-sac statement executed the statement which is a part of the
matched pattern here the ‘Name’. Below is the terminal shell pictorial depiction after executing
the following script –

String and Numeric Comparisons


The string comparison means in the shell scripting we can take decisions by doing comparisons
within strings as well. Here is a descriptive table with all the operators –

Operator Description

== Returns true if the strings are equal

!= Returns true if the strings are not equal

-n Returns true if the string to be tested is not null

-z Returns true if the string to be tested is null

Arithmetic operators are used for checking the arithmetic-based conditions. Like less than,
greater than, equals to, etc. Here is a descriptive table with all the operators –
Operator Description

-eq Equal

-ge Greater Than or Equal

-gt Greater Than

-le Less Than or Equal

-lt Less Than

-ne Not Equal

Below is a simple example of the same –


Example Script:
if [ 10 -eq 10 ];then
echo "Equal"
fi

if [ 'Geeks' == 'Geeks' ];
then
echo "same" #output
else
echo "not same"
fi
Output of String and Numeric Comparisons:
Equal
same
In this example first one (-eq )is a numeric comparison that checks for equality. The second one
( == ) is also check for equality but in strings. Below is the terminal shell pictorial depiction after
executing the following script –

We will learn more about this concept in the next articles.


Conclusion
In this article we discuss Bash scripting which plays a vital role in automating tasks within the
Linux environment. As a command-line interpreter, Bash offers simplicity and ease of use for
developers to create scripts that streamline routine operations, manipulate files, and execute
complex tasks. While Bash scripts can enhance productivity by avoiding repetitive actions and
executing sequences of commands efficiently, they also come with considerations such as
managing permissions, handling variables, and implementing decision-making structures. By
mastering Bash scripting, users can harness its power to optimize their workflow and enhance
system administration tasks on Linux platforms.
Debian Software Package Management(dpkg) in Linux
In Linux, there are a lot of different distributions and each of these distributions has a different
package type. For example .rpm or Red hat Package Manager is used as the package in the
Linux distribution. A package is the compressed version of the software. In this article, we will go
through the Debian package which is used by Ubuntu. D package or Debian Package is used to
install and download the software in Debian based Linux systems. Debian files end with .deb
extension.
Working with Debian Packages using dpkg Command
1. Installing a stand-alone package using the Debian package
To install a package -i flag is used. To download a stand-alone package using the Debian
package, this command is used:
sudo dpkg -i name_of_package.deb
Example: To download standalone package for the open-source text editor, atom
sudo dpkg -i [Link]
2. Removing a package using the Debian package
To remove a package -r flag is used
sudo dpkg -r name_of_package
Example: To remove the package for the text editor “atom”
sudo dpkg -r atom

Note: -P flag helps to remove everything including conf files.


dpkg -P [package-name]
dpkg -P googler_3.3.0-1_all.deb
3. Listing the debian packages
To list all the Debian packages -l flag is used.
dpkg -l
The above line would give the output similar to the one shown below:
To find a particular package use the grep command:
dpkg -l | grep name_of_package.
The output of this command would look similar to the one shown below:

4. List the dpkg commands available


-help option lists all the available dpkg commands
dpkg –help
Output of this command would look similar to the one shown below:
5. View the content of a particular package
To view the content of the particular package -c flag is used
dpkg -c [name of the package]
dpkg -c flashplugin-nonfree_3.2_i386.deb
Output of this command would look similar to the one shown below:

6. Print architecture of dpkg installs


–print-architecture command prints the architecture of dpkg installs
dpkg --print-architecture
Output could be amd64, i386, etc.
For example, the output produced by the above code is as shown:

7. Unpack a package
–unpack flag helps us unpack the package.
dpkg --unpack [package-name]
dpkg --unpack flashplugin-nonfree_3.2_i386.deb
The output would look as shown below:

The package can be later configured using –configure flag.


dpkg --configure [package-name]
dpkg --configure flashplugin-nonfree

8. Check if the package is installed or not


To check if a particular package is installed or not -s flag is used.
dpkg -s [package-name]
dpkg -s git
The output would look as shown below:
9. Reconfigure the installed package
To reconfigure the already installed package dpkg-reconfigure command is used
dpkg-reconfigure [package-name]
Locate the installed package
The location of the installed package can be found using -L flag.
dpkg -L [package-name]
dpkg -L git
The output would look as shown below:
10. Check for any issue with the installed package
–audit flag would be used to check for the issues with the package.
dpkg --audit
11. Erase information
–clear-avail – Erases available information about the current packages
dpkg –-clear-avail
12. Display dpkg version
dpkg –version is used to display dpkg version information.
sudo dpkg --version

Linux Networking Tools


One can use a variety of network tools to perform tasks such as obtaining information about
other systems on your network, accessing other systems, and communicating directly with other
users. Network information can be obtained using utilities such as ping, finger, traceroute,
host, dig, nslookup etc. These are useful for smaller networks and enable to access remote
systems directly to copy files or execute the command.

Network Information Tools are listed below:


ping
The ping command is used to check if a remote system is running or up. In short this command
is used to detect whether a system is connected to the network or not.
Syntax:
$ ping [Link]

Note: In place of using domain name you can use IP address also. A ping operation can fail
if ping access is denied by a network firewall.
host
This command is used to obtain network address information about a remote system connected
to your network. This information usually consists of system’s IP address, domain name address
and sometimes mail server also.
Syntax:
$ host [Link]

finger
One can obtain information about the user on its network and the who command to see what
users are currently online on your system. The who command list all users currently connected,
along with when, how long, and where they logged in. finger can operate on large networks,
though most systems block it for security reasons.
Syntax:
$ finger [Link]
In place of ABC you can use any website domain or IP address.
traceroute
This command is used to track the sequence of computer networks. You can track to check the
route through which you are connected to a host. mtr or xmtr tools can also be used to perform
both ping and traces. Options are available for specifying parameters like the type of service (-t)
or the source host (-s).
netstat
This command is used to check the status of ports whether they are open, closed, waiting, and
masquerade connections. Network Statistic (netstat) command displays connection information,
routing table information, etc.
Syntax:
$ netstat

Note: To display routing table information use (netstat -r).


tracepath
tracepath performs a very similar function to that of traceroute command. The main difference
between this command is that tracepath doesn’t take complicated options. This command
doesn’t require root privileges.
Syntax:
$ tracepath [Link]

dig
dig(Domain Information Groper) query DNS-related information like a record, cname, mxrecord
etc. This command is used to solve DNS related queries.
Syntax:
$ dig [Link]

hostname
This command is used to see the hostname of your computer. You can change hostname
permanently in etc/sysconfig/network. After changing the hostname you need to reboot the
computer.
Syntax:
$ hostname
route
The route command is used to display or modify the routing table. To add a gateway use (-n).
Syntax:
$ route -n

nslookup
You can use nslookup(name server lookup) command to find out DNS related queries or testing
and troubleshoot DNS server.
Syntax:
$ nslookup [Link]

Network configuration and trouble shooting commands in Linux


Computers are often connected to each other on a network. They send requests to each other
in the form of packets that travel from the host to the destination. Linux provides various
commands from network configuration and troubleshooting.

Here is the list of important commands:


Using Ping (Packet Internet Groper) Command for Network configuration and
troubleshooting

The ping command is used to ensure that a computer can communicate with a specified device
over the network. The pings command sends Internet Control Message Protocol (ICMP) Echo
Request messages in the form of packets to the destination computer and waits in order to get
the response back. Once the packets are received by the destined computer, it starts sending
the packets back. This command keeps executing until it is interrupted.
ping command provides details such as
 the number of packets transmitted.
 the number of packets received.
 time is taken by the packet to return.
ping command is generally used for the following purposes:
 measuring the time taken by the packets to return to determine the speed of the
connection.
 to make sure that the network connection between the host and the destined computer
can be established.

ping [Link]
ping an IP
Using nslookup Command for Network configuration and troubleshooting

nslookup command queries the DNS in order to fetch the IP address or the domain name from
DNS records.

nslookup [Link]
Using traceroute Command for Network configuration and troubleshooting

This command is used to get the route of a packet. In other words, the traceroute command is
used to determine the path along which a packet travels. It also returns the number of hops
taken by the packet to reach the destination. This command prints to the console a list of hosts
through which the packet travels in order to the destination.
traceroute [Link]
Using host Command for Network configuration and troubleshooting

host command is used to find a domain name associated with the IP address or find an IP
address associated with the domain name. The returned IP address is either IPv4 or IPv6.

host [Link]

host [Link]
Using netstat Command for Network configuration and troubleshooting

netstat (Network Statistics) is the command that is used to display routing tables, connection
information, the status of ports, etc. This command works with Linux Network Subsystem. This
command basically displays the content of /proc/net file defined in the Linux file system.
netstat

netstat -r
Using Arp Command for Network configuration and troubleshooting

ARP (Address Resolution Protocol) command is used to display and modify ARP cache, which
contains the mapping of IP address to MAC address. The system’s TCP/IP stack uses ARP in
order to determine the MAC address associated with an IP address.

arp
arp -e
Using ifconfig Command for Network configuration and troubleshooting

ifconfig(Interface Configuration) is a utility in an operating system that is used to set or display


the IP address and netmask of a network interface. It also provides commands to enable or
disable an interface. Many UNIX-like operating systems initialize their network interfaces using
ifconfig at boot time. ifconfig is also used to view the MTU(Maximum transmission unit).

ifconfig
Using Dig Command for Network configuration and troubleshooting
It is called domain information groper; it is a tool used to find query information related to
domain name and troubleshoot DNS issue in Linux. This tool can provide various types of DNS
records, such as CNAME, MX records and records etc.
For example: If we want to query a record for “[Link]”
syntax:
dig A [Link]
This will show the IP address associated with the domain name.
Using Route Command for Network configuration and troubleshooting
This tool helps us display and manipulate the routing table in Linux. Information contained by
this is about how network packets should be routed through a network.
Syntax:
route
This command shows destination, mask, flags, metric, gateway, reference count, and interface.
We can also add or delete routes from a network with IP address.
For example: If we have “IP address: [Link]” and “Subnet mask = 24” and “gateway
(gw) = [Link]”
syntax:
route add -net [Link]/24 gw [Link]
Using Ethtool Command for Network configuration and troubleshooting
This tool is used to view and modify the settings of a network interface card (NIC) in Linux. It
has replaced the old tool named mii-tool. This command can be used to view the current speed
and duplex setting of the NIC. To view the settings for the NIC named “enp0s3” use the
following command.
Syntax:
enthool enp0s3
You can find your NIC name by using “ifconfig” command.
Ethtool
Using Hostname Command for Network configuration and troubleshooting
This command is used to display the current hostname of the system.
Hostname
We can also change our host name by using commands.
syntax:
hostnamectl set-hostname mypc
This will set the hostname to “mypc” and display it after reboot also.
Linux SSH Server (sshd) Configuration and Security Options With Examples
SSH is short for Secure Shell or Secure Socket shell. According to Wikipedia, the Secure Shell
Protocol is a cryptographic network protocol for operating network services securely over an
unsecured network. sshd is short for Secure shell daemon. SSH is one of the most reliable
ways that you can choose to secure your Linux server-Virtual Private Server, which may be
hosted on the Cloud or a server that you have hosted locally on your machine.
This article assumes that you already have ssh utilities installed on your Linux machine.
Configuration and Security Options
Step 1: Generate ssh key pairs using the keygen utility.
Open your Linux terminal and connect to your server. Next on the client side(open another
terminal) run the following commands to log in using ssh key pairs. To generate public and
private key pairs execute the below command:
ssh-keygen -t rsa -b 2048 -C “put any comments here”

To view, the id_rsa.pub key, then run execute the below command.
cat .ssh/id_rsa.pub

The below command lists the contents of the id_rsa file.


[Link]/id_rsa
Step 2: Now copy the keys to your virtual machine
Run the below command on your machine to copy the keys.
ssh-copy-id {username}@{ipaddress}

Step 3: If you want to disable password authentication, open sshd configuration by


running(It is recommended)
sudo vim /etc/ssh/sshd_config
Look for the PasswordAuthentication option and change it to no

Remove the “#” symbol before the PasswordAuthentication (or any option that you wish to
modify) and change it to no. Make sure that the PubkeyAuthentication is set to yes The
authorized keys file shows all the keys that you have generated.
Now restart the ssh service by running the below command:
systemctl restart ssh
When you open the sshd configuration, you will notice many options there. We will discuss
some of them here.
Option 1: Port 22
The port by default is set to 22. If you wish to change the default settings, remove the comments
and enter a port of your choice. It is recommended that you do not use port 22 as anyone trying
the infiltrate your system is most likely to check port 22 for vulnerabilities first.

changing port number


Option 2: AddressFamily
This allows you to configure the type of addresses you want to connect to your server like ssh,
bastion(for linux machine hosted virtually on Microsoft Azure), ipv4, ipv6, etc. The default is
‘Any’ which allows you to connect to your server using any protocol.
Option 3: MaxAuthTries
This allows you to set the maximum limit to wrong password entries. It is essential because it
helps to protect your server against possible brute-force attacks.

Option 4: MaxSessions
This option allows you to enter a limit on the number of sessions that a user can have active.
Just in case the user ever leaks their passwords, this option provides additional security.
Max Auth Tries and Max Sessions- changing the defaults
Option 5: Choosing your desired algorithm
The default algorithm for public and private keys is the RSA algorithm. However, you can
change the type of the algorithm to suit your needs using the following key generation
command:
ssh-keygen -t {put the name of your desired algorithm over here} -b 2048 -C “put any comments
here”
Linux Virtualization : Linux Containers (lxc)
Operating-system-level virtualization is a server virtualization method in which an operating
system’s kernel allows multiple isolated user-space instances, instead of just one. Such
instances, which are sometimes called containers, software containers, virtualization engines
(VEs), or jails (FreeBSD jail or chroot jail), may look and feel like a real server from the point of
view of its owners and users.

The above definition sums up the broad idea about containers, but to be more accurate, the
traditional Virtual Machines used a hypervisor that runs on top of a kernel. This hypervisor
provides virtualization to the applications that run on it by monitoring their resource usage and
access patterns. This causes a lot of overhead resulting in unnecessary loss of performance.
On the other hand, Operating-system-level virtualization works differently. It uses namespaces
and cgroups to restrict the application’s capabilities including the use of resources. This is a
feature provided by the Linux kernel. This has almost no overhead.

cgroups

This method is so effective that Docker is using these containers internally to provide that
isolated environment which is very useful for deploying multiple integrated systems. They are
even bound towards creating their own containers library. Google have their own services
running on containers on shared hardware.
Install `lxc` in Linux Operating System
To install lxc in Ubuntu,
$ sudo apt-get install lxc lxctl lxc-templates
This package installs LXC’s requirements, some templates and also sets up the network
structure for the containers.
Run `lxc-checkconfig` to check if the kernel configuration is ready.
$ sudo lxc-checkconfig
Kernel configuration not found at /proc/[Link]; searching...
Kernel configuration found at /boot/config-4.4.0-24-generic
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
Network namespace: enabled
Multiple /dev/pts instances: enabled

--- Control groups ---


Cgroup: enabled
Cgroup clone_children flag: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: enabled
Cgroup cpuset: enabled

--- Misc ---


Veth pair device: enabled
Macvlan: enabled
Vlan: enabled
Bridges: enabled
Advanced netfilter: enabled
CONFIG_NF_NAT_IPV4: enabled
CONFIG_NF_NAT_IPV6: enabled
CONFIG_IP_NF_TARGET_MASQUERADE: enabled
CONFIG_IP6_NF_TARGET_MASQUERADE: enabled
CONFIG_NETFILTER_XT_TARGET_CHECKSUM: enabled
FUSE (for use with lxcfs): enabled

--- Checkpoint/Restore ---


checkpoint restore: enabled
CONFIG_FHANDLE: enabled
CONFIG_EVENTFD: enabled
CONFIG_EPOLL: enabled
CONFIG_UNIX_DIAG: enabled
CONFIG_INET_DIAG: enabled
CONFIG_PACKET_DIAG: enabled
CONFIG_NETLINK_DIAG: enabled
File capabilities: enabled

Note: Before booting a new kernel, you can check its configuration usage:
“CONFIG=/path/to/config /usr/bin/lxc-checkconfig”
You should also see the output something similar to the above.
lxc provides a lot of ready templates, which are really helpful for fast deployment.
$ ls -l /usr/share/lxc/templates/
total 404
-rwxr-xr-x 1 root root 12973 May 18 14:48 lxc-alpine
-rwxr-xr-x 1 root root 13713 May 18 14:48 lxc-altlinux
-rwxr-xr-x 1 root root 11090 May 18 14:48 lxc-archlinux
-rwxr-xr-x 1 root root 12159 May 18 14:48 lxc-busybox
-rwxr-xr-x 1 root root 29503 May 18 14:48 lxc-centos
-rwxr-xr-x 1 root root 10374 May 18 14:48 lxc-cirros
-rwxr-xr-x 1 root root 19732 May 18 14:48 lxc-debian
-rwxr-xr-x 1 root root 17890 May 18 14:48 lxc-download
-rwxr-xr-x 1 root root 49600 May 18 14:48 lxc-fedora
-rwxr-xr-x 1 root root 28384 May 18 14:48 lxc-gentoo
-rwxr-xr-x 1 root root 13868 May 18 14:48 lxc-openmandriva
-rwxr-xr-x 1 root root 15932 May 18 14:48 lxc-opensuse
-rwxr-xr-x 1 root root 41720 May 18 14:48 lxc-oracle
-rwxr-xr-x 1 root root 11205 May 18 14:48 lxc-plamo
-rwxr-xr-x 1 root root 19250 May 18 14:48 lxc-slackware
-rwxr-xr-x 1 root root 26862 May 18 14:48 lxc-sparclinux
-rwxr-xr-x 1 root root 6862 May 18 14:48 lxc-sshd
-rwxr-xr-x 1 root root 25602 May 18 14:48 lxc-ubuntu
-rwxr-xr-x 1 root root 11439 May 18 14:48 lxc-ubuntu-cloud
 We’ll start by creating a new container with the name “my_container” with “ubuntu”
template.
 This will take some time and finish creating a container for you. Yes! It’s that simple.
 Once it’s completed, the last few lines show the password for the root user of the
container. It would look something similar to this,
$ sudo lxc-create -n my_container -t ubuntu
.....
.....
##
# The default user is 'ubuntu' with password 'ubuntu'!
# Use the 'sudo' command to run tasks as root in the container.
##
We can check the status of container using lxc-ls. This will show the container to be in a
stopped state.
$ sudo lxc-ls --fancy
NAME STATE IPV4 IPV6 AUTOSTART
----------------------------------------------
my_container STOPPED - - NO
Now to start the container run lxc-start. The -d argument creates a daemon.
$ sudo lxc-start -n my_container -d
Check the status of container using lxc-ls to verify its running. We can access the console using
lxc-console. Use the credentials we received above to get the console access.
$ sudo lxc-console -n my_container
After logging in, run the following command on the container,
$ top
And on the host-pc run the following command to see the list of running processes.
$ ps auxf
and somewhere you’ll find a process tree that looks similar to this,

It would be surprising, but all the processes on the container are just simple processes on the
host pc. The important part is that all are isolated and monitored by kernel. Hence you can think
of these as simple processes on the host PC and you can even kill them (only if you have
sufficient privileges)

You can exit the console and return to the host by typing Ctrl-A followed by Q.

To get more info about the running container use,


$ sudo lxc-info -n my_container

You can access the root filesystem of this container directly from the host machine by
accessing. You will need root permission to do so.
$ sudo su
$ cd /var/lib/lxc/my_container/rootfs
That’s it. Now this is like a brand-new operating system. You can run any service on this
container.
Think of containers as separate operating systems, where you can run anything, you want. The
only thing that makes it special is that all containers run on the same hardware. So, practically,
companies/institutions buy a heavy shared machine then deploy containers with resource limits
according to the multiple services they want. This makes it scalable and easier to manage.

To stop the container run,


$ sudo lxc-stop -n my_container
To delete the container use,
$ sudo lxc-destroy -n my_container
NOTE: lxc provides a wrapper and easy to use API to use the kernel features. It is not
equivalent to containers in any sense.
Read the documentation to get more details on the working of containers. There are a lot of
commands that are really helpful and make it easier to set up containers.
How to Manage Linux Containers using LXC
LXC is a technology for creating light weighted containers on your system. This technology is
able to run on more than one Linux host. It is just like its counterparts i.e the other hypervisors
like KVM and Xen. Unlike complete virtualization, containerization does not provide the user
with complete isolation but the amount of load that is exerted on the Host OS is considerably
less.
In this article, we will see the Management of Linux containers with the help of LXC, the Linux
distribution that will be used in Ubuntu.
Installing LXC on the system
Use the below command to install LXC on your system:
sudo apt-get install lxc
Unfortunately, the above command does not download all the templates, so we might run into
an error named No such file or directory – bad template while creating the container. To make
sure we have the templates installed run the below command:
sudo apt-get install lxc-templates

Creation, Listing, login, and stopping of the container:


Step 1: Creation
A new container can be created by using the below command, we will be creating a sample
container named sample1 which will be based on an ubuntu template :
sudo lxc-create -t <name_of_template> -n <name_of_container>

This process takes a while because there are a lot of packages to be collected and the creation
of a container is also time taking.
Step 2: Listing the container(s)
Use the below command to list the available containers on your system:
sudo lxc-ls

By default the user created is ubuntu and the password is also ubuntu.

To see the complete details about the container use the below command:
sudo lxc-info -n <name_of_container>

Step 3: Starting the container


To start the container use the following command:
sudo lxc-start -n <name_of_container>
or
sudo lxc-start -d -n <name_of_container> (To start the container in a detached mode)
To verify the running status of the container use the below command:
sudo lxc-info -n <name_of_container>

Step 4: Logging into the container


To log in to the container we take the help of the lxc-console command:
sudo lxc-console -n <name_of_container>

To stop the container use the below command:


sudo lxc-stop -n <name_of_container>

Verify it:
sudo lxc-info -n <name_of_container>
Step 5: Freezing and Unfreezing
With the help of the lxc-freeze command we can freeze the containers:
sudo lxc-freeze -n <name_of_container>

Confirm whether the container is stopped or not:

You can see the FROZEN state.


To unfreeze the frozen container use the below command:
sudo lxc-unfreeze -n <name_of_container>
Step 6: Cloning the container and its power off
Before we clone a container make sure to stop it using the command discussed above, then
type the below command to make a clone of it. The name of our cloned container will be
sample2.
Note: Earlier the command had lxc-clone instead of lxc-copy, lxc-clone is now deprecated.
sudo lxc-copy -n <old container> -N <new container>
-n —-> old name, -N—-> new name

The clone of the container can also be seen along with the existing container.
Step 7: Powering off the container
To power off the container use the below command:
sudo poweroff
Verify it once the container is stopped:

Step 8: Take snapshots of a container and its deletion


To take a snapshot of a container use the lxc-snapshot command:
sudo lxc-snapshot -n <name_of_container>

To delete a container use the below command:


sudo lxc-destroy -n <name_of_container>

Management of LXC Containers using a web panel:


Some people find working with the command line a bit tedious, this method is just for them. By
installing the web panel of LXC one can manage the containers with the help of GUI.
Note: For installing the web panel you must be a root user.
sudo su
wget [Link] -O - | bash
The user interface can be accessed at the Url: http:/your_ip_address:5000/ by using the user
id and password which by default are admin and admin.

Go to the site and enter the details:

You might also like