Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
127 views

Linux Unit I

The document discusses Linux operating system including its history, architecture, components, kernel mode vs user mode, and advantages of using Linux. It explains that Linux is an open-source OS like other OS and describes its evolution. It also discusses Linux architecture, kernel, system libraries, system utilities, hardware layer, and shell.

Uploaded by

Abhishek Dhaka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views

Linux Unit I

The document discusses Linux operating system including its history, architecture, components, kernel mode vs user mode, and advantages of using Linux. It explains that Linux is an open-source OS like other OS and describes its evolution. It also discusses Linux architecture, kernel, system libraries, system utilities, hardware layer, and shell.

Uploaded by

Abhishek Dhaka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

3CS4-06: Linux and Shell Programming

Credit:3 Max. Marks: 100 (IA:30, ETE:70) 3L+0T+ 0P End Term Exams: 3 Hours
Course Objectives:
1. Understand the Linux architecture, features and Commands.
2. Understand Linux file systems, shell basics, and shell environments
3. Understand Shell Programming and to write shell scripts.
4. Understand Linux utilities, software installation and source code management.

Course Outcomes:

CO-1: Explain multi user Linux OS and its features


CO-2: Interpret Linux Commands, Shell basics, and shell environments
CO-3: Design and develop shell programs, communication, System calls.
CO-4: Handling installation of software for Linux based OS with source code management

UNIT I - INTRODUCTION TO LINUX AND LINUX UTILITIES: history, architecture, and


features of LINUX, Linux commands- sudo, man, echo, printf, script, passwd, uname, who,date,
stty, pwd, cd, mkdir, rmdir, ls, cp, mv, rm, cat, more, wc, lp, od, tar, gzip, networking commands
-unlink, du, df, mount, umount, find, unmask, ulimit, ps, w, finger, arp, ftp, telnet, rlogin. Text
Processing commands - tail, head, sort, nl, uniq, grep, egrep, fgrep, cut, paste, join, tee, pg, comm,
cmp, diff, tr, cpio

I. 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.

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.

II. 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.

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

III. Components of Linux System

Linux Operating System has primarily three components

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

2|Page
IV. Architecture of LINUX OS
The Linux operating system's architecture mainly contains some of the components: the Kernel,
System Library, Hardware layer, System, and Shell utility.

1. Kernel:- The kernel is one of the core section of an operating system. It is responsible for each of
the major actions of the Linux OS. This operating system contains distinct types of modules and
cooperates with underlying hardware directly. The kernel facilitates required abstraction for hiding
details of low-level hardware or application programs to the system. There are some of the important
kernel types which are mentioned below:

• Monolithic Kernel

• Micro kernels

• Exo kernels

• Hybrid kernels

2. System Libraries: - These libraries can be specified as some special functions. These are applied
for implementing the operating system's functionality and don't need code access rights of the
modules of kernel.

3. System Utility Programs: - It is responsible for doing specialized level and individual activities.

4. Hardware layer:- Linux operating system contains a hardware layer that consists of several
peripheral devices like CPU, HDD, and RAM.

5. Shell: - It is an interface among the kernel and user. It can afford the services of kernel. It can take
commands through the user and runs the functions of the kernel. The shell is available in distinct
types of OSes. These operating systems are categorized into two different types, which are
the graphical shells and command-line shells.

The graphical line shells facilitate the graphical user interface, while the command line shells
facilitate the command line interface. Thus, both of these shells implement operations. However, the
graphical user interface shells work slower as compared to the command-line interface shells.

3|Page
V. Kernel Mode vs User Mode

Kernel component code executes in a special privileged mode called kernel mode with full access to
all resources of the computer. This code represents a single process, executes in single address space
and do not require any context switch and hence is very efficient and fast. Kernel runs each process
and provides system services to processes, provides protected access to hardware to processes.

Support code which is not required to run in kernel mode is in System Library. User programs and
other system programs works in User Mode which has no access to system hardware and kernel
code. User programs/ utilities use System libraries to access Kernel functions to get system's low-
level tasks.

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:

• 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

4|Page
manages communication between all the devices. The kernel has certain rules that have to be
followed by all the devices.
• 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.
• 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.
• 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

5|Page
programmers and are called toolchain. A toolchain is a vital development tool used by the developers
to produce a working application.

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.

VI. 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.

A. 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

6|Page
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."

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

B. 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.

C. 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.

D. 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.

E. 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

7|Page
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.

F. 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.

VII. 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.

VIII. Linux Distribution

