Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

LINUX SYSTEM PROGRAMMING

Chapter one: General Overview

Linux is an open-source operating system. It is like Windows, Mac, Android, etc.

Unix is also an operating system like Linux. It is a commercial OS. It consists of three parts: Kernel, Shell
and Programs. Most of the Unix and Linux commands are similar in nature.

This course includes all topics of Linux OS such as Linux commands, Directories, Files, Man Pages, File
Contents, File Permissions, shells, VI editor etc. There is also given Linux interview questions to help you
better understand the Linux operating system.

What Is Linux
Linux is an open-source operating system like other operating systems such as Microsoft Windows

, Apple Mac OS, iOS, Google android, etc. An operating system is a software that enables the communication
between computer hardware and software. It conveys input to get processed by the processor and brings output
to the hardware to display it. This is the basic function of an operating system. Although it performs many other
important tasks, let's not talk about that.

Linux is around us since the mid-90s. It can be used from wristwatches to supercomputers. It is
everywhere in our phones, laptops, PCs, cars and even in refrigerators. It is very much famous among
developers and normal computer users.

Evolution of Linux OS
The Linux OS

was developed by Linus Torvalds in 1991, which sprouted as an idea to improve the UNIX OS. He suggested
improvements but was rejected by UNIX designers. Therefore, he thought of launching an OS, designed in a way
that could be modified by its users.

Nowadays, Linux is the fastest-growing OS. It is used from phones to supercomputers by almost all
major hardware devices.

Structure Of Linux Operating System


An operating system is a collection of software, each designed for a specific function.

Linux OS has following components:

Linux System Programming, Instructor: Tante Henry S. Page 1 of 38


1) Kernel
Linux kernel is the core part of the operating system. It establishes communication between devices
and software. Moreover, it manages system resources. It has four responsibilities:

Linux System Programming, Instructor: Tante Henry S. Page 2 of 38


o device management: A system has many devices connected to it like CPU, a memory device,
sound cards, graphic cards, etc. A kernel stores all the data related to all the devices in the device
driver (without this kernel won't be able to control the devices). Thus kernel knows what a device
can do and how to manipulate it to bring out the best performance. It also manages
communication between all the devices. The kernel has certain rules that have to be followed by
all the devices.
o Memory management: Another function that kernel has to manage is the memory
management. The kernel keeps track of used and unused memory and makes sure that processes
shouldn't manipulate data of each other using virtual memory addresses.
o Process management: In the process, management kernel assigns enough time and gives
priorities to processes before handling CPU to other processes. It also deals with security and
ownership information.
o Handling system calls: Handling system calls means a programmer can write a query or ask the
kernel to perform a task.

2) System Libraries
System libraries are special programs that help in accessing the kernel's features. A kernel has to be
triggered to perform a task, and this triggering is done by the applications. But applications must know
how to place a system call because each kernel has a different set of system calls. Programmers have
developed a standard library of procedures to communicate with the kernel. Each operating system
supports these standards, and then these are transferred to system calls for that operating system.

The most well-known system library for Linux is Glibc (GNU C library).

3) System Tools
Linux OS has a set of utility tools, which are usually simple commands. It is a software which GNU project
has written and publish under their open source license so that software is freely available to everyone.

With the help of commands, you can access your files, edit and manipulate data in your directories or
files, change the location of files, or anything.

4) Development Tools
With the above three components, your OS is running and working. But to update your system, you
have additional tools and libraries. These additional tools and libraries are written by the programmers
and are called toolchain. A toolchain is a vital development tool used by the developers to produce a
working application.

Linux System Programming, Instructor: Tante Henry S. Page 3 of 38


5) End User Tools
These end tools make a system unique for a user. End tools are not required for the operating system
but are necessary for a user.

Some examples of end tools are graphic design tools, office suites, browsers, multimedia players, etc.

Why use Linux?


This is one of the most asked questions about Linux systems. Why do we use a different and bit complex
operating system, if we have a simple operating system like Windows? So there are various features of
Linux

systems that make it completely different and one of the most used operating systems. Linux may be a perfect
operating system if you want to get rid of viruses, malware, slowdowns, crashes, costly repairs, and many more.
Further, it provides various advantages over other operating systems
, and we don't have to pay for it. Let's have a look at some of its special features that will attract you to switch
your operating system.

Free & Open Source Operating System


Most OS come in a compiled format means the main source code has run through a program called a
compiler that translates the source code into a language that is known to the computer.

Modifying this compiled code is a tough job.

On the other hand, open-source is completely different. The source code is included with the compiled
version and allows modification by anyone having some knowledge. It gives us the freedom to run the
program, freedom to change the code according to our use, freedom to redistribute its copies, and
freedom to distribute copies, which are modified by us.

In short, Linux is an operating system that is "for the people, by the people."

Linux System Programming, Instructor: Tante Henry S. Page 4 of 38


And we can dive in Linux without paying any cost. We can install it on Multiple machines without paying
any cost.

It is secure
Linux supports various security options that will save you from viruses, malware, slowdowns, crashes.
Further, it will keep your data protected. Its security feature is the main reason that it is the most
favorable option for developers. It is not completely safe, but it is less vulnerable than others. Each
application needs to authorize by the admin user. The virus cannot be executed until the administrator
provides the access password. Linux systems do not require any antivirus program.

Favorable choice of Developers


Linux is suitable for the developers, as it supports almost all of the most used programming languages
such as C

/C++
, Java
, Python
, Ruby
, and more. Further, it facilitates with a vast range of useful applications for development.

Developers find that the Linux terminal is much better than the Windows command line, So, they prefer
terminal over the Windows command line. The package manager on Linux system helps programmers
to understand how things are done. Bash scripting

is also a functional feature for the programmers. Also, the SSH support helps to manage the servers quickly.

A flexible operating system


Linux is a flexible OS, as, it can be used for desktop applications, embedded systems, and server
applications. It can be used from wristwatches to supercomputers. It is everywhere in our phones,
laptops, PCs, cars and even in refrigerators. Further, it supports various customization options.

Linux Distributions
Many agencies modified the Linux operating system and makes their Linux distributions. There are
many Linux distributions available in the market. It provides a different flavor of the Linux operating
system to the users. We can choose any distribution according to our needs. Some popular distros are
Ubuntu, Fedora, Debian, Linux Mint, Arch Linux, and many more.

For the beginners, Ubuntu and Linux Mint are considered useful and, for the proficient developer,
Debian and Fedora would be a good choice. To Get a list of distributions, visit Linux Distributions

.
Linux System Programming, Instructor: Tante Henry S. Page 5 of 38
How does Linux work?
Linux is a UNIX-like

operating system, but it supports a range of hardware devices from phones to supercomputers. Every Linux-based
operating system has the Linux kernel and set of software packages to manage hardware resources.

Also, Linux OS includes some core GNU tools to provide a way to manage the kernel resources, install
software, configure the security setting and performance, and many more. All these tools are packaged
together to make a functional operating system.

How to use Linux?


We can use Linux through an interactive user interface as well as from the terminal (Command Line
Interface). Different distributions have a slightly different user interface but almost all the commands
will have the same behavior for all the distributions. To run Linux from the terminal, press the
"CTRL+ALT+T" keys. And, to explore its functionality, press the application button given on the left
down corner of your desktop.

Advantages of Linux
Linux is an open-source operating system like Windows and MacOS. It is not just limited to the
operating system, but nowadays, it is also used as a platform to run desktops, servers, and embedded
systems. It provides various distributions and variations as it is open source and has a modular design.
The kernel is a core part of the Linux system.

Linux system is used to manage various services such as process scheduling, application scheduling,
basic peripheral devices, file system, and more. Linux provides various advantages over other
operating systems such as Windows and macOS. So, it is used in almost every field, from cars to home
appliances and smartphones to servers (supercomputers).

In this section, we will see some major advantages of the Linux system. Further, we will see the
advantages of Linux over other operating systems and will determine why it is better than other
operating systems.

Why is Linux better than other operating systems?


There are many features of the Linux operating system that demonstrate that it is better than other
operating systems. However, in some prospective other operating systems can be more useful than
Linux. Let's see the top 20 advantages of Linux OS.

op 20 Advantages of Linux
Linux System Programming, Instructor: Tante Henry S. Page 6 of 38
Following are top 20 advantages of the Linux operating system:

1. pen Source
As it is open-source, its source code is easily available. Anyone having programming knowledge can
customize the operating system. One can contribute, modify, distribute, and enhance the code for any
purpose.

2. Security
The Linux security feature is the main reason that it is the most favorable option for developers. It is not
completely safe, but it is less vulnerable than others. Each application needs to authorize by the admin
user. The virus is not executed until the administrator provides the access password. Linux systems do
not require any antivirus program.

3. Free
Certainly, the biggest advantage of the Linux system is that it is free to use. We can easily download it,
and there is no need to buy the license for it. It is distributed under GNU GPL (General Public License).
Comparatively, we have to pay a huge amount for the license of the other operating systems.

4. Lightweight

Linux System Programming, Instructor: Tante Henry S. Page 7 of 38


Linux is lightweight. The requirements for running Linux are much less than other operating systems. In
Linux, the memory footprint and disk space are also lower. Generally, most of the Linux distributions
required as little as 128MB of RAM around the same amount for disk space.

5. Stability
Linux is more stable than other operating systems. Linux does not require to reboot the system to
maintain performance levels. It rarely hangs up or slow down. It has big up-times.

6. Performance
Linux system provides high performance over different networks. It is capable of handling a large
number of users simultaneously.

7. Flexibility
Linux operating system is very flexible. It can be used for desktop applications, embedded systems, and
server applications too. It also provides various restriction options for specific computers. We can install
only necessary components for a system.

8. Software Updates
In Linux, the software updates are in user control. We can select the required updates. There a large
number of system updates are available. These updates are much faster than other operating systems.
So, the system updates can be installed easily without facing any issue.

9. Distributions/ Distros
There are many Linux distributions available in the market. It provides various options and flavors of
Linux to the users. We can choose any distros according to our needs. Some popular distros
are Ubuntu, Fedora, Debian, Linux Mint, Arch Linux, and many more.

For the beginners, Ubuntu and Linux Mint would be useful and, Debian and Fedora would be good
choices for proficient programmers.

10. Live CD/USB


Almost all Linux distributions have a Live CD/USB option. It allows us to try or run the Linux operating
system without installing it.

11. Graphical User Interface


Linux is a command-line based OS but, it provides an interactive user interface like Windows.

Linux System Programming, Instructor: Tante Henry S. Page 8 of 38


12. Suitable for programmers
It supports almost all of the most used programming languages such as C/C++, Java, Python, Ruby,
and more. Further, it offers a vast range of useful applications for development.

The programmers prefer the Linux terminal over the Windows command line. The package manager on
Linux system helps programmers to understand how things are done. Bash scripting is also a functional
feature for the programmers. It also provides support for SSH, which helps in managing the servers
quickly.

13. Community Support


Linux provides large community support. We can find support from various sources. There are many
forums available on the web to assist users. Further, developers from the various opensource
communities are ready to help us.

14. Privacy
Linux always takes care of user privacy as it never takes much private data from the user. Comparatively,
other operating systems ask for the user's private data.