Linux distribution is an operating system that is made up of a collection of software based on Linux
kernel or you can say distribution contains the Linux kernel and supporting libraries and software.
And you can get Linux based operating system by downloading one of the Linux distributions and
these distributions are available for different types of devices like embedded devices, personal
computers, etc. Around 600 + Linux Distributions are available and some of the popular Linux
distributions are:
• MX Linux
• Manjaro
• Linux Mint
• elementary
• Ubuntu

8|Page
• Debian
• Solus
• Fedora
• openSUSE
• Deepin

IX. 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.

Top 20 Advantages of Linux

Following are top 20 advantages of the Linux operating system:

1) Open 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.

9|Page
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 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

10 | P a g e
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.

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.

11 | P a g e
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.

12 | P a g e
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.

X. Disadvantages of Linux

• It is not very user-friendly. So, it may be confusing for beginners.


• It has small peripheral hardware drivers as compared to windows.

XI. Is There Any Difference between Linux and Ubuntu?


The answer is YES. The main difference between Linux and Ubuntu is Linux is the family of open-
source operating systems which is based on Linux kernel, whereas Ubuntu is a free open-source
operating system and the Linux distribution which is based on Debian. Or in other words, Linux is
the core system and Ubuntu is the distribution of Linux. Linux is developed by Linus Torvalds and
released in 1991 and Ubuntu is developed by Canonical Ltd. and released in 2004.

XII. Linux Features

➢ Multiuser capability: Multiple users can access the same system resources like memory,
hard disk, etc. But they have to use different terminals to operate.

➢ Multitasking: More than one function can be performed simultaneously by dividing the CPU
time intelligently.
➢ Portability: Portability doesn't mean it is smaller in file size or can be carried in pen drives or
memory cards. It means that it supports different types of hardware.

13 | P a g e
➢ Security: It provides security in three ways namely authenticating (by assigning password
and login ID), authorization (by assigning permission to read, write and execute) and
encryption (converts file into an unreadable format).

➢ Live CD/USB: Almost all Linux distros provide live CD/USB so that users can run/try it
without installing it.
➢ Graphical User Interface (X Window system): Linux is command line-based OS but it can
be converted to GUI based by installing packages.

➢ Support's customized keyboard: As it is used worldwide, hence supports different


languages keyboards.

➢ Application support: It has its own software repository from where users can download and
install many applications.

➢ File System: Provides hierarchical file system in which files and directories are arranged.
➢ Open Source: Linux code is freely available to all and is a community-based development
project.

XIII. Why Use Linux

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

• It is an open-source OS which gives a great advantage to the programmers as they can design
their own custom operating systems.

• It gives you a lot of option of programs having some different features so you can choose
according to your need.

• A global development community look at different ways to enhance its security, hence it is
highly secured and robust so you don't need an antivirus to scan it regularly. Companies like
Google, Amazon and Facebook use Linux in order to protect their servers as it is highly reliable
and stable.

• Above all you don't have to pay for software and server licensing to install Linux, it’s absolutely
free and you can install it on as many computers as you want.

• It’s completely trouble-free operating system and don't have an issue with viruses, malware
and slowing down your computer.

14 | P a g e
14. 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:

• It provides command-line editing


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

a) 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.

15. 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.

15 | P a g e
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:

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.

16. 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.

16 | P a g e
1. Difference between Linux and Unix

Comparison Linux Unix

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


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

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

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

Usage Linux is used everywhere from servers, PC, It is used in servers,


smartphones, tablets to mainframes and workstations and PCs.
supercomputers.

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


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

Development As it is open source, it is developed by sharing Unix was developed by AT&T


and collaboration of codes by world-wide Labs, various commercial
developers. vendors and non-profit
organizations.

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

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

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

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 Unix contain a completely different
doesn't 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- Unix is also highly secured. It has
100 viruses listed till date. about 85-120 viruses listed till
date

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

2. 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.

a) Difference between Linux and Windows operating system

18 | P a g e
i. What is the Windows operating system?

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.

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

• File System

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

19 | P a g e
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.

Parameter Linux Windows

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

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

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

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

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

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

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

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

Update Linux provides full control to its Windows updates are annoying. The updates will
users on updates. A user can install come at any time and take too much time to install.
the update whenever needed. Also, Sometimes, you power on your machine, and updates
it takes less time to install an are automatically getting started. Unfortunately, the
update. 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 primarily target the Windows for malware and virus.
and attackers to find a loophole in Windows is most vulnerable without anti-virus.
it. So, Linux is hard to
breakthrough.