15. Networking
Linux facilitates with powerful support for networking. The client-server systems can be easily set to a
Linux system. It provides various command-line tools such as ssh, ip, mail, telnet, and more for
connectivity with the other systems and servers. Tasks such as network backup are much faster than
others.

16. Compatibility
Linux is compatible with a large number of file formats as it supports almost all file formats.

17. Installation
Linux installation process takes less time than other operating systems such as Windows. Further, its
installation process is much easy as it requires less user input. It does not require much more system
configuration even it can be easily installed on old machines having less configuration.

18. Multiple Desktop Support


Linux system provides multiple desktop environment support for its enhanced use. The desktop
environment option can be selected during installation. We can select any desktop environment such
as GNOME (GNU Network Object Model Environment) or KDE (K Desktop Environment) as both
have their specific environment.
Linux System Programming, Instructor: Tante Henry S. Page 9 of 38
19. Multitasking
It is a multitasking operating system as it can run multiple tasks simultaneously without affecting the
system speed.

20. Heavily Documented for beginners


There are many command-line options that provide documentation on commands, libraries, standards
such as manual pages and info pages. Also, there are plenty of documents available on the internet in
different formats, such as Linux tutorials, Linux documentation project, Serverfault, and more. To help
the beginners, several communities are available such as Ask Ubuntu, Reddit, and StackOverflow.

Linux History
Evolution of Computer
In earlier days, computers were as big as houses or parks. So you can imagine how difficult it was to
operate them. Moreover, every computer has a different operating system which made it completely
worse to operate on them. Every software was designed for a specific purpose and was unable to
operate on other computer. It was extremely costly and normal people neither can afford it nor can
understand it.

Evolution of Unix
In 1969, a team of developers of Bell Labs started a project to make a common software for all the
computers and named it as 'Unix'. It was simple and elegant, used 'C' language instead of assembly
language and its code was recyclable. As it was recyclable, a part of its code now commonly called
'kernel' was used to develop the operating system and other functions and could be used on different
systems. Also its source code was open source.

Initially, Unix was only found in large organizations like government, university, or larger financial
corporations with mainframes and minicomputers (PC is a microcomputer).

Unix Expansion
In eighties, many organizations like IBM, HP and dozen other companies started creating their own
Unix. It result in a mess of Unix dialects. Then in 1983, Richard Stallman developed GNU project with
the goal to make it freely available Unix like operating system and to be used by everyone. But his
project failed in gaining popularity. Many other Unix like operating system came into existence but
none of them was able to gain popularity.

Evolution of Linux

Linux System Programming, Instructor: Tante Henry S. Page 10 of 38


In 1991, Linus Torvalds a student at the university of Helsinki, Finland, thought to have a freely available
academic version of Unix started writing its own code. Later this project became the Linux kernel. He
wrote this program specially for his own PC as he wanted to use Unix 386 Intel computer but couldn't
afford it. He did it on MINIX using GNU C compiler. GNU C compiler is still the main choice to compile
Linux code but other compilers are also used like Intel C compiler.

He started it just for fun but ended up with such a large project. Firstly he wanted to name it as 'Freax'
but later it became 'Linux'.

He published the Linux kernel under his own license and was restricted to use as commercially. Linux
uses most of its tools from GNU software and are under GNU copyright. In 1992, he released the kernel
under GNU General Public License.

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

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

Linux System Programming, Instructor: Tante Henry S. Page 11 of 38


o Open Source: Linux code is freely available to all and is a community based development
project.

Why Use Linux


Linux is completely different from other operating systems in many ways.

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

Linux Bash
The Linux Bash is also known as 'Bourne-again Shell.' It is a command language interpreter for the
Linux based system. It is a replacement of Bourne shell (sh). It was developed under the GNU Project
and written by Brian Fox. Nowadays, Bash is the default user shell of most of the Linux distributions.

The Linux/Unix shell allows us to interact with the Linux system through the commands. It let us invoke
an executable file to create a running process. Moreover, it also allows us to interact with the Linux file
system. It is designed in such a way that we can perform all the Linux operations through Bash.

The Bash is a command language interpreter as well as a programming language. It


supports variables, functions, and flow control, like other programming languages. It can also read
and execute the commands from a file, which is called a shell script.

It offers various functional improvements over Bourne Shell (sh) for both interactive and programming
use. Although many sh scripts can be run by Bash without any change. The Bash contains the following
improvements over sh:

o It provides command-line editing


o It contains unlimited size command history

Linux System Programming, Instructor: Tante Henry S. Page 12 of 38


o It provides Job Control
o It facilitates with Shell Functions and Aliases
o It provides the indexed arrays of unlimited size
o It contains integer arithmetic in any base from 2 to 64.

How to download Bash

It can be downloaded from the official GNU server via both HTTP (http://ftp.gnu.org/gnu/bash/) and
FTP (ftp://ftp.gnu.org/gnu/bash/ ) servers.

People often get confused between bash, shell, and shell script. Let's walk through shell and scripting
to clear a picture of bash, shell, and scripting.

What is Shell
If we are a new Linux user, and we open the terminal, it is assumed that we are well confused as to what
to do with it. Here the Shell comes in the role.

The terminal contains the shell; it allows us to execute the commands to interact with the system. We
can perform various operations such as store and retrieve data, process information, and various other
simple as well as complex tasks.

To open the terminal, press CTRL+ALT+T keys. Perform some basic operations such as date, cal,
ls, and pwd to take a tour with it.

Consider the below image:

Linux System Programming, Instructor: Tante Henry S. Page 13 of 38


As we can see from the above image, the shell allows us to interact with the Linux system. When we
have executed the date and cal command, the shell interacts with the system and retrieves data.

What is Scripting
Suppose we are required to execute some basic commands every day, for example above four
commands. Linux supports a feature called scripting that allows us to execute more than one task at
once. So, it is good to define a script rather than performing repetitive tasks.

To understand how to use Linux script, let's define a script in a combination of some tasks. To define a
script, create a file with a .sh extension. We are using the VI text editor. However, any text editor can be
used to define a script. Consider below command:

1. vi tasks.sh

The above command will open the vi editor in normal mode. Switch it to insert mode by pressing ESC,
and after that enter 'i' keys, enter your desired tasks. Every task should be defined in a new line.
Consider the below tasks:

1. date
2. cal
3. pwd
4. ls

After entering the tasks, press ESC and :wq! Keys to save and exit from the editor.

Linux System Programming, Instructor: Tante Henry S. Page 14 of 38


Now, make the created file as executable, use the chmod command with +x option as follows:

1. chmod +x tasks.sh

We have created our first script. To execute the shell script, execute the file name by predefining './
' with the file name. Consider the below command:

1. ./task.sh

Consider the below output:

As we can see from the above output, by using the shell, we can automate the tasks. We can use this
whenever we need it. It can be used repeatedly any number of times.

Features of Bash
All the built-in command of the sh shell is available in Bash; moreover, it facilitates us with many other
features. Some key features of Bash are as follows:

o Shell Syntax: The shell syntax contains shell operations, quoting, and comments. The shell
operations are the basic operation of the shell. Quoting allows how to remove the special
meaning from characters, and comments are meant to specify the comments.
o Shell commands: Shell commands are the types of commands that you can execute. These
commands can be simple commands, pipelines, lists, compound commands, and more.
o Shell Functions: Shell functions are used to group commands by name. They are executed as
traditional commands. When we use the name of a shell function, the list of commands
associated with that is executed.
Linux System Programming, Instructor: Tante Henry S. Page 15 of 38
o Shell parameters: Basically, a parameter is an entity that stores value; it can be a name, number,
or special character. The shell parameters specify how the shell store value. They can be a
positional parameter or a special parameter. Positional parameters are the shell's command-line
arguments, and the special parameters are denoted by a special character.
o Shell Expansions: Shell expansion is a technique that is used by Bash to expand the parameters.
Expansion is performed on the command line after the input has been splitted into tokens.
o Redirections: It is a way to manage and control the input and output.
o Command execution: It decides how the system will react when we execute a command.
o Shell Scripts: It is a text file that has shell commands and executes them when it is used. Bash
reads and executes the commands then exits.

Unix Vs Linux
Today Linux is in great demand. You can see the use of Linux everywhere. It's dominating on our servers,
desktop, smartphones and even used in some electrical devices like refrigerators.

Some people think Unix and Linux as synonyms, but that's not true. Many operating systems were
developed to be like Unix but none of them got the popularity as Linux. Linux is the clone of Unix. It
has several features similar to Unix, still have some key differences. Before Linux and Windows,
computer world was dominated by Unix. Unix is a copyrighted name and IBM AIX, HP-UX and Sun
Solaris are only Unix operating system remained till date.

Difference between Linux and Unix

Comparison Linux Unix

Definition It is an open-source operating system which It is an operating system which can


is freely available to everyone. be only used by its copyrighters.

Examples It has different distros like Ubuntu, Redhat, Fedora, IBM AIX, HP-UX and Sun Solaris.
etc

Users Nowadays, Linux is in great demand. Anyone can It was developed mainly for servers,
use Linux whether a home user, developer or a workstations and mainframes.
student.

Linux System Programming, Instructor: Tante Henry S. Page 16 of 38


Usage Linux is used everywhere from servers, PC, It is used in servers, workstations
smartphones, tablets to mainframes and and PCs.
supercomputers.

Cost Linux is freely distributed,downloaded, and Unix copyright vendors decide


distributed through magazines also. And priced different costs for their respective
distros of Linux are also cheaper than Windows. Unix Operating systems.

Development As it is open source, it is developed by sharing and Unix was developed by AT&T Labs,
collaboration of codes by world-wide developers. various commercial vendors and
non-profit organizations.

Manufacturer Linux kernel is developed by the community of Unix has three distributions IBM
developers from different parts of the world. AIX, HP-UX and Sun Solaris. Apple
Although the father of Linux, Linus Torvalds also uses Unix to make OSX
oversees things. operating system.

GUI Linux is command based but some distros provide Initially it was command based OS,
GUI based Linux. Gnome and KDE are mostly used but later Common Desktop
GUI. Environment was created. Most
Unix distributions use Gnome.

Interface The default interface is BASH (Bourne Again SHell). It originally used Bourne shell. But is
But some distros have developed their own also compatible with other GUIs.
interfaces.

File system Linux supports more file system than Unix. It also supports file system but
support lesser than Linux.

Coding Linux is a Unix clone,behaves like Unix but doesn't Unix contain a completely different
contain its code. coding developed by AT&T Labs.

Operating Linux is just the kernel. Unix is a complete package of


system Operating system.

Security It provides higher security. Linux has about 60-100 Unix is also highly secured. It has
viruses listed till date. about 85-120 viruses listed till date

Linux System Programming, Instructor: Tante Henry S. Page 17 of 38


Error detection As Linux is open-source,whenever a user post any In Unix, users have to wait for some
and solution kind of threat, developers from all over the world time for the problem to be resolved.
start working on it. And hence, it provides faster
solution.

Linux vs. Windows


Linux vs. Windows has always been one of the most frequently asked questions related to the operating
system. Users often get confused about which one is better for them. There is diversity between the
users as most users prefer the graphical user interface (GUI) and most command-line interface (CLI).
There are many disagreements and acrimonious behavior among users, and it seems that it will be
forever.

In this section, we will discuss the differences between Linux and Windows by considering several
parameters such as performance, usability, security, ease of use, and more to clear a picture of
using both operating systems. Further, we will see the advantages of Linux over other operating
systems such as Windows and Mac OS. This will help you to decide which one is better for you.

Difference between Linux and Windows operating system


To understand the difference between Linux and Windows, let's see a brief introduction to both
operating systems. Later we will talk on their features and security options.

What is the Windows operating system?

Linux System Programming, Instructor: Tante Henry S. Page 18 of 38


Windows is a graphical operating system developed and marketed by Microsoft. It is also referred to
as Microsoft Windows. Several versions of Windows have been introduced in the market; the current
version is Windows 10. The first version of Windows was introduced on November 20, 1985, as a
graphical operating system for MS-DOS.

Microsoft Windows is a family of various operating systems. It comes with two versions, i.e., 64 bit and
32 bit. It facilitates both client and server versions. The latest client version is Windows 10, and the server
version is Windows server 2019.

Windows is a straight forward and simple to use. Generally, it is designed for users having no
programming knowledge. So, mostly it is used for business and alternative industrial purposes.

What is a Linux operating system


Linux is an open-source operating system. As it is open-source, it is special and different from other
operating systems, which means that you can customize it by editing source code. It provides
programming as well as a graphical user interface. Linux is built by Linux Torvalds because he wanted
to create a free operating system kernel that anyone can use.

Linux is a collection of operating systems that are based on Linux kernel. The first version of Linux was
released in the year 1991. The Linux system is most commonly used for servers; however, it is available
in desktop versions as well.

Ubuntu, Devian, and Fedora are some popular Linux distributions. Also, we have SUSE Linux
Enterprise Server (SLES) and RedHat Enterprise Linux for the commercial distribution of Linux. As it
is open-source, we can modify the source code and make variations in the operating system.

Let's discuss some features and parameters to understand the difference between both operating
systems:

o File System

Linux System Programming, Instructor: Tante Henry S. Page 19 of 38


Windows uses different drives such as C, D, E, and more, having some folders to store files.

But Linux uses a tree structure to store and organize files. Linux file structure starts from the root
directory, and it is considered as a start point of the file system. It is represented by a forward slash (/).
In Linux, everything (Directories, devices, and files) is considered a file.

There are three types of files available in a Linux system.

o General files
o Directory files
o Device files

The general file system of Unix is as follows:

Linux System Programming, Instructor: Tante Henry S. Page 20 of 38


General files: General files or ordinary files are files that contain images, text, or a program. These files
are in ASCII text or Binary format. The General files are the most common in any Linux system.

Directory files: Directory files are the depository for other files. A directory can have a subdirectory file
within it. For Windows, we can understand them as folders.

Device files: Windows represents the external devices (Pendrives, hard drives, and CD-ROM) as letters
such as E: F: But, Linux represents devices as files, such as the hard drive's partitions are represented
as dev/sda1, dev/sda2, and more (depends upon the number of partitions). All the device files take
place within the directory /dev.

o Naming Conventions for file

Linux files are case sensitive; therefore, we can have two files having the same name; one in upper case
and other in lower case. Comparatively, the Windows files are not case sensitive; we cannot have two
files with the same name.

o Users

Windows supports four types of users:

o Administrator
o Standard
o Child
o Guest

Comparatively, Linux supports three types of users:

o Regular
o Administrative(root)
o Service

Regular User

In Linux, when we install ubuntu on our system, a regular account user is created. By default, all our files
are saved in the home directory (/home/). A regular user cannot access the other user's directories.

Root User

Apart from the regular user account, a root user account is also created during installation. The root
account is also called as superuser as it can access the restricted files, install the software and other
utility, and has administrative rights. For installing the software or editing the system files or any other
Linux System Programming, Instructor: Tante Henry S. Page 21 of 38
administrative task, we need the root access. For general tasks such as creating files, playing games,
browsing the internet, we don't need the root access.

Service user

The Linux system is widely used as the server operating system. The leading service providers such as
Apache, Squid, email, and more have their service accounts to increase security. Linux service user can
allow or deny access to various resources depending on the service type.

Note: The Ubuntu Desktop version does not have a service account, and the regular accounts are
called standard accounts.
o Home Directory

Linux OS has a separate home directory for a user. The files and directories created by a user are stored
under a particular home directory. A user cannot store the files under any other user's directory, as it is
not allowed to access the other user's directory. For example, the home directory of a user 'Alex' is
automatically created as "/home/alex/" at the time of installation.

The Windows OS also has different home directories for different users. It is as "C:\ documents or
\settings."

o Other Directories

Linux systems have a tree structure to save the directories; comparatively, Windows uses different drives
to save the directories. In Windows, system and program files usually take place in C drive. But in Linux,
the system and program files take place in their specific directory such as software files are stored in
/bin, programs and device files are in /dev, and boot files are stored in /boot directory.

o Kernel

The basic part of any operating system is its kernel. It interacts with hardware devices and performs
other functions such as process management, file handling, and more. Different operating systems have
different kernels.

Linux and Windows have a different kernel. The Linux kernel is monolithic, and it consumes more
running space. Comparatively, Windows uses the microkernel, which consumes less running spaces.
But, Windows running efficiency is less than Linux. The key difference between Linux kernel and
Windows kernel is that Windows is a commercial software while Linux is open-source software.

o Pricing

Linux is an open-source operating system, so almost all the utilities and libraries are entirely free. The
GNU/Linux distribution can be simply downloaded for free. However, some companies are providing
paid support for their Linux distributions, but the underlying software is still free.
Linux System Programming, Instructor: Tante Henry S. Page 22 of 38
Microsoft Windows licensed copy usually cost between $99.00 and $199.00. Microsoft currently stops
providing support for the earlier versions. The latest version, Windows 10, is available at $139.

Let's focus some head to head comparison between Linux and Windows.

Parameter Linux Windows

Access Users can access the source code of Usually, users cannot access the source
kernel in Linux and can alter the kernel code. However, members of some groups
according to need. can have access to it.

Variety Linux has several distributions that are Windows have fewer options to customize.
highly customizable.

Command- The command line usually referred Windows also have a command line, but it is
line to as Terminal, which is the most not such effective as a comparison to the
useful tool of the Linux system. It used Linux terminal. Most users prefer the GUI
for administration and daily tasks. For options for daily tasks.
the end-users, it does not look so
effective.

Installation The Linux installation process is a bit Windows OS is easy to install and set up on
complicated to set up as it requires a machine; it requires fewer user input
many user inputs. It takes less time options during installation. However, it
than Windows to install. takes more time to install as compared to
Linux.

Ease of use The Linux OS is meant to be for the Windows comes with simple and rich GUI
technical user because you must have options, so it is easy to use it. It can be
some exposure to various Linux simply used by technical as well as non-
commands. Users may take more time technical users. The troubleshooting
to be a handy user of Linux. The process is also much easy than Linux.
troubleshooting process is also
complicated as compared to Windows.

Written in Linux is written in assembly Windows is written in C++ and assembly


language and C. language.

Reliability Linux is highly reliable and secure. It Windows is not as much reliable as Linux.
has well-established system security, However, now Windows has improved
process management, and uptime. reliability but still has some security
weaknesses and system instabilities.

Linux System Programming, Instructor: Tante Henry S. Page 23 of 38


Support Linux has a Windows also provide good support to its
user. It provides free as well as paid support.
good support as it has a huge It has an easily accessible online forum.
community of user forums and online
search.

Update Linux provides full control to its users Windows updates are annoying. The
on updates. A user can install the updates will come at any time and take too
update whenever needed. Also, it much time to install. Sometimes, you power
takes less time to install an update. on your machine, and updates are
automatically getting started.
Unfortunately, the user does not have much
control over updates.

Security Linux OS is more secure than Windows is less secure than Linux. Attackers
Windows. It is hard for the hackers and primarily target the Windows for malware
attackers to find a loophole in it. So, and virus. Windows is most vulnerable
Linux is hard to breakthrough. without anti-virus.

License Linux is distributed under Windows is distributed under a Proprietary


the GPL(GNU General Public commercial software license.
License) license.

Linux Distributions (Distros)


Other operating systems like Microsoft combine each bit of codes internally and release it as a single
package. You have to choose from one of the version they offer.

But Linux is different from them. Different parts of Linux are developed by different organizations.

Different parts include kernel, shell utilities, X server, system environment, graphical programs, etc. If
you want you can access the codes of all these parts and assemble them yourself. But its not an easy
task seeking a lot of time and all the parts has to be assembled correctly in order to work properly.

From here on distribution (also called as distros) comes into the picture. They assemble all these parts
for us and give us a compiled operating system of Linux to install and use.

Linux Distributions List


There are on an average six hundred Linux distributors providing different features. Here, we'll discuss
about some of the popular Linux distros today.
Linux System Programming, Instructor: Tante Henry S. Page 24 of 38
1) Ubuntu
It came into existence in 2004 by Canonical and quickly became popular. Canonical wants Ubuntu to
be used as easy graphical Linux desktop without the use of command line. It is the most well known
Linux distribution. Ubuntu is a next version of Debian and easy to use for newbies. It comes with a lots
of pre-installed apps and easy to use repositories libraries.

Earlier, Ubuntu uses GNOME2 desktop environment but now it has developed its own unity desktop
environment. It releases every six months and currently working to expand to run on tablets and
smartphones.

2) Linux Mint
Mint is based on Ubuntu and uses its repository software so some packages are common in both.