License Linux is distributed under Windows is distributed under a Proprietary


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

17. 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.

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.

21 | P a g e
1. Common Environment Variables

Some standard environment variables are as follows:

• PATH

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

• USER

This variable holds the username.

• HOME

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

• EDITOR

This variable contains the path to the specified editor.

• UID

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

• TERM

This variable contains the path to the default terminal emulator.

• SHELL

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

• ENV

This variable displays all the environment variable.

22 | P a g e
18. Linux Commands with Examples
The Linux command is a utility of the Linux operating system. All basic and advanced tasks can be
done by executing commands. The commands are executed on the Linux terminal. The terminal is a
command-line interface to interact with the system, which is similar to the command prompt in the
Windows OS. Commands in Linux are case-sensitive.

Linux

Linux provides a powerful command-line interface compared to other operating systems such as Windows
and MacOS. We can do basic work and advanced work through its terminal. We can do some basic tasks
such as creating a file, deleting a file, moving a file, and more. In addition, we can also perform advanced
tasks such as administrative tasks (including package installation, user management), networking tasks (ssh
connection), security tasks, and many more.

Linux terminal is a user-friendly terminal as it provides various support options. To open the Linux
terminal, press "CTRL + ALT + T" keys together, and execute a command by pressing the 'ENTER'
key.

In this topic, we will discuss the top 50 most frequently used Linux commands with their examples.
These commands are very useful for a beginner and professional both. We have divided these
commands into following sections so that you can easily identify their usage:

19. Linux Commands


The following are the top 50 Linux commands:

A. Linux Directory Commands

1. sudo command

sudo (Super User DO) command in Linux is generally used as a prefix of some command that only
superuser are allowed to run. If you prefix “sudo” with any command, it will run that command
with elevated privileges or in other words allow a user with proper permissions to execute a
command as another user, such as the superuser. This is the equivalent of “run as administrator”
option in Windows.

23 | P a g e
These users who can use the sudo command need to have an entry in the sudoers file located
at “/etc/sudoers”. Remember that to edit or view the sudoers file you have to use sudo command.
To edit the sudoers file it is recommended to use “visudo” command.
By default, sudo requires that users authenticate themselves with a password which is the user’s
password, not the root password itself.
Syntax:

sudo OPTION... COMMAND

2. man Command

man is the system's manual pager. Each page argument given to man is normally the name of a
program, utility or function. The manual page associated with each of these arguments is then found
and displayed. A section, if provided, will direct man to look only in that section of the manual. The
default action is to search in all of the available sections, following a pre-defined order and to show
only the first page found, even if page exists in several sections.

Syntax:
$man [OPTION]... [COMMAND NAME]

Options and Examples

b) No Option: It displays the whole manual of the command.

Syntax:
$ man [COMMAND NAME]

Example:
$ man printf

24 | P a g e
3. echo command

echo command in linux is used to display line of text/string that are passed as an argument. This is
a built-in command that is mostly used in shell scripts and batch files to output status text to the
screen or a file.

Syntax:
echo [option] [string]

Options of echo command

NOTE :- -e here enables the interpretation of backslash escapes

i. \b : it removes all the spaces in between the text

Example :

echo -e "I \blive \bin \bPilani"

ii. \c : suppress trailing new line with backspace interpreter ‘-e‘ to continue without emitting
new line.

Example :
echo -e "I live\cin Pilani"

iii. \n : this option creates new line from where it is used.


iv. \t : this option is used to create horizontal tab spaces.
v. \r : carriage return with backspace interpreter ‘-e‘ to have specified carriage return in
output.

25 | P a g e
vi. \v : this option is used to create vertical tab spaces.
vii. \a : alert return with backspace interpreter ‘-e‘ to have sound alert.
viii. echo * : this command will print all files/folders, similar to ls command

4. printf command

“printf” command in Linux is used to display the given string, number or any other format
specifier on the terminal window. It works the same way as “printf” works in programming
languages like C.

Syntax:
$printf [-v var] format [arguments]

Format Specifiers: The most commonly used printf specifiers are %s, %b, %d, %x and %f.

Examples:
• %s specifier: It is basically a string specifier for string output.

$printf "%s\n" "Hello, World!"

• %b specifier: It is same as string specifier but it allows us to interpret escape sequences


with an argument.
• %d specifier: It is an integer specifier for showing the integral values.
• %f specifier: It is used for output of floating-point values
• %x specifier: It is used for output of lowercase hexadecimal values for integers and for
padding the output.

5. script command

script command in Linux is used to make typescript or record all the terminal activities. After
executing the script command, it starts recording everything printed on the screen including the
inputs and outputs until exit. By default, all the terminal information is saved in the file typescript,
if no argument is given. script is mostly used when we want to capture the output of a command or

26 | P a g e
a set of command while installing a program or the logs generated on the terminal while compiling
an opensource codes, etc. script command uses two files i.e., one for the terminal output and other
for the timing information.
Syntax:
script [options] [file]

In order to stop the typescript, we just need to execute exit command and script will stop the
capturing process. Since there’s no filename given as argument, the script will automatically create
a file namely typescript in the home directory to save the recorded information.

6. passwd Command

The passwd command is used to create and change the password for a user.

Syntax:

passwd <username>

Output:

7. uname

The command ‘uname‘ displays the information about the system.

Syntax:
uname [OPTION]

Options and Examples


i. -a option: It prints all the system information in the following order: Kernel
name, network node hostname, kernel release date, kernel version, machine hardware
name, hardware platform, operating system
ii. -s option: It prints the kernel name.
iii. -n option: It prints the hostname of the network node(current computer).
iv. -r option: It prints the kernel release date

27 | P a g e
v. -v option: It prints the version of the current kernel.
vi. -m option: It prints the machine hardware name.
vii. -p option: It prints the type of the processor.
viii. -i option: It prints the platform of the hardware.
ix. -o option: It prints the name of the operating system.

8. who command

who command is used to find out the following information:


1. Time of last system boot
2. Current run level of the system
3. List of logged in users and more.
Description: The who command is used to get information about currently logged in user on to
system.
Syntax:
$who [options] [filename]

Examples:
The who command displays the following information for each user currently logged in to the
system if no option is provided:
1. Login name of the users
2. Terminal line numbers
3. Login time of the users in to system
4. Remote host name of the user

9. date command

date command is used to display the system date and time. date command is also used to set date
and time of the system. By default, the date command displays the date in the time zone on which
unix/linux operating system is configured. You must be the super-user (root) to change the date
and time.

Syntax:

28 | P a g e
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

10. stty command

stty command in Linux is used to change and print terminal line settings. Basically, this command
shows or changes terminal characteristics.

Syntax:
stty [-F DEVICE | --file=DEVICE] [SETTING]...
stty [-F DEVICE | --file=DEVICE] [-a|--all]
stty [-F DEVICE | --file=DEVICE] [-g|--save]

11. pwd Command

The pwd command is used to display the location of the current working directory.

Syntax:

pwd

Output:

12. mkdir Command

The mkdir command is used to create a new directory under any directory.

Syntax:

mkdir <directory name>


Output:

13. rmdir Command

29 | P a g e
The rmdir command is used to delete a directory.

Syntax:

rmdir <directory name>


Output:

14. ls Command

The ls command is used to display a list of content of a directory.

Syntax:

ls

Output:

15. cd Command

The cd command is used to change the current directory.

Syntax:

cd <directory name>

Output:

16. lp command
This command is also known as the printer management command Linux. It submits files for
printing or alters a pending job

30 | P a g e
Syntax:
lp [filename]

17. od command

The 'od' term stands for octal dump. It displays content of a file in different human-readable
formats like hexadecimal, octal and ASCII characters

Syntax:

od -b <fileName> (display files in octal format)


od -t x1 <fileName> (display files in hexadecimal bytes format)
od -c <fileName> (display files in ASCII (backslashed) character format)

18. tar command

The Linux ‘tar’ stands for tape archive, is used to create Archive and extract the Archive files. tar
command in Linux is one of the important commands which provides archiving functionality in
Linux. We can use Linux tar command to create compressed or uncompressed Archive files and also
maintain and modify them.

Syntax:
tar [options] [archive-file] [file or directory to be archived]

Options:
-c : Creates Archive
-x : Extract the archive
-f : creates archive with given filename
-t : displays or lists files in archived file
-u : archives and adds to an existing archive file
-v : Displays Verbose Information
-A : Concatenates the archive files
-z : zip, tells tar command that creates tar file using gzip
-j : filter archive tar file using tbzip

31 | P a g e
-W : Verify a archive file
-r : update or add file or directory in already existed .tar file

What is an Archive file?


An Archive file is a file that is composed of one or more files along with metadata. Archive files are
used to collect multiple data files together into a single file for easier portability and storage, or
simply to compress files to use less storage space.

19. Gzip command

gzip command compresses files. Each single file is compressed into a single file. The compressed
file consists of a GNU zip header and deflated data.