Earlier it was an alternative of Ubuntu because media codecs and proprietary software are included in
mint but was absent in Ubuntu. But now it has its own popularity and it uses cinnamon and mate
desktop instead of Ubuntu's unity desktop environment.

3) Debian
Debian has its existence since 1993 and releases its versions much slowly then Ubuntu and mint.

This makes it one of the most stable Linux distributor.

Ubuntu is based on Debian and was founded to improve the core bits of Debian more quickly and make
it more user friendly. Every release name of Debian is based on the name of the movie Toy Story.

Red Hat Enterprise / CentOS


Red hat is a commercial Linux distributor. There products are red hat enterprise Linux (RHEL) and Fedora
which are freely available. RHEL is well tested before release and supported till seven years after the
release, whereas, fedora provides faster update and without any support.

Red hat uses trademark law to prevent their software from being redistributed. CentOS is a community
project that uses red hat enterprise Linux code but removes all its trademark and make it freely available.
In other words, it is a free version of RHEL and provide a stable platform for a long time.

5) Fedora
It is a project that mainly focuses on free software and provides latest version of software. It doesn't
make its own desktop environment but used 'upstream' software. By default it has GNOME3 desktop
environment. It is less stable but provides the latest stuff.

Linux System Programming, Instructor: Tante Henry S. Page 25 of 38


Choosing a Linux Distro

Distribution Why To Use

UBuntu It works like Mac OS and easy to use.

Linux mint It works like windows and should be use by new comers.

Debian It provides stability but not recommended to a new user.

Fedora If you want to use red hat and latest software.

Red hat enterprise To be used commercially.

CentOS If you want to use red hat but without its trademark.

OpenSUSE It works same as Fedora but slightly older and more stable.

Arch Linux It is not for the beginners because every package has to be installed by yourself.

Linux Licensing
Linus Torvalds has given linux kernel license to GNU General Public License (GPL) version 2. GNU make
sure that any software source code licensed under it have to make originating source code open and
freely availble to all its users. Here, freely doesn't mean by cost but it means that it is freely available to
users to distribute and modify the code.

There is the third version of GNU, GNU Lesser General Public License (LGPL) version 3. But it imposes
some more permissions on the license. Torvalds doesn't like some provisions in version 3 and have
announced that linux kernel will not come under version 3.

Linux Set Environment Variable


The environment variables are dynamic values that are stored within a system and used by applications
launched in shells or sub-shells. These variables have a name and their respected value. The
environment variable customizes the system performance and the behavior of an application.

Linux System Programming, Instructor: Tante Henry S. Page 26 of 38


The environment is the track for a computer application to interact with the system. The environment
variable can have information about the default applications of the system, the system locale, the path
of the executable file and the keyboard layout setting, and more. The environment variable makes an
app available as per the system.