If given a file as an argument, gzip compresses the file, adds a “.gz” suffix, and deletes the original
file. With no arguments, gzip compresses the standard input and writes the compressed file to
standard output.

Syntax:
gzip [Options] [filenames]

Example:
$ gzip mydoc.txt

32 | P a g e
B. LINUX Networking Commands
1. unlink command

unlink is a command-line utility for removing a single file.

Syntax:
unlink filename

2. du command
du command, short for disk usage, is used to estimate file space usage.
The du command can be used to track the files and directories which are consuming excessive
amount of space on hard disk drive.

Syntax:
du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F

Options :
-0, –null : end each output line with NULL
-a, –all : write count of all files, not just directories
–apparent-size : print apparent sizes, rather than disk usage.
-B, –block-size=SIZE : scale sizes to SIZE before printing on console
-c, –total : produce grand total
-d, –max-depth=N : print total for directory only if it is N or fewer levels below command line
argument
-h, –human-readable : print sizes in human readable format
-S, -separate-dirs : for directories, don’t include size of subdirectories
-s, –summarize : display only total for each directory
–time : show time of last modification of any file or directory.
–exclude=PATTERN : exclude files that match PATTERN

33 | P a g e
3. df command

The df command (short for disk free), is used to display information related to file systems about
total space and available space.

Syntax :
df [OPTION]... [FILE]...

If no file name is given, it displays the space available on all currently mounted file systems.
For example:
df

4. mount command
All files in a Linux filesystem are arranged in form of a big tree rooted at ‘/‘.These files can be
spread out on various devices based on your partition table, initially your parent directory is
mounted(i.e attached) to this tree at ‘/‘, others can be mounted manually using GUI interface(if
available) or using mount command.
mount command is used to mount the filesystem found on a device to big tree
structure(Linux filesystem) rooted at ‘/‘. Conversely, another command umount can be used to
detach these devices from the Tree.
Syntax: mount -t type device dir

Some Important Options:


• l : Lists all the file systems mounted yet.
• h : Displays options for command.
• V : Displays the version information.
• a : Mounts all devices described at /etc/fstab.
• t : Type of filesystem device uses.
• T : Describes an alternative fstab file.
• r : Read-only mode mounted.

5. unmount command

The umount command unmounts a previously mounted device, directory, file, or file system.
Processing on the file system, directory, or file completes and it is unmounted. Members of the system
group and users operating with root user authority can issue any umount command.

34 | P a g e
Syntax:

umount [-hV]

umount -a [-dflnrv] [-t vfstype] [-O options]

umount [-dflnrv] {dir|device}

6. find command

The find command in UNIX is a command line utility for walking a file hierarchy. It can be used to
find files and directories and perform subsequent operations on them. It supports searching by
file, folder, name, creation date, modification date, owner and permissions. By using the ‘-exec’
other UNIX commands can be executed on files or folders found.
Syntax:
$ find [where to start searching from]
[expression determines what to find] [-options] [what to find]

Options:
• -exec CMD: The file being searched which meets the above criteria and returns 0 for as
its exit status for successful command execution.
• -ok CMD: It works same as -exec except the user is prompted first.
• -inum N: Search for files with inode number ‘N’.
• -links N: Search for files with ‘N’ links.
• -name demo: Search for files that are specified by ‘demo’.
• -newer file: Search for files that were modified/created after ‘file’.
• -perm octal: Search for the file if permission is ‘octal’.
• -print: Display the path name of the files found by using the rest of the criteria.
• -empty: Search for empty files and directories.
• -size +N/-N: Search for files of ‘N’ blocks; ‘N’ followed by ‘c’can be used to measure size
in characters; ‘+N’ means size > ‘N’ blocks and ‘-N’ means size < ‘N’ blocks.
• -user name: Search for files owned by user name or ID ‘name’.
• \(expr \) : True if ‘expr’ is true; used for grouping criteria combined with OR or AND.
• ! expr: True if ‘expr’ is false.

35 | P a g e
7. umask command
The umask command in Linux is used to set default permissions for files or directories the user
creates.
How does the umask command work?
• The umask command specifies the permissions that the user does not want to be given
out to the newly created file or directory.
• umask works by doing a Bitwise AND with the bitwise complement (where the bits are
inverted, i.e. 1 becomes 0 and 0 becomes 1) of the umask.
• The bits which are set in the umask value, refer to the permissions, which are not
assigned by default, as these values are subtracted from the maximum permission for
files/directories.
How to calculate umask value?
Syntax:
$umask

[The above command will give the following output]

8. ulimit command
ulimit is admin access required Linux shell command which is used to see, set, or limit the resource
usage of the current user. It is used to return the number of open file descriptors for each process.
It is also used to set restrictions on the resources used by a process.
Syntax:
To check the ulimit value use the following command:

ulimit -a

Working with ulimit commands:


1. To display maximum users process or for showing maximum user process limit for the logged-
in user.
ulimit -u

36 | P a g e
2. For showing the maximum file size a user can have.
ulimit -f
3. For showing maximum memory size for the current user.
ulimit -m
4. For showing maximum memory size limit.

ulimit -v

9. ps command
Linux provides us a utility called ps for viewing information related with the processes on a system
which stands as abbreviation for “Process Status”. ps command is used to list the currently
running processes and their PIDs along with some other information depends on different options.
It reads the process information from the virtual files in /proc file-system. /proc contains virtual
files, this is the reason it’s referred as a virtual file system.
ps provides numerous options for manipulating the output according to our need.

Syntax

ps [options]

10. w command
w command in Linux is used to show who is logged on and what they are doing. This command
shows the information about the users currently on the machine and their processes. The header
shows, in this order, the current time, how long the system has been running, how many users are
currently logged on, and the system load averages for the past 1, 5, and 15 minutes. The following
entries are displayed for each user: login name, the tty name, the remote host, login time, idle time,
JCPU, PCPU, and the command line of their current process. The JCPU time is the time used by all
processes attached to the tty. It does not include past background jobs but does include currently
running background jobs. The PCPU time is the time used by the current process, named in the
“what” field.

Syntax:
w [options] user [...]

37 | P a g e
11. finger command

Finger command is a user information lookup command which gives details of all the users
logged in. This tool is generally used by system administrators. It provides details like login name,
user name, idle time, login time, and in some cases their email address even.

Installing finger User Information Lookup Tool

To install finger tool use the following commands as per your Linux distribution.
• In case of Debian/Ubuntu

$sudo apt-get install finger

• In case of CentOS/RedHat

$sudo yum install finger

• In case of Fedora OS

$sudo dnf install finger


Syntax:
$finger manav

*manav is the username

It displays the login name, name, directory, shell, login time, email, and plan of the user.

12. arp command

arp command manipulates the System’s ARP cache. It also allows a complete dump of the ARP
cache. ARP stands for Address Resolution Protocol. The primary function of this protocol is to
resolve the IP address of a system to its mac address, and hence it works between level 2(Data link
layer) and level 3(Network layer).

Syntax:
arp [-v] [-i if] [-H type] -a [hostname]

38 | P a g e
13. ftp command

The ftp stands for File Transfer Protocol. It connects to the remote host to exchange files and
directories from one host to another over a network which can be LAN or any other.

Syntax:

ftp

14. telnet command

In Linux, the telnet command is used to create a remote connection with a system over a TCP/IP
network. It allows us to administrate other systems by the terminal. We can run a program to conduct
administration.

It uses a TELNET protocol. However, this protocol has some security defects, but it is one of the most
used networking protocols due to its simplicity. It is not a secure protocol because it transfers data
in unencrypted form. Often Linux user prefers ssh over telnet because ssh transfers data in encrypted
form. This utility is similar to the Remote Desktop feature in Windows. The syntax for the telnet is as
Follows:

telnet hostname/IP address

15. rlogin command


• The rlogin command enables you to log in to other UNIX machines on your network.

• If a password prompt appears, type the password for the remote machine and press Return.

Syntax
rlogin [-8EKLdx] [-e char] [-l username] host

39 | P a g e
Options

-8 Allows an eight-bit input data path at all times; otherwise, parity bits are stripped except when the remote

side's stop and start characters are other than ^S/^Q.

-E Stops any character from being recognized as an escape character. When used with the -8 option, this

provides a completely transparent connection.

-L The -L option allows the rlogin session to run in "litout" mode, a special BSD terminal mode.

-d Turns on socket debugging on the TCP sockets used for communication with the remote host.

-e Allows user specification of the escape character, which is "~" (tilde) by default. This specification may be as a

literal character, or as an octal value in the form \nnn.

40 | P a g e
C. Text Processing Commands
1. head command

It is the complementary of Tail command. The head command, as the name implies, print the top
N number of data of the given input. By default, it prints the first 10 lines of the specified files. If
more than one file name is provided then data from each file is preceded by its file name.
Syntax:
head [OPTION]... [FILE]...

Let us consider two files having name state.txt and capital.txt contains all the names of the
Indian states and capitals respectively.