Common Environment Variables


Some standard environment variables are as follows:

o PATH

This variable contains a list of directories in which our system looks for files. It separates directories by
a (:) colon.

o USER

This variable holds the username.

o HOME

This variable holds the default path to the user's home directory.

o EDITOR

This variable contains the path to the specified editor.

o UID

This variable contains the path to the user's unique id.

o TERM

This variable contains the path to the default terminal emulator.

o SHELL

This variable contains the path to the default shell that is being used by the user.

o ENV

This variable displays all the environment variable.

How to set Environment Variable in Linux?


Linux System Programming, Instructor: Tante Henry S. Page 27 of 38
There are multiple commands in Linux that allow us to set and create the environment variable.

Use the export command to set a new environment variable.

To create a new variable, use the export command followed by a variable name and its value.

Syntax:

1. export NAME=VALUE

To create a new variable say new_variable, execute the command as follows:

1. export new_variable=10

The echo command is used to display the variable:

1. echo new_variable

To display the value of the variable, use the $ symbol before the variable name:

1. echo $new_variable

Consider the below output:

To set Java Environment Variable, execute the command as follows:

1. export PATH=$PATH:/home/jdk1.8/bin/

We can also create a user to define a variable by directly declaring it on the terminal.

If we want to create a variable new_variable2, we can create it as follows:

1. new_variable2=100

Consider the below output:

Linux System Programming, Instructor: Tante Henry S. Page 28 of 38


Accessing the value of Environment Variable
To access the value of a variable, execute the echo command as follows:

1. echo $variable name


Note: The variables are case sensitive; we cannot use any variable name 'new_variable' as
'New_variable.'

The env command


The env command is used to display all the available variables in the system.

1. env

Output:

SHELL=/bin/bash
SESSION_MANAGER=local/javatpoint-GB-BXBT-2807:@/tmp/.ICE-unix/1458,unix/javatpoint-GB-
BXBT-2807:/tmp/.ICE-unix/1458
COLORTERM=truecolor
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
XDG_MENU_PREFIX=gnome-
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GTK_IM_MODULE=ibus
LANGUAGE=en_IN:en
QT4_IM_MODULE=ibus
MANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.path
GNOME_SHELL_SESSION_MODE=ubuntu
SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
XMODIFIERS=@im=ibus
DESKTOP_SESSION=ubuntu
SSH_AGENT_PID=1362
GTK_MODULES=gail:atk-bridge
PWD=/home/javatpoint
LOGNAME=javatpoint
XDG_SESSION_DESKTOP=ubuntu
XDG_SESSION_TYPE=x11
GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1
XAUTHORITY=/run/user/1000/gdm/Xauthority
GJS_DEBUG_TOPICS=JS ERROR;JS LOG
WINDOWPATH=2
HOME=/home/javatpoint
USERNAME=javatpoint
IM_CONFIG_PHASE=1
LANG=en_IN
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01
:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=0
1;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:
*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=0
1;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.
bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01
;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*
.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=0
1;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01
;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:
Linux System Programming, Instructor: Tante Henry S. Page 29 of 38
*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mp
eg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=
01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:
*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=0
1;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*
.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.og
g=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
XDG_CURRENT_DESKTOP=ubuntu:GNOME
VTE_VERSION=5802
GNOME_TERMINAL_SCREEN=/org/gnome/Terminal/screen/dc6e48ed_7868_43d5_a086_fd6d6a90a74a
INVOCATION_ID=fadf556d1cf5422ea55b247bf57c3c99
MANAGERPID=1252
CLUTTER_IM_MODULE=ibus
GJS_DEBUG_OUTPUT=stderr
LESSCLOSE=/usr/bin/lesspipe %s %s
XDG_SESSION_CLASS=user
TERM=xterm-256color
DEFAULTS_PATH=/usr/share/gconf/ubuntu.default.path
LESSOPEN=| /usr/bin/lesspipe %s
USER=javatpoint
GNOME_TERMINAL_SERVICE=:1.216
DISPLAY=:0
SHLVL=1
QT_IM_MODULE=ibus
XDG_RUNTIME_DIR=/run/user/1000
JOURNAL_STREAM=9:35179
XDG_DATA_DIRS=/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/g
ames:/snap/bin
GDMSESSION=ubuntu
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
_=/usr/bin/env
javatpoint@javatpoint-GB-BXBT-2807:~$

Removing an Environment Variable


By removing an environment variable we can remove all existing component of particular variable.

To remove an environment variable, execute the unset command followed by variable name:

1. unset variable name

The above command will delete the specified variable and its components from the system.

To remove a variable new_variable from the system, execute the command as follows:

1. unset new_variable

Consider the below output:

Linux System Programming, Instructor: Tante Henry S. Page 30 of 38