2. tail command
It is the complementary of head command.The tail command, as the name implies, print the last N
number of data of the given input. By default it prints the last 10 lines of the specified files. If
more than one file name is provided then data from each file is precedes by its file name.

Syntax:
tail [OPTION]... [FILE]...

41 | P a g e
3. sort command
SORT command is used to sort a file, arranging the records in a particular order. By default, the
sort command sorts file assuming the contents are ASCII. Using options in the sort command can
also be used to sort numerically.

• SORT command sorts the contents of a text file, line by line.


• sort is a standard command-line program that prints the lines of its input or
concatenation of all files listed in its argument list in sorted order.
• The sort command is a command-line utility for sorting lines of text files. It supports
sorting alphabetically, in reverse order, by number, by month, and can also remove
duplicates.
• The sort command can also sort by items not at the beginning of the line, ignore case
sensitivity, and return whether a file is sorted or not. Sorting is done based on one or
more sort keys extracted from each line of input.
• By default, the entire input is taken as the sort key. Blank space is the default field
separator.
The sort command follows these features as stated below:
1. Lines starting with a number will appear before lines starting with a letter.
2. Lines starting with a letter that appears earlier in the alphabet will appear before lines
starting with a letter that appears later in the alphabet.
3. Lines starting with a uppercase letter will appear before lines starting with the same
letter in lowercase.

42 | P a g e
Syntax:
$ sort filename.txt

OPTIONS:

-r Sorts in reverse order.


-u If line is duplicated display only once.
-o filename Sends sorted output to a file.

EXAMPLE:

1. sort test.txt
Sorts the 'test.txt'file and prints result in the screen.
2. sort -r test.txt
Sorts the 'test.txt' file in reverse order and prints result in the screen.

4. nl command
Linux offers a wide range of commands for text formatting and editing. While editing a text file, you
might want to display the lines with line numbers appended before them, and here comes the role-
play of the nl command in Linux. nl command is a Unix/Linux utility that is used for numbering
lines, accepting input either from a file or from STDIN. It copies each specified file to STDOUT, with
line numbers appended before the lines.
Syntax:
nl [OPTION]... [FILE]

Options:
-b NUMBER or -bNUMBER : used for numbering body lines
-i NUMBER or -iNUMBER : line number increment at each line
-n FORMAT or -nNUMBER : insert line numbers according to FORMAT
-v NUMBER or -vNUMBER : change first line number of the given input
-l NUMBER or -lNUMBER : group of NUMBER empty lines are counted as one
-s STRING or -sSTRING : add any STRING after every logical line number
-w NUMBER or -wNUMBER : use different NUMBER columns for line numbers

43 | P a g e
5. uniq commands
The uniq command in Linux is a command-line utility that reports or filters out the repeated
lines in a file. In simple words, uniq is the tool that helps to detect the adjacent duplicate lines and
also deletes the duplicate lines.

uniq filters out the adjacent matching lines from the input file (that is required as an argument)
and writes the filtered data to the output file.

Syntax:
$uniq [OPTION] [INPUT[OUTPUT]]

The syntax of this is quite easy to understand. Here, INPUT refers to the input file in which repeated
lines need to be filtered out and if INPUT isn’t specified then uniq reads from the standard
input. OUTPUT refers to the output file in which you can store the filtered output generated
by uniq command and as in the case of INPUT if OUTPUT isn’t specified then uniq writes to the
standard output.

6. grep command
The grep filter searches a file for a particular pattern of characters, and displays all lines that
contain that pattern. The pattern that is searched in the file is referred to as the regular expression
(grep stands for global search for regular expression and print out)

Syntax:
grep [options] pattern [files]

Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching

44 | P a g e
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line, with each such part on a separate output line.
-A n : Prints searched line and nlines after the result.
-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.

7. egrep command

egrep is a pattern searching command which belongs to the family of grep functions. It works the
same way as grep -E does. It treats the pattern as an extended regular expression and prints out
the lines that match the pattern. If there are several files with the matching pattern, it also
displays the file names for each line.
Syntax: egrep [ options ] 'PATTERN' files

8. fgrep command
The fgrep filter is used to search for the fixed-character strings in a file. There can be multiple files
also to be searched. This command is useful when you need to search for strings which contain
lots of regular expression metacharacters, such as “^”, “$”, etc.

Syntax
fgrep [options] [ -e pattern_list] [pattern] [file]

Options with Description:

• -c : It is used to print only a count of the lines which contain the pattern.

• -h : Used to display the matched lines.