Linux Set Command
Linux set command is used to set and unset certain flags or settings within the shell environment. These
flags and settings determine the behavior of a defined script and help in executing the tasks without
facing any issue. The values of shell attributes and parameters can be changed or displayed by using
the set command.

Syntax:

1. set [options]

To know about how to set the environment variable in Linux, visit Here.

Options: The supported options by the set command are as following:

o -a: It is used to mark variables that are modified or created for export.
o -b: It is used to notify of job termination immediately.
o -e: It is used to exit immediately if a command exits with a non-zero status.
o -f: It is used to disable the file name generation (globbing).
o -h: It is used to save the location of commands where they looked up.
o -k: It is used to place all assignment arguments in the environment variable of a command, except those
that precede the command name.
o -m: It is used to enable Job control.
o -n: It is used to read commands.
o -o: It is used for option-name.
o -p: It is used to disable the processing of the '$ENV' file and import shell functions. It is turned on
whenever the real and effective user ids do not match. Turning off this option may cause the working uid
and gid to be set as the authorized uid and gid.
o -t: It is used to exit from the command after executing one command.
o -u: It is used to treat unset variables as an error when substituting.
o -v: It is used to print shell input lines.
o -x: It is used to print commands and their arguments in a sequential way (as they are executed).
o -B: It is used to perform brace expansion by the Shell.
o -C: It is used to disallow existing regular files to be overwritten by redirection of output.
o -E: It is used if the ERR trap is inherited by the shell functions.
Linux System Programming, Instructor: Tante Henry S. Page 31 of 38
o -H: It is used to enable style history substitution. By default, it is on when the shell is interactive.
o -P: It is used if we do not want to follow symbolic links when executing commands.
o -T: If this flag is set, the DEBUG trap is inherited by the shell functions.

Note: To turn off the above flags, use the '+' rather than '-' symbol.

To better understand the set command, let's see a brief introduction of the shell variable:

Shell Variable
A variable is a character string that can hold a value. The assigned value could be anything such as
number, filename, text, or any other data type. It is just like a pointer to the actual data. The shell allows
us to create, delete, and assign variables.

The variable name can have any letters, numbers, or underscore (_) character. We cannot have the
characters such as "! *, or -", because these special characters have other meanings for the shell.
According to Unix naming convention, the Unix shell variable must have their names in UPPERCASE.

Examples of the set command


Turn debugging information on or off.

The '-x' option is used with the set command to show command and their arguments. It is useful for
debugging the shell script.

Execute the following command:

To turn on the debugging information:

1. set -x

To turn off the debugging information:

1. set +x

Disable Bash's default behavior.

To disable the default behavior of Bash, execute the command as follows:

1. set -C

Stop a script immediately.

To stop a script immediately, execute the following command:


Linux System Programming, Instructor: Tante Henry S. Page 32 of 38
1. set -e

Getting Help
If you get stuck during the use of the set command, you can access the help documentation from your
terminal. To access the help manual, execute the below command:

1. set --help

Linux export Command


The export command is a built-in utility of Linux Bash shell. It is used to ensure the environment
variables and functions to be passed to child processes. It does not affect the existing environment
variable.

Environment variables are set when we open a new shell session. At any time, if we change any variable
value, the shell has no way to select that change. The export command allows us to update the current
session about the changes that have been made to the exported variable. We do not need to wait to
start a new shell session.

Syntax:

1. export [-f] [-n] [name[=value] ...] or export -p

Let's have a look at various examples of the export command:

Example1: The export command without any argument

The basic export command will display all the exported environment variables of your system. It is
executed as follows:

1. export

Consider the below snap of output:

Linux System Programming, Instructor: Tante Henry S. Page 33 of 38


Example2: Display all exported variable on current shell

To display all the exported environment variable of the current shell, execute the command with -p
option as follows:

1. export -p

Consider the below snap of output:

Linux System Programming, Instructor: Tante Henry S. Page 34 of 38


Example3: Using export with functions

To use a function with the export command, use the -f option. If we do not use this option, it will be
considered as a variable, not function.

Syntax:

1. export -f function_name

We are exporting a function 'name' as follows:

1. name() { echo "Javatpoint";}

To export the above function, execute the command as follows:

1. export -f name

Now, invoke the bash shell to execute the function:

Linux System Programming, Instructor: Tante Henry S. Page 35 of 38


1. bash

To call the function, enter the function name:

1. name

Consider the below output:

let's create another function 'hello,' execute the command as follows:

1. function hello
2. > {
3. > echo hello, welcome to javatpoint
4. > }

To export the above function, execute the command as follows:

1. export -f hello

Consider the below output:

Example4: Assign a value before exporting a function or variable:

The export command allows us to assign a value before exporting a function. Consider the below
command:

1. export name[=value]

For example, assign a value to a variable as follows:

1. a=5

now export it as:

1. export a
Linux System Programming, Instructor: Tante Henry S. Page 36 of 38
we can verify the assignment by using the printenv command as follows:

1. printenv a

Consider the below output:

Example5: Set vim as default editor:

The vim editor is the most widely used text editor for the Linux systems. We can set the vim as default
text editor by using the export command.

To set the vim as a default text editor, execute the following command:

1. export EDITOR=/usr/bin/vim
2. export | grep EDITOR

The above commands will not show any confirmation. Consider the below output:

Example6: Set an environment variable

To create a new variable, use the export command followed by a variable name and its value.

Syntax:

1. export NAME=VALUE

To create a new variable, 'sys,' execute the command as follows:

1. export sys=50

The echo command is used to display the variable:

1. echo sys

To display the value of the variable, use the $ symbol before the variable name

Linux System Programming, Instructor: Tante Henry S. Page 37 of 38


1. echo $sys

Consider the below output:

Linux System Programming, Instructor: Tante Henry S. Page 38 of 38

You might also like