• -i : During comparisons, it will ignore upper/lower case distinction.

45 | P a g e
• -l : Used to print the names of files with matching lines once, separated by new-lines. It
will not repeat the names of files when the pattern is found more than once.

• -n : It is used precede each line by its line number in the file (first line is 1).

• -s : It will only display the error messages.

• -v : Print all lines except those contain the pattern.

• -x : Print only lines matched entirely.

• -e pattern_list : Search for a string in pattern-list (useful when the string begins with a
“-“).

• -f pattern-file : Take the list of patterns from pattern-file.

• pattern : Specify a pattern to be used during the search for input.

• file : A path name of a file to be searched for the patterns. If no file operands are
specified, the standard input will be used .

9. cut Command

The cut command is used to select a specific column of a file . The '-d' option is used as a

delimiter, and it can be a space (' '), a slash (/), a hyphen (-), or anything else.
And, the '-f' option is used to specify a column number.

Syntax:

cut -d(delimiter) -f(columnNumber) <fileName>

Output:

46 | P a g e
10. paste Command
Paste command is one of the useful commands in Unix or Linux operating system. It is used to join
files horizontally (parallel merging) by outputting lines consisting of lines from each file specified,
separated by tab as delimiter, to the standard output. When no file is specified, or put dash (“-“)
instead of file name, paste reads from standard input and gives output as it is until a interrupt
command [Ctrl-c] is given.
Syntax:
paste [OPTION]... [FILES]...
sOptions:
i. -d (delimiter): Paste command uses the tab delimiter by default for merging the files. The
delimiter can be changed to any other character by using the -d option. If more than one
character is specified as delimiter then paste uses it in a circular fashion for each file line
separation.
ii. -s (serial): We can merge the files in sequentially manner using the -s option. It reads all
the lines from a single file and merges all these lines into a single line with each line
separated by tab. And these single lines are separated by newline.
iii. –version: This option is used to display the version of paste which is currently running on
your system.
1.

47 | P a g e
Applications of Paste Command
i. Combining N consecutive lines: The paste command can also be used to merge N
consecutive lines from a file into a single line. Here N can be specified by specifying
number hyphens(-) after paste.
ii. Combination with other commands: Even though paste require at least two files for
concatenating lines, but data from one file can be given from shell. Like in our example
below, cut command is used with -f option for cutting out first field of state file and output
is pipelined with paste command having one file name and instead of second file name
hyphen is specified.

11. join command


The join command in UNIX is a command line utility for joining lines of two files on a common
field.

Suppose you have two files and there is a need to combine these two files in a way that the output
makes even more sense. For example, there could be a file containing names and the other
containing ID’s and the requirement is to combine both files in such a way that the names and
corresponding ID’s appear in the same line. join command is the tool for it. join command is used
to join the two files based on a key field present in both the files. The input file can be
separated by white space or any delimiter.

Syntax:

$join [OPTION] FILE1 FILE2

Options for join command:


1. -a FILENUM : Also, print unpairable lines from file FILENUM, where FILENUM is 1
or 2, corresponding to FILE1 or FILE2.
2. -e EMPTY : Replace missing input fields with EMPTY.
3. -i - -ignore-case : Ignore differences in case when comparing fields.
4. -j FIELD : Equivalent to "-1 FIELD -2 FIELD".
5. -o FORMAT : Obey FORMAT while constructing output line.
6. -t CHAR : Use CHAR as input and output field separator.
7. -v FILENUM : Like -a FILENUM, but suppress joined output lines.

48 | P a g e
8. -1 FIELD: Join on this FIELD of file 1.
9. -2 FIELD: Join on this FIELD of file 2.
10. - -check-order: Check that the input is correctly sorted, even if all input lines
are pairable.
11. - -nocheck-order: Do not check that the input is correctly sorted.
12. - -help: Display a help message and exit.
13. - -version: Display version information and exit.

12. tee command

The tee command is quite similar to the cat command. The only difference between both filters is
that it puts standard input on standard output and also write them into a file.

Syntax:
cat <fileName> | tee <newFile> | cat or tac |.....

Output:

49 | P a g e
13. comm Command

The 'comm' command is used to compare two files or streams. By default, it displays
three columns, first displays non-matching items of the first file, second
indicates the non-matching item of the second file, and the third column displays
the matching items of both files.

Syntax:

comm <file1> <file2>

Output:

• tr Command

The tr command is used to translate the file content like from lower case to upper
case.

Syntax:

command | tr <'old'> <'new'>

Output:

50 | P a g e
51 | P a g e

You might also like