0% found this document useful (0 votes)
14 views315 pages

Linux-All Lectures

The document provides an overview of various Linux distributions, including Ubuntu, Linux Mint, Debian, RedHat Enterprise/CentOS, and Fedora, highlighting their features and target users. It also discusses the functions of the Linux kernel, the shell and its types, and details about Ubuntu and its flavors like Kubuntu and Xubuntu. Additionally, it covers Linux installation, partitioning with GParted, the bootloader process, and the Linux file system structure.

Uploaded by

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

Linux-All Lectures

The document provides an overview of various Linux distributions, including Ubuntu, Linux Mint, Debian, RedHat Enterprise/CentOS, and Fedora, highlighting their features and target users. It also discusses the functions of the Linux kernel, the shell and its types, and details about Ubuntu and its flavors like Kubuntu and Xubuntu. Additionally, it covers Linux installation, partitioning with GParted, the bootloader process, and the Linux file system structure.

Uploaded by

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

Linux

System Administration

Prepared by- Amitabh Srivastava


Prepared by- Amitabh Srivastava
Prepared by- Amitabh Srivastava
Prepared by- Amitabh Srivastava
Linux Distributions
There are on an average six hundred Linux distributors providing different features. Here are some
of the popular Linux distros-

Ubuntu-

▪ It is the most well-known Linux distribution.

▪ It came into existence in 2004 by Canonical and quickly became popular.

▪ It can be used as easy graphical Linux desktop without the use of command line.

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

▪ It releases every six months and currently working to expand to run on tablets and
Prepared by- Amitabh Srivastava
smartphones.
Linux Distributions
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.

Prepared by- Amitabh Srivastava


Linux Distributions
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.

Prepared by- Amitabh Srivastava


Linux Distributions
RedHat 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.

▪ CentOS is a community project that uses RedHat 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.
Prepared by- Amitabh Srivastava
Linux Distributions
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.

Prepared by- Amitabh Srivastava


Prepared by- Amitabh Srivastava
Prepared by- Amitabh Srivastava
Prepared by- Amitabh Srivastava
Prepared by- Amitabh Srivastava
Prepared by- Amitabh Srivastava
Prepared by- Amitabh Srivastava
Prepared by- Amitabh Srivastava
Functions of a Kernel
➢ Device Management

➢ Memory Management

➢ Resource Management

➢ Accessing Computer Resources

Prepared by- Amitabh Srivastava


Shell
▪ A shell is a special user program that provides an interface for
the user to use operating system services.

▪ Shell accepts human-readable commands from users and


converts them into something which the kernel can
understand.

▪ It is a command language interpreter that executes


commands read from input devices such as keyboards or from
files.

▪ The shell gets started when the user logs in or starts the
terminal. Prepared by- Amitabh Srivastava
Types of Shell
▪ There are many different types of shell available in
Linux. Some of them are-

✓ Bash- Bourne Again Shell (default)

✓ Dash- Debian Alquist Shell

✓ Csh- C Shell

✓ Ksh- Korn Shell


Prepared by- Amitabh Srivastava
Types of Shell
▪ To see the available shells use the following command-

▪ To see the active shell use the following command-


$ echo $SHELL
Prepared by- Amitabh Srivastava
Ubuntu OS
▪ Ubuntu is a Linux distribution derived from Debian and
composed mostly of free and open-source software.

▪ Ubuntu is officially released in multiple editions- Desktop, Server,


and Core for Internet of things devices and robots.

▪ The operating system is developed by the British company


Canonical and a community of other developers.

Prepared by- Amitabh Srivastava


Ubuntu Desktop
▪ Ubuntu Desktop has always been free to download, use and share. Each release
of Ubuntu Desktop delivers the latest applications, libraries and toolchains.

▪ It is a primary platform for all major IDEs, game development tools and AI/ML
software.

▪ It offers essential applications for web browsing, messaging, gaming and content
creation, including Firefox, Chrome, OBS Studio, supporting all daily computing
needs.

▪ It prioritizes user privacy and system integrity, making it the secure choice for
those who value data protection.
Prepared by- Amitabh Srivastava
Ubuntu Server
▪ Ubuntu Server is a Free and Open-source operating system.

▪ Developed and maintained by Canonical Ltd. with regular LTS (Long Term Support) releases.

▪ Designed for server environments, focusing on stability, security, and performance.

▪ No GUI by default, which reduces system overhead and makes it ideal for performance.

▪ Pre-packaged software in the Ubuntu repository (e.g., Apache, Nginx, MySQL, PostgreSQL,
Docker).

▪ Excellent support for cloud platforms like AWS, Azure, and Google Cloud.

▪ Automatic security updates for critical packages and vulnerabilities.

Prepared by- Amitabh Srivastava


Kubuntu
▪ It’s an open-source, user-friendly operating system that provides a powerful, customizable, and
visually appealing environment.

▪ Kubuntu is an official Ubuntu flavour that uses the KDE Plasma desktop environment instead
of the default GNOME desktop found in Ubuntu.

▪ Plasma offers a sleek, visually stunning desktop with modern animations and effects.

▪ Users can modify themes, widgets, taskbars, and window decorations easily with the KDE
System Settings.

▪ Plasma includes many productivity tools like Krita, Kdenlive, and Okular for drawing, video
editing, and document viewing.

▪ KRunner is a powerful launcher that helps you quickly access apps, settings, and files.
Prepared by- Amitabh Srivastava
Key Apps of Kubuntu
▪ Dolphin File Manager-
A powerful file manager with features like tabbed browsing, advanced file search,
and integration with cloud services.

▪ Konsole Terminal-
A robust terminal emulator, great for developers and advanced users who need
command-line access.

▪ Kate Text Editor-


A highly capable text editor for coding, with syntax highlighting, and features for
advanced users.

▪ LibreOffice Suite-
Kubuntu comes with LibreOffice pre-installed for word processing, spreadsheets,
and presentations. Prepared by- Amitabh Srivastava
Xubuntu
▪ Xubuntu is an official Ubuntu flavor that uses the XFCE desktop environment, focusing on
providing a lightweight, efficient, and user-friendly experience.

▪ XFCE is a lightweight desktop environment that uses fewer system resources, making it ideal
for low-resource or older hardware.

▪ Xubuntu provides a fast, responsive experience even on systems with limited RAM or CPU
power.

▪ XFCE offers a simple, straightforward interface with a traditional desktop layout (menu, taskbar,
system tray).

▪ XFCE is customizable, allowing users to tweak the appearance and functionality of the desktop
environment to suit their preferences.

▪ Includes the ability to change themes, panel layouts, and add widgets, but it remains simple
and uncluttered. Prepared by- Amitabh Srivastava
Key Apps of xubuntu
▪ Thunar File Manager-
It is a lightweight, easy to use, and highly customizable, allowing users to manage
files quickly and efficiently. Includes features like customizable actions, a simple
search function, and file previews.

▪ XFCE Terminal-
A fast and efficient terminal emulator, perfect for users who prefer command-line
operations.

▪ Mousepad Text Editor-


A simple and lightweight text editor designed for fast note-taking and basic text
manipulation.

▪ Ristretto Image Viewer-


A lightweight image viewer for quick and easy viewing of common image
Preparedformats.
by- Amitabh Srivastava
Activating / Installing Linux (ubuntu)

Prepared by- Amitabh Srivastava


Activating / Installing Linux

Prepared by- Amitabh Srivastava


Partitioning with Gparted
▪ Disk Partitioning in Linux is done using command line
programs like fdisk.

▪ GParted(GNOME Partition Editor) is a Graphical user


program based on GTK which allows Disk Partitioning with
just a click of the buttons.

▪ GParted is used for creating, deleting, resizing, moving,


checking, and copying disk partitions and manipulating file
systems such as exfat, fat32/64, ext2/3/4, Linux-swap and
more. Prepared by- Amitabh Srivastava
Installing Gparted
▪ We can install GParted in Ubuntu with a single command using APT-
sudo apt install gparted

▪ You can verify whether GParted is successfully installed or not using the below
command which gives an output similar to below image-

Prepared by- Amitabh Srivastava


Gparted Interface
▪ To open GParted , give the following command-
▪ sudo gparted

▪ It will open the Gparted application same as below image-

Prepared by- Amitabh Srivastava


Gparted Menu
▪ new – create a new partition
▪ delete – delete an existing partition
▪ resize/move – to resize a partition, either shrink(or)increase the size
▪ copy/paste – used to copy/paste the text(or) information
▪ undo – undo the previous action
▪ apply – to perform the operation chosen from above, you have to click the
apply icon to commit any selected operation in gparted

Prepared by- Amitabh Srivastava


Bootloader in Linux
▪ The bootloader in Linux serves as a critical component in the startup process of
our Operating systems. It plays an important role in initiating the system and
facilitating the loading of the Linux kernel.
▪ Once we turn on our Linux machines the first thing that runs is the BIOS (Basic
Input Output System). The BIOS is responsible for basic hardware initialization. It
checks for connected devices, performs a power-on self-test (POST), and
searches for the MBR (Master Boot Record) in the BIOS boot menu.
▪ The MBR is a small piece of information (512 bytes) that contains the bootloader
program and disk partitioning information. After the MBR is run the bootloader
begins to start. The bootloader is a crucial component of the Linux booting
process and is responsible for loading the Linux kernel into memory. Then
followed by the kernel initialization phase of the Linux booting process.
Prepared by- Amitabh Srivastava
Functions of Bootloader
❑ Some common functions of a Bootloader are-
• Performs boot device selection based on configuration.
• Takes control from the BIOS firmware after the initial bootloader is loaded.

• Provides a menu or configuration options for selecting an operating system (in


multi-boot setups, if applicable).

• Loads the operating system's kernel and necessary files into memory from the
boot device.

• Sets up the necessary environment for the operating system to start execution.

• Transfers control to the operating system, handing off the boot process to the
kernel. Prepared by- Amitabh Srivastava
Best Linux Bootloader
GRUB-
GRUB or Grand Unified Bootloader is one of the most popular and widely used
bootloaders in Linux. It supports a wide range of operating systems, provides a user-
friendly interface, and offers advanced features. GRUB supports features like dual-
booting, customizable boot menus, and compatibility with various filesystems. It is
highly flexible and is the default bootloader in many Linux distributions.

LILO-
LILO or Linux Loader is another well-known bootloader that has been used in Linux
for a long time. It is known for its simplicity and reliability. LILO can handle various
disk layouts and supports multiple boot options. While it may not have as many
advanced features as GRUB, LILO is still a solid choice, especially for users seeking a
straightforward and stable bootloader.
Prepared by- Amitabh Srivastava
Linux Boot Process

Prepared by- Amitabh Srivastava


Linux Boot Process
STEP-1- BIOS

BIOS is an acronym for Basic Input/Output System. In other words, the BIOS can load
and run the MBR (Master Boot Record) boot loader. When we first turn on our system,
the BIOS first implements a few integrity checks of the SSD or HDD.
After that, the BIOS finds, loads, and runs the boot loader function, which can be
detected in the MBR. Then, the boot loader function is loaded into memory.

Step-2- MBR

MBR is an acronym for Master Boot Record and is liable to load and run the GRUB boot
loader. MBR is placed in the first bootable disk sector, which is generally /dev/sda,
relying on our hardware.

Prepared by- Amitabh Srivastava


Linux Boot Process
STEP-3- GRUB

GRUB is known as GRand Unified Bootloader. It is the classic bootloader for almost all
the latest Linux systems. The splash screen of GRUB is often the initial thing we see
when we boot our system.

It contains a general menu where we can choose some portions. We can use our
keyboard to choose the one we wish our system to initiate with if we have multiple
installed kernel images. The latest kernel image is chosen by default.

The splash screen will delay for some seconds for us to choose options. It will load the
kernel image (default) if we don't. In several systems, we can see the GRUB
configuration file at /etc/grub/conf or /boot/grub/grub.conf.

Prepared by- Amitabh Srivastava


Linux Boot Process
STEP-4- KERNEL

Often, the kernel is called the code of an operating system. In this boot process stage,
the kernel mounts the base file system that was chosen that is set up in the file,
i.e., grub.conf.

Then, it runs the /sbin/init function, which is always the initial function to be run. We
can confirm it with its PID (process id), which should be always 1.

Then, the kernel creates a temporary base file system with the help of initrd (Initial
RAM Disk) until the actual file system is mounted.

Prepared by- Amitabh Srivastava


Linux Boot Process
STEP-5- INIT

init is the parent of all processes and it is executed by the kernel and is responsible for
starting all other processes.
At this stage, our system runs runlevel programs. It would find an init file, generally
detected at /etc/inittab, to determine the run level of Linux. Modern Linux systems use
systemd to select a run level rather. There are six runlevels in the Linux-
Runlevel 0- halt
Runlevel 1- single-user mode
Runlevel 2- multiuser, without NFS
Runlevel 3- Full multiuser mode
Runlevel 4- unused
Runlevel 5- X11
Runlevel 6- reboot Prepared by- Amitabh Srivastava
Linux File System Structure
▪ Linux file system has a hierarchal file structure as it contains a root
directory and its subdirectories.

▪ All other directories can be accessed from the root directory.

▪ The directory structure of Linux is well-documented and defined in


the Linux FHS (Filesystem Hierarchy Standard).

▪ To access the sequentially deeper directory names of the directory


linked by '/' forward slash like /var/spool/mail and /var/log. These
are known as paths.
Prepared by- Amitabh Srivastava
Top-level directories

Prepared by- Amitabh Srivastava


touch command
touch command is used to create empty files and also to modify the timestamp of a file.
Options-
touch file1.txt
It will create an empty file called ‘file1.txt’.
touch file1.txt file2.txt file3.txt
It will create three empty files in current directory called ‘file1.txt’, ‘file2.txt’ and ‘file3.txt’.

touch -- -file.txt
It will create an empty file as ‘-file.txt’ whose first character is hyphen (-). in current directory.
touch –a file1.txt
It will change the access time of file called ‘file1.txt’ and set it as current time.
(use ‘stat <filename>’ command to check the access time of file)

touch –m file1.txt
It will change only the modification date & time of file ‘file1.txt’ to current date and time.
touch –t 201210280605 file1.txt (format used- YYYYMMDDHHMM)
It will change the access & modification date & time of file ‘file1.txt’ to 2012-10-28 06-05.

touch –r oldfile newfile


Prepared by- Amitabh Srivastava
It will change the modification date & time of newfile as per oldfile.
mkdir command
mkdir command is used to create directories/folders.
It can create multiple directories at once as well.

Options-
mkdir mydir
It will create the directory “mydir” in the current directory.

mkdir –p one/two/mydir
The –p option allows the creation of parent directories if required. So, the above
command will create directories one and two if they do not exists and then finally
create mydir inside the directory two.

Prepared by- Amitabh Srivastava


Changing ‘root’ user password
▪ To change the ‘root’ user password in wsl Ubuntu, use the following command-

wsl –u root

Prepared by- Amitabh Srivastava


Switching to ‘root’ user

Prepared by- Amitabh Srivastava


rm command
rm command is used to remove files, directories and links from a specific directory.
By default, it does not remove directories. It works silently so we should be careful while deleting any
file/directory using ‘rm’ command.
Example-
rm file1.txt-> this command will delete file called ‘file1.txt’.
Options-
rm file1 file2 file3
This will delete all three mentioned files.

rm –i <filename(s)>
It will interactively confirm the deletion of file from the user.

rm –f <filename>
If the file is write-protected, it will not ask for the confirmation and forcefully deletes the file.

rm –r <dirname> or rm –R <dirname>
It performs a tree-walk and delete all the files and sub-directories recursively from the current directory.

rm-- <-filename>
It will delete the file whose first character is hyphen (-). Prepared by- Amitabh Srivastava
rmdir command
rmdir command is used to remove empty directories.
By default, it does not remove directories. It works silently so we should be careful while deleting any
file/directory using ‘rm’ command.

Options-
rmdir dir1
This will delete single directory ‘dir1’ only if it is empty.

rmdir dir1 dir2 dir3


This will delete directories ‘dir1’, ‘dir2’ and ‘dir3’ only if they are empty.

rmdir –v dir1 dir2 dir3 (verbose mode)


This will delete directories ‘dir1’, ‘dir2’ and ‘dir3’ only if they are empty and also displays the message of removal.

rmdir –p dir1/d1/d2/d3
This will delete the entire given directory structured only if it is empty(no files/subdirectories should be inside).

Prepared by- Amitabh Srivastava


Creating / Merging Files
▪ cat command is used to create file or merge two or more files in Linux.
Examples-
cat > myfile.txt (press ctrl+c to save the file)

cat file1 file2


It will display the contents of file1 and file2 but not save them in any other file.

cat file1 file2 > file3


It will send/redirect the output of cat command to file ‘file3’.

cat file1 file2 >> file3


It will send/redirect the output of cat command to file ‘file3’ and appended the output to file3.

Merging Commands
▪ We can also execute two or multiple commands in a single line.
▪ To do so separate the commands with semicolon (;) symbol.
Prepared by- Amitabh Srivastava
Some Useful Commands
▪ mv file1 file2
It will change the name (rename) of ‘file1’ to ‘file2’.

▪ mv file1 dir1/file2
It will move the file ‘file1’ into the directory ‘dir1’ and set the filename as ‘file2’.

▪ pwd
It will display the absolute path of current directory. (Present Working Directory)

▪ whoami
It will print the logged in username.

▪ man <command>
It will display the manual/help of the given command. ‘spacebar’ to move screen forward, ‘b’ to move
screen backword and ‘q’ to quit from manual.

▪ file <filename>
It will display the file type information along with filename.

▪ file –b <filename>
Prepared by- Amitabh Srivastava
Will display the file type information in brief mode. Removes the filename from output.
cp Command
▪ cp command is used to create copy of file(s).

▪ Syntax-
cp source_file destination
▪ This command creates a copy of the source_file at the specified destination.

▪ Examples-

▪ cp a.txt b.txt
This command contains two file names, it copies the contents of the first file
(a.txt) to the second file (b.txt). If the second file (b.txt) doesn’t exist, it is
created, and the content is copied into it. However, if the second file (b.txt)
already exists, it is overwritten without warning.
Prepared by- Amitabh Srivastava
cp Command- Examples
▪ Examples-

▪ cp a.txt b.txt c.txt dir1/


This command contains three file names and a directory (dir1). it copies the
contents of all the three files (a.txt, b.txt and c.txt) to the specified directory
(dir1).

▪ cp –R dir1 dir2 or cp –r dir1 dir2


This command contains two directory names (dir1 and dir2). With –R or –r
option, it copies all the files from the source directory (dir1) to the destination
directory (dir2).

▪ To Interactive copying with a warning before overwriting the destination file, we can
use –i option with cp command.
Prepared by- Amitabh Srivastava
Practice Question
▪ Create the files and directories as per the below tree diagram-

Prepared by- Amitabh Srivastava


ls command
▪ The ls command will show the full list or content of your directory.
▪ By default, it will display the files in ascending order of their names.

Options-

ls-l
It will show the list of files / directories in a long list format in total 7 respective
columns.

1st column- permissions on the file.


2nd column- number of links.
3rd column- owner of the file.
4th column- group that owns the file.
5th column- size of the file in bytes.
6th column- last modification date and time.
7th column- name of the file / directory. Prepared by- Amitabh Srivastava
What is total in ls command ?
▪ The size of one memory block in Linux is generally 1024 bytes (1KB).

▪ When any file or directory is created it occupies at least 4KB of memory


space which means 4 blocks.

▪ The Total represents the memory blocks occupied by the listed


directories and files.

Prepared by- Amitabh Srivastava


ls command…cont.
Options-

ls –a
In Linux, hidden files start with . (dot) symbol and they are not visible in the regular directory.
This option will enlist the whole list of the current directory including the hidden files.

ls –i or ls--inode
Shows the index number of each file and directory.

ls –lh
This command will show you the file sizes in human readable format.

ls –lS
It will display the files in descending order (highest at the top) according to their size.

ls –lr
It will display the files in reverse order. Prepared by- Amitabh Srivastava
find command
find command is used to find files and directories in a Linux file system.
Options-
-name <filename>
Search files with specific filename.

-inum <inode_number>
Search file having specified i-node / index number.

-empty
Search only empty files and directories.

-perm <permission_in_number>
Search files and directories having specified permissions.

-user <username>
Search files owned by specified username.

-type f/d
Display regular files with ‘f’ and directories with ‘d’.
Prepared by- Amitabh Srivastava
locate command
locate is an utility command used to search files and directories quickly. It is more convenient and
effective than ‘find’ command.
locate command doesn’t search the entire file system, but it looks through a regularly updated file
database called ‘mlocate’ or ‘plocate’ in the system. To update the locate database, ‘updatedb’
command is used.
If any file created after updating the locate database, it will not include in the search result.
Example-
locate file1-> this command will search and display all files containing the ‘file1’ pattern in the filename.
Options-
locate file1 | less
This will be convenient way to see the output if there are large number of files in the output. ‘Spacebar’ is used to
move forward, ‘b’ key to move backward and ‘q’ key to quit.

locate –c file1 or locate--count file1


Display only the count of files containing ‘file1’ pattern.

locate file1 –n 10 or locate file1 –l 10 or locate file1--limit 10


Limit the number of search results. Prepared by- Amitabh Srivastava
locate command...cont.
Options-

locate –i <filename> or locate –ignore-case <filename>


Ignores the case of filename.

locate-0 <filename> or locate--null <filename>


Displays the result containing the specified pattern but omits newline character.

locate –h or locate--help
Displays the usage of locate command with its available options.

Prepared by- Amitabh Srivastava


more command
▪ more command is used to display the contents of text files on command window (terminal window).

▪ It facilitates to display the contents one screen/page at a time in case of large files along with the
display ratio (in percentage).
Options-
more <filename>
It will display the contents one screen/page at a time.

more –d <filename>
It will display the help to the user regarding navigation controls at the end of every screen.
“[Press space to continue, ‘q’ to quit.]”

more-5 <filename>
It will display 5 number of lines per screen of the given filename.

more +10 <filename>


It will start displaying the contents after leaving 10 lines of the given filename.

more –p <filename> Prepared by- Amitabh Srivastava


It will first clear the screen and then start displaying the contents of given filename.
less command
▪ less is used to display the contents of a text file one screen/page at a time.
▪ It is fast because for large files, it does not access the entire file at a time but access the contents
page-by-page.
▪ G- Moves to the end of the file
▪ g- Moves to the beginning of the file.
▪ /pattern is used to search the pattern in a file.
▪ PageUp, PageDown, Up Arrow and Down Arrow keys are working as usual.
▪ To quit from ‘less’ utility, we must need to press the ‘q’ key.
Options-
less <filename>
It will display the contents one screen/page at a time.
less –E <filename>
It will automatically exit when reaching at the end of the file.
less-F <filename>
Automatically exits if the complete file can be displayed on the first screen.
less-N <filename>
It will display the content of the given filename along with the line numbers. Prepared by- Amitabh Srivastava
history command
▪ history command is used to view previously executed commands.
▪ Supported by BASH and Korn shells and not available under Bourne Shell.
▪ Stored under .bash_history file in user’s home directory.

history
It will display the list of previously used commands.

history 5
It will display the last/recent 5 commands used. (including this command)

!!
It will execute the last command used.

!<command_number>
It will display and excutes the the command from history at given command_number.
Example- !1201

history –d <command_number>
It will delete the command from history at given command number position.
Prepared by- Amitabh Srivastava
head command
▪ head command is used to display the top N number of lines from the given input.
▪ By default, it will print first 10 lines from the given file/input.
Options-
head myfile
It will display the first 10 lines of file called ‘myfile’.

head –n 7 <filename> or head –7 <filename>


It will display the first 7 lines from the given filename.

head –c <num_of_chars> <filename>


It will display mentioned number of characters/bytes from the beginning of the given filename.
Example- head –c 8 myfile.txt

head –q <file 1> <file 2>.....<file N>


This option is used with multiple files. If used, it would not show the filename. For long files, user may confuse that
where first file contents are finished and contents of next file starts.

head –v <filename>
It will always displays the filename before the contents of the file. Prepared by- Amitabh Srivastava
head Examples
▪ Examples-
head –n-7 file1.txt
It will ignore last 7 lines from the file ‘file1.txt’.

head <file1> <file2> <file3>


It will display the first 10 lines from both the files along with the filenames.

head –q <file1> <file2> <file3>


It will display the first 10 lines from both the files but ignores the filenames.

head –n 5 <file1> <file2>


It will display the first 5 lines from both the files along with the filenames.

Prepared by- Amitabh Srivastava


tail command
▪ tail command is used to display the last N number of lines from the given input.
▪ By default, it will print last 10 lines from the given file/input.
Options-
Extra feature not in ‘head’ command-
tail myfile
It will display the last 10 lines of file called ‘myfile’. tail +5 <filename> OR tail –n +5 <filename>
It will display the contents starting from line
tail –n 7 <filename> or tail –7 <filename> number 5 up to end of the file.
It will display the last 7 lines from the given filename.

tail –c <num_of_chars> <filename>


It will display mentioned number of characters/bytes from the end of the given filename.
Example- tail –c 8 myfile.txt
Watch a file for changes-
tail –q <file 1> <file 2>.....<file N>
This option is used with multiple files. If used, it would not tail-f <filename>
show the filename. It will display all the newly added lines to standard
output (monitor).
tail –v <filename>
It will always displays the filename before the contents of the file. Prepared by- Amitabh Srivastava
head & tail Applications
Q.1
▪ Print lines between 3 and 7 from file ‘file1’. Means the actual lines need to be display
are 4, 5 and 6.
▪ To achieve this we can use the pipe symbol to redirect/send the output of one
command to another command.
▪ Solution-
tail +4 file1 | head-3
Or
head-6 file1 | tail-3

Q.2
▪ Print the most recently 3 modified files from the current directory.
▪ Solution-
ls –t | head-3
Prepared by- Amitabh Srivastava
id command
▪ Prints the User and Group IDs of currently logged user if username is omitted.
▪ If username is given, will display the User and Group IDs of given username.

Options-

$ id –g or id –group
Prints only the effective group id.

$ id –G or id –groups
Prints all the group ids user belongs to.

$ id –n or id--name (for ugG)


Prints the group names instead of group ids.

$ id-u
Prints only the user id of currently logged user.

$ id <username>
Prints all the User and Group IDs of specified username. Prepared by- Amitabh Srivastava
uname command
▪ Prints the system information.

Options-

$ uname-s $ uname-o
Prints the kernel name. Prints the operating system name.

$ uname –n $ uname-a
Prints the host name. Prints all the system information.

$ uname –r $ uname-p
Prints the kernel release. Prints the processor type.

$ uname-v
Prints the kernel version with release date.

$ uname-m
Prints the machine’s hardware type.
Prepared by- Amitabh Srivastava
date & timedatectl command
date command displays the current date and time, including the abbreviated day name,
abbreviated month name, day of the month, the time separated by colons, the time
zone name, and the year.

timedatectl command lets you set your time, date, and time-zone for your system clock.

Options-
timedatectl list-timezones
It will display the list of all time zones.

sudo timedatectl set-timezone Asia/Kolkata


It will set the time-zone as “Asia/Kolkata”.

Prepared by- Amitabh Srivastava


date command
▪ date command is used to display the current system date and time information.

Options and Examples-

date
It will display the current system date and time. The format is-

date –u
It will display the system date and time in GMT(Greenwich Mean Time)/UTC(Coordinated Universal
Time).

date –d “02/03/2010” or date--date “02/03/2010”


It will display the given date in the date format.

Prepared by- Amitabh Srivastava


date command...contd.
Options and Examples-

date--date “2 years ago”


It will display the date and time 2 years ago from the current system date and time.

date--date “5 minutes ago”


It will display the date and time 5 minutes ago from the current system date and time.

date--date “10 seconds ago”


It will display the date and time 10 seconds ago from the current system date and time.

date--date “2 months ago”


It will display the date and time 2 months ago from the current system date and time.

date--date “next tuesday” or date--date “tomorrow”


It will display the date on next Tuesday or of tomorrow.

date--date “5 days”
It will display the date 5 days after the current system date and time. Prepared by- Amitabh Srivastava
date command...contd.
Options and Examples-

$ cat datefile.txt $ date--file=“datefile.txt”


Sep 23 2010
Nov 27 2022
Oct 28 2009

Prepared by- Amitabh Srivastava


Date- Format Specifiers
Following are the format specifiers used to display the date and time information in a specific format-

To use these specifiers, follow the


syntax-
$ date +format-specifier

Examples-
$ date +%D
11/28/23

$ date +”Today is- %B %d %Y”


Today is- November 28 2023
Prepared by- Amitabh Srivastava
cal command
It is used to display the calendar on Linux terminal.

Options and examples-

cal
It will display the calendar of current month.

cal <mm> <yyyy>


It will display the calendar of given/selected month and year.

cal <yyyy>
It will display all the months of given year.

cal –j <yyyy>
It will display the calendar of given year in Julian calendar format. In Julian calendar date is not reset to
1st after every month.

Prepared by- Amitabh Srivastava


bc command
▪ Stands for Basic Calculator used for command line calculations.
▪ It can perform simple arithmetic operations to complex calculations including mathematical functions.

Options and examples-

$ echo ‘20+5’ | bc
$ echo ‘scale=2; 3/2’ | bc

$ echo ‘a=5; b=12; a+b’ | bc

$ echo 'a[1]=10; a[2]=20; a[1]+a[2]' | bc

$ echo 'a=0; while(a<=5) { print a; a+=1; print "\n";}' | bc

$ echo 'x=45; sqrt(x)' | bc $ echo 'x=5674; length(x)' | bc


Returns the no. of digits in x.
$ bc-i
Starts interactive mode. To quit from interactive mode type ‘quit’.
Prepared by- Amitabh Srivastava
Adding User in ‘sudo’ Group
▪ Adding a user in sudoers list / sudo group allows the user to run commands with
admin privileges.

▪ To add any user in sudo group, just give the following command-

▪ Syntax- adduser <username> <groupname>

▪ Example- adduser user100g2 sudo

▪ To confirm if the user is successfully added in sudo group, just open the /etc/group
file and see the added username entry against the sudo group.

▪ $ cat /etc/group

Prepared by- Amitabh Srivastava


chmod command
▪ chmod (Change Mode) command is used to change the access mode of a file.
▪ It actually allows to set the permissions on a file/directory like who can read the file, who can edit the file
contents and who can execute the file (if file is executable).
▪ Symbols “r”, “w” and “x” are used to represent the read, write and execute permissions, respectively.
▪ These letters are collectively form a tri-group.
▪ There are 3 tri-groups or triads as-
• “User/Owner” (u) “Group” (g) “Other” (o)
▪ All the three permissions (r, w and x) can be set on any triad (u,g,o).
▪ Octal numbers used to represent these permissions are-
Read (4) [100] Write (2) [010] Execute (1) [001]
Example-
To give permissions r, w and x to the owner and r and x to the group
And r to others on file file1.txt-
$chmod 754 file1.txt OR $chmod u+rwx,g+rx,o+r file1.txt

$chmod u-x,g-x,o-wx file1.txt (to remove permissions, use- sign) Prepared by- Amitabh Srivastava
chmod Operators

Examples-

$ chmod u=rwx file1


It will apply read, write and execute permissions to the given file for the owner only.

$ chmod g=rw,o-x file1


It will apply read and write permissions to the given file for the group and removes execute permission
from others. Prepared by- Amitabh Srivastava
chmod Options
Options and examples-

$ chmod –v 744 <directory/filename>


It will apply the given permissions to the directory/file and also display a message indicating permission
changes.

$ chmod –R 744 dir1


It will apply the given permissions to directory dir1 recursively to all the files and directories.

$ chmod u+rwx <directory/filename>


It will apply read, write and execute permissions to the given file/directory for the owner only.

$ chmod go-w <directory/filename>


It will remove write permission from the given file/directory for the group and others only.

$ chmod u+rw, go+r <directory/filename>


It will apply read, write and execute permissions to the given file/directory for the owner only.
And, also apply read permission to the group and others on the specified directory/file.
Prepared by- Amitabh Srivastava
Default Permissions- umask
▪ Full permission set for a file is 666 ( read, write permission for all )
▪ Full permission set for a directory is 777 ( read, write and execute permissions )

▪ umask value is responsible to set default permissions for files and directories created
by the user.

▪ Default umask value is set to 022.

▪ umask value is subtracted from the full file permission and full directory permission.

▪ Therefore, when user creates any-


▪ File- it has the default permissions 644
▪ Directory- it has the default permission 755

▪ Default umask value can be defined under user’s home directory .bashrc file.
Prepared by- Amitabh Srivastava
chown command
▪ chown command is used to change the owner or group of a file/directory.
▪ Only root or sudo users can run this command.

Options-
chown <username> <filename/directory>
It will change the current user/owner of the file/directory to the given username.

chown <username>:<groupname> <filename/directory>


It will change the current owner and group of the file/directory to the given username and groupname.

chown :<groupname> <filename/directory>


It will change only the current group of the file/directory to the given groupname.

chown -c <username>:<groupname> <filename/directory>


This option (-c) will display a confirmation message after changing the ownership. (same as-v)

chown -v <username>:<groupname> <filename/directory>


The verbose option (-v) will display a message on the screen after changing the ownership.Prepared by- Amitabh Srivastava
chgrp command
▪ chgrp command is used to change the group ownership of a file/directory.
Options-
chgrp <groupname> <filename>
It will change the current group of the file to the given groupname. The groupname must be exist and can
be checked within /etc/group file.

chgrp <groupname> <file...1> <file...2> <file...3> <file...N>


It will change the current group of all the files to the given groupname.

chgrp <groupname> <directory>


It will change the current group of the directory to the given groupname but does not change the group
of files and directories inside it.

chgrp -R <groupname> <directory>


It will change the current group of the directory to the given groupname and also recursively change the
group ownership of all folders and files inside it.

chgrp -c <groupname> <file/directory>


Prepared by- Amitabh Srivastava
It will display a confirmation message after changing the group ownership.
Advanced Packaging Tool (apt)
▪ The apt command is a powerful command-line tool, known as Ubuntu’s Advanced Packaging
Tool (APT).

▪ It is used to install new software packages, upgrade existing software packages, update the
package list index, and even upgrade the entire Ubuntu system.

▪ Actions like installation and removal of packages are logged under the /var/log/dpkg.log file.

▪ Examples-

✓ Installing packages-
sudo apt install <package_name>

✓ Removing packages-
sudo apt remove <package_name>
adding the --purge option with remove will remove the package along with its
configuration files as well, so use it with caution. Prepared by- Amitabh Srivastava
Advanced Packaging Tool (apt)
▪ Examples-

✓ Updating package index-


To update the local package index with the latest changes made in the
repositories, type the following command-
sudo apt update

✓ Upgrading packages-
To upgrade your system, first update your package index and then perform the
upgrade as follows-
sudo apt update
sudo apt upgrade

Prepared by- Amitabh Srivastava


Advanced Packaging Tool (apt)
▪ Options-

✓ $ apt list
List packages based on package names

✓ $ apt show <package_name>


Show package details
Examples-
apt show tree
apt show ncal

✓ $ apt-file list <package_name>


Show package details
Examples-
apt-file list tree Prepared by- Amitabh Srivastava
dpkg Command
▪ dpkg is a package manager for Debian-based systems.

▪ It can install, remove, and build packages, but unlike other package management
systems, it cannot automatically download and install the dependencies required for
that package.

▪ APT is newer than the dpkg.

List packages- dpkg –l (more readable than ‘apt list’ command)

List package installed files- dpkg –L <package_name>

Installing a package- dpkg –i <package_name> (.deb file is required)

Removing a package- dpkg –r <package_name>


Prepared by- Amitabh Srivastava
df command
▪ Disk Free utility provides valuable information on disk space utilization.
▪ Displays information about file system’s disk space usage on the mounted file system.

Options-
df
It displays information about all the mounted file systems like total size, used space, usage percentage, and the
mount point.

df <filename>
It will display the mount information of the given filename.

df -h
It will display the disk space usage in human readable form (in KB/MB/GB).

df --total
It will display the grand total of disk space usage at the end.

df --help
It will display the help on ‘df’ command.
Prepared by- Amitabh Srivastava
du command
▪ Disk Usage utility provides information of a directory or file space usage.
▪ Displays information about the storage consumption of files and directories.
Options-
du
It displays the disk usage information of current directory. (by default, numbers in KB)

du <filename>
It will display the storage information of the given filename.

du -h
It will display the disk space usage in human readable form (in KB/MB/GB).

du --time <filename>
It will display the disk space usage along with the last modification time of the given filename.

du -c <dirname> OR du --total <dirname>


It will display the disk space usage along with the total disk usage of the given directory.

du --help
It will display the help on ‘du’ command.
Prepared by- Amitabh Srivastava
Linux
System Administration

[ ST.2 ]

Prepared by- Amitabh Srivastava


User Management
▪ A user is an entity in Linux OS that can manipulate files and perform several other
operations.
▪ Each user has a unique id.
▪ ID of ‘root’ user (Superuser) is always 0.
▪ System user IDs starts from 1 to 999.
▪ Local user IDs starts from 1000 onwards.
▪ User Management Commands (‘root’ user)-
• passwd
• useradd
• usermod
• groupadd
• groupdel
• userdel
• id Prepared by- Amitabh Srivastava
Primary Group of User
▪ When a user is created, a primary group for that user is also automatically created
which is the same name used in creating the user.

▪ Creating a user is automatically creating a primary group for the user. So, a user is
automatically a member of its primary group.

▪ For example, if I create a user Kavita, Kavita group is automatically a primary group
for the user Kavita.

▪ If you create a new user and open the “/etc/passwd” file where the attributes of
users are found, you will notice in one of the fields, a primary group ID is
automatically assigned to the user.

▪ Similarly, open the “/etc/group” file where the group information is stored, you will
by default see the name of the group as the username with the corresponding group
ID in the file. Prepared by- Amitabh Srivastava
Secondary Group of User
▪ The secondary group is the second, third, n.. group that a user is added
to.

▪ In other words, the secondary group is the other group a user is added
to.

▪ A secondary group is usually created by the administrator.

▪ No user can have more than one primary group but a user can be added
to more than one secondary group.

Prepared by- Amitabh Srivastava


passwd command
▪ It is used to change/modify the password of an existing user account through command line.
▪ The ‘root’ user can change the password of any user account but a normal user can change the
password only for his/her account.
Options-
passwd
If given without any options or username, it will ask to change the account password of currently logged user.
passwd <username>
It will change the password of the specified username.
passwd –l <username>
It will lock the password of specified user so that user cannot login to his/her account.
passwd –u <username>
It will unlock the password of specified user so that user can login to his/her account.
passwd –d <username>
It will delete the password of the specified user. Means, user can login without password.

passwd –e <username>
It will enforce the user to change the account password on next login attempt. Prepared by- Amitabh Srivastava
passwd command...(cntd.)
▪ All the password information is stored in a file /etc/shadow.
▪ User password expiry information can be viewed using command ‘chage –l <username>’
Options-
passwd –n <no. of days> <username>
It will change the minimum number of days between password change. Means, if the no. of days is set to 2, and user
changes its own password one time then he/she cannot change its own password until 2 days have passed. By
default, this value is set to ‘zero’ (0) means, user can change its own password at any time.

passwd –S <username>
It will show the password status of the user in 7 fields.
First field- User’s login name
Second field- Indicates if user has a locked password (L), has no Password (NP), or has a usable password (P).
Third field- Date of the last password change.
Next four fields are the minimum age, maximum age (enforce to change the password), warning period, and
inactivity period for the password. These ages are expressed in days.

Prepared by- Amitabh Srivastava


chpasswd command
▪ Routine bulk password resets are a common need in large organizations, server
management, and web hosting companies.
▪ Manually updating passwords for numerous users is inefficient and prone to error.
▪ The chpasswd command in Linux offers a streamlined solution for updating user
passwords, either in bulk or individually.
▪ It allows system administrators to change passwords for multiple users
simultaneously.
▪ It simplifies the process of changing passwords by reading username-password pairs
from an input or a file and applying the changes automatically.

Example:1- To change a single user password

Syntax- echo ‘username:password’ | chpasswd


$ echo ‘user001:12345’ | chpasswd
Prepared by- Amitabh Srivastava
chpasswd command: Example-2
▪ Create a text file and write the username and passwords in the following format-
username1:password1
username2:password2
…..
…..
usernameN:passwordN

▪ Now, save the file and give the chpasswd command with following syntax-
chpasswd < filename

Example- To change multiple user passwords


$ cat > users-passwords.txt
user001:user001@123
user002:user002@xyz
user003:321@user003
$ chpasswd < users-passwords.txt OR
$ cat users-passwords.txt | chpasswd Prepared by- Amitabh Srivastava
adduser command
▪ It is used to create a new user account through command line interactively.

Prepared by- Amitabh Srivastava


Benefits of ‘adduser’ command
▪ Home directory is automatically created under /home directory.

▪ Ownership of user for user & group assigns to the home directory.

▪ Prompt is properly configured for the user.

▪ Default files can be copied from /etc/skel directory.

▪ Primary group is also created for the user with same name as
username.
Prepared by- Amitabh Srivastava
useradd command
▪ It is used to add/create new user accounts through command line.
Options-

useradd –c <comment> <username>


It will add a new user and add the specified comment in /etc/passwd file.

useradd –d <homedirectorypath> <username>


It will add a new user and set the home directory as specified.

useradd –m <username>
It will add a new user and creates the home directory as specified.

useradd –u <userid> <username>


It will create new user with specified user id.

useradd –e <yyyy-mm-dd> <username>


It will create new user and set the account expiry date as mentioned.

useradd --help
It will display the help section of useradd command.
Prepared by- Amitabh Srivastava
usermod command
▪ It is used to change/modify the properties of an existing user through command line.
▪ After creating a user, if you want to change its home directory or group information, etc. then you can
use ‘usermod’ command.
Options-
usermod –c “Local user” <username>
It will add a comment “Local user” for the given username. Comments are stored in 5th field of /etc/passwd file.
usermod –d /home/ram ram001
It will change the default home directory of user ram001 to /home/ram. This new home directory must be created
separately. Home directories are stored in 6th field of /etc/passwd file.
usermod –e 2023-12-25 <username>
It will set the expiry date for the given username. Expiry dates are stored inside /etc/shadow file.
Check expiry information using command- ‘chage –l <username>’
usermod –g <newgroup> <username>
It will change the primary group for the given username to <newgroup>. Group-id is stored in 4th field of /etc/passwd
file. All group names are stored in /etc/group file.
usermod –aG sudo <username>
It will add the given username to the ‘sudo’ group. ‘id’ command is used to check the group information.
Prepared by- Amitabh Srivastava
usermod: Practice Exercise
▪ Create a new user account by name: testuser001
▪ Now, do the following-
▪ Change testuser001 username to test_user1
▪ Set test_user1 home directory to /home/test_user1
▪ Add test_user1 to sudo group to grant him administrative priviliges.
Solution-
Change testuser001 username to test_user1.
$ sudo usermod –l test_user1 testuser001

Set test_user1 home directory to /home/test_user1


$ sudo usermod –d /home/test_user1 –m test_user1

Add test_user1 to sudo group to grant him administrative priviliges.


$ sudo usermod –aG sudo test_user1

Prepared by- Amitabh Srivastava


chage command
▪ It is used to manage user’s password expiry information and account aging information.
▪ With chage command, administrators can view and modify password expiry details, set
mandatory password change intervals and specify account expiry date.
Options-
$ chage –l <username>
It will display the account aging information of specified user.

$ chage –d 2024-12-31 newuser001


Will set the last password change date to the specified date in the command for user ‘newuser001’.

$ chage –E 2025-12-31 newuser001 use -1 with –E option to remove the expiration date.
Will set the account expiration date to 31st December 2025 for user ‘newuser001’.

$ chage –W 3 newuser001
Will set the warning period (in days) before the password expires.
$ chage –M 5 newuser001 user –m for minimum no. of days between password change.
Will set the maximum number of days between password change.

Prepared by- Amitabh Srivastava


chage: Practice Question
You are administering a Linux system and need to manage password expiration policies for a
user named test_user1. Perform the following actions using the chage command-
1. Set the maximum password age for test_user1 to 6 days.
2. Set the minimum password age for test_user1 to 3 days.
3. Set the password expiration warning to 14 days before the password expires.
4. Set the account to expire on 28th October 2025.

Solution-

1. $ sudo chage -M 6 test_user1

2. $ sudo chage -m 3 test_user1

3. $ sudo chage -W 14 test_user1

4. $ sudo chage -E 2025-10-28 test_user1 Prepared by- Amitabh Srivastava


groupadd command
▪ It is used to add a new group.
▪ Using groups, we can group together a number of users, and set privileges and
permissions for the entire group.
Options-

groupadd <grpname>
It will create the specified group.

groupadd –g <grpid> <grpname>


It will create the specified group with mentioned group id.

groupadd –r <grpname>
It will create the specified system group. The IDs of system groups are chosen from a
range 1 to 999 defined for system groups in the configuration file. The IDs of normal
users start from 1000. Prepared by- Amitabh Srivastava
groupdel command
▪ It is used to delete an existing group.
Options-

groupdel <grpname>
It will delete the specified group.

groupdel –f <grpname>
It will delete the specified group even if it is the primary group of a user.

groupdel –h or groupdel --help


It will display the help of groupdel command with all its options.

Prepared by- Amitabh Srivastava


userdel command
▪ It is used to delete an existing user account.
Options-

userdel <username>
It will delete the specified user account.

userdel –f <username>
It will delete the specified user even if the user still logged in.

userdel –r <username>
It will also delete the user’s home directory.

userdel –h or userdel --help


It will display the help of userdel command with all options.
Prepared by- Amitabh Srivastava
Check for User’s Home Directory Path

Prepared by- Amitabh Srivastava


Blocking User Account
▪ To block a user’s account, follow the below steps-

Step-1-
Login as ‘root’ user.

Step-2-
Run the following command-
# chsh –s /usr/sbin/nologin <username>

Step-3-
Exit from ‘root’ user and try to login with blocked username.

Prepared by- Amitabh Srivastava


Blocking Account with Customize Message
▪ To block a user’s account with customize message, follow the below steps-
Step-1-
Login as ‘root’ user.

Step-2-
Create a file with name ‘security’ under /home and write following contents in it-
#!/usr/bin/tail +2
This account has been temporarily blocked due to SECURITY BREACH!
Please contact your system administrator or call at 0512-06321.

Step-3-
Run the following command-
# chmod 755 security
Step-4-
Run the following command-
# chsh –s /home/security <username> Prepared by- Amitabh Srivastava
Practice Question
▪ Add 3 users as Ram, Shiva and Shree.

▪ Secondary group of Shiva and Shree should


be ‘tester’.

▪ Block Ram’s account with the following


message-
Your account has been locked!
Please contact your system administrator. Prepared by- Amitabh Srivastava
Vi or Vim Editor
▪ Vi editor is elaborated as visual editor.
▪ Available to every Unix system.
▪ Available to all Linux distros.
▪ Improved version of Vi is known as Vim.
▪ Used to create simple text files in Linux using command line interface.
▪ Two vi modes- Command mode and Insert mode.
▪ To switch from command mode to insert mode press “i” key.
▪ To switch from insert mode to command mode press ‘Esc’ key.
▪ By default, Vi editor starts in command mode. Prepared by- Amitabh Srivastava
Prepared by- Amitabh Srivastava
Pico/Nano Editor
▪ Nano is a user-friendly, simple and WYSIWYG(What You See Is What You Get) text editor.
▪ Unlike vim editor or any other command-line editor, it doesn’t have any mode.
▪ Available as PICO editor in modern Linux distros.
▪ To create a file using nano editor, give the following command-
▪ nano <filename>

▪ To save a file- Ctrl + o

▪ To cut the selection- Ctrl + k

▪ To paste- Ctrl + u

▪ To search and replace- Ctrl + \

▪ To read/insert another file text- Ctrl + r

▪ To copy and paste- Ctrl + 6, Alt + 6 and Ctrl + u

▪ To quit from nano editor- Ctrl + x


Prepared by- Amitabh Srivastava
grep command
▪ Grep (Global Regular Expression Print) is a filter command that searches a given pattern of characters
in a file.
▪ It displays all the lines containing that given pattern.
Options and Examples-
grep linux about.txt
It searches the given pattern “linux” in about.txt file.

grep –i linux about.txt


It will displays all the lines containing the given pattern by ignoring the case of pattern.

grep –c Linux about.txt


Will display only the count of matches.

grep –l “linux” *
It will display the filenames that matches that pattern.

grep –w “user” about.txt


It will display the lines having match pattern as a whole word and not as a substring (default behaviour).
Prepared by- Amitabh Srivastava
grep command...contd.
Options and Examples-

grep -o “Linux” about.txt


It will display all the matched patterns only and not the entire line containing matched pattern.

grep –n “Linux” about.txt


It will displays all the lines containing the given pattern along with the line numbers.

grep –v “Linux” about.txt


It will displays all the lines NOT containing the given pattern.

grep “^Linux” about.txt


It will displays all the lines containing the given pattern at the beginning of line.

grep “OS.$” about.txt


It will displays all the lines containing the given pattern at the end of line.

grep –e “Linux” –e “Linus” about.txt OR grep –E “Linux|Linus” about.txt


Used to search multiple patterns in a file. Prepared by- Amitabh Srivastava
grep command...contd.
Options and Examples-
grep –A1 “Linus” about.txt
It will displays all the lines containing the given pattern along with one line below the
matched one.

grep –B1 “Linus” about.txt


It will displays all the lines containing the given pattern along with one line above the
matched one.

grep –C1 “source” about.txt


It will displays all the lines containing the given pattern along with one line above and
one line below the matched one.
Prepared by- Amitabh Srivastava
grep –f option
grep –f will take the pattern from the given file (one pattern per line) and searches
inside the given file.
It will display all the lines containing that pattern.

Example-

$ grep –f patterns.txt about.txt

Prepared by- Amitabh Srivastava


grep –r OR -R option
grep –r / -R will look for the given pattern in the given directory recursively in all files
and displays all the lines containing matched pattern.

Example-

$ mkdir dir1; cd dir1

$ cat file1.txt $ cat file2.txt


Linux is a user friendly OS. Windows is also a user friendly OS.
It is multiuser OS. It is developed by Microsoft Corp.
It has many distros.

$ cd ..

$ grep –r user *

Prepared by- Amitabh Srivastava


grep command with pipe
Examples-

ls –l | grep “file”
It will display all the files having name containing the given pattern.

ls –l /etc | grep “Nov”


It will display all the files from /etc directory having November in the month field.

To display all the files having extension .txt. To display total no. of directories.
ls –l | grep “.txt$” ls –l | grep –c ^d

To display all the directories only.


ls –l | grep “^d”

To display the user information of the users those names start with the word user.
cat /etc/passwd | grep ^user

To display the group information named “sudo”.


cat /etc/group | grep “sudo” Prepared by- Amitabh Srivastava
Grep- Practice Question
Filename: Poem.txt
In the world of code, there's the Linux way,
Then freedom to build, create, and play.
Those who know its power, feel the delight,
That open-source world, a beacon of light.
The kernel hums quietly, efficient and true,
Then comes the magic, as commands break through.
Those who choose Linux, know what it's worth!

Question-
Write a grep command to display all those lines with there line numbers containing
words either ‘then’ or ‘those’.

Solution-

cat poem.txt | grep -niE "then|those"


Prepared by- Amitabh Srivastava
egrep command
▪ Egrep (Extended Global Regular Expression Print) is a filter command that searches a
file for a given pattern of characters.
▪ It is faster than grep.
▪ Difference between grep and egrep is that egrep uses extended regular expressions.
▪ All the options are same as grep command.

Example- $ cat file1.txt | egrep “[A-Za-z]$” Output-

Prepared by- Amitabh Srivastava


Regular Expressions in Linux
Symbol Description
. It is called wild card character, matches any one character other than
newline.
^ Matches the beginning of a line/string.
$ Matches the end of line/string.
* Matches zero or more occurrences of the preceding character.
? Makes the preceding character optional, matching zero or one.
{} Used to define a specific quantity of characters to match.
\ Used to escape following character.
[] Used to match any of the characters.
+ Matches one or more occurrences of the preceding character.
Prepared by- Amitabh Srivastava
Regular Expressions with egrep
▪ $ egrep “^Linux” about.txt
Matches all lines start with ‘Linux’.

▪ $ egrep “OS.$” about.txt


Matches all lines end with ‘OS.’.

▪ $ egrep “[aeiou]” about.txt


Matches all the vowels.

▪ $ egrep “[a-h]” about.txt $ egrep “\^” about.txt


Matches lowercase letters from a to h. Matches all ^ symbols.

▪ $ egrep “[0-9]{4}” about.txt $ egrep “Linu[a-z]?” about.txt


Matches digits from range 0 to 9 with Matches all “Linux” and “Linus” because ? means
minimum length of 4. “Zero or More”.

▪ $ egrep “[Pp]a*s*words” about.txt


Matches both spellings “Passwords” and “Paaswords”. Prepared by- Amitabh Srivastava
egrep: Assignment-1
▪ Suppose you have a file named “contacts.txt” in which some email addresses are stored-

▪ Write the egrep command with correct regular expression that matches only valid email addresses.

▪ Solution-

$ egrep '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]+$' contacts.txt


Prepared by- Amitabh Srivastava
egrep: More Examples
▪ File named “mypasswords.txt” contain some passwords-

▪ Select passwords with at least one digit in it-


$ egrep “[0-9]” mypasswords.txt

▪ Select passwords with at least one uppercase letter in it-


$ egrep “[A-Z]” mypasswords.txt

▪ Write egrep command to select passwords with exactly 8 characters long-


$ egrep “^.{8}$” mypasswords.txt

▪ Select passwords with at least 8 characters long-


$ egrep “^.{8,}” mypasswords.txt

▪ Select passwords with at least one special symbol-


$ egrep “[^a-zA-Z0-9]” mypasswords.txt
Prepared by- Amitabh Srivastava
egrep: Assignment-2
▪ File named “mypasswords.txt” contains following passwords-

▪ Write the egrep command with correct regular expression pattern that matches the
passwords having exactly one uppercase and one lowercase letter in it.

▪ Solution-

$ egrep '[a-z].*[A-Z]|[A-Z].*[a-z]' mypasswords.txt


Prepared by- Amitabh Srivastava
sort command
▪ It is used to sort a file and arrange the records/lines in a particular order.
▪ By default, files are sorted by assuming the contents are ASCII (characters).
Features-
• Sorts the contents of a text file line-by-line.
• Supports sorting alphabetically, by number, by months and in reverse order.
• It can also remove duplicate records.
• Supports sorting on columns/fields. (default field separator is blank space)

Options & Examples-


$ sort names $ sort –f names (ignore-case)

Prepared by- Amitabh Srivastava


sort command...contd.
Options & Examples-
File having numbers Sorting is not correct-
$ sort –r names (numeric sort)- $ sort numbers
Sort in reverse order. $ cat > numbers

Sorting is now correct-


$ sort –n numbers

Prepared by- Amitabh Srivastava


sort command...contd.
➢ Sorting on Columns-

$ cat items $ sort –k2 items $ sort –nk1 items

➢ Storing sort result in a file-

$ sort –k2 –o <output filename> <input filename>

OR

$ sort <filename> > <output filename>

Prepared by- Amitabh Srivastava


sort command...contd.
➢ Check whether a file is sorted-

$ cat fruits $ sort –c fruits $ cat fruits $ sort –c fruits


no output means sort- fruits-3- disorder- Banana
file is sorted!

➢ Remove duplicates-

$ cat fruits $ sort –u fruits

Prepared by- Amitabh Srivastava


sort command...contd.
➢ Sort by Month names-

$ cat months $ sort –M months $ sort --month-sort months

➢ How to list the files/directories on ascending order of their month?

Solution-
$ ls –l | sort –Mk6

Prepared by- Amitabh Srivastava


Sort on Multiple Columns
Example File- items.txt To sort the rows by column 1-
$ sort –k1,1n items.txt

To sort the rows by column 1


and then by column 2
in reverse order-
$ sort –k1,1n –k2,2r items.txt

Prepared by- Amitabh Srivastava


Sort on Multiple Columns…cont.
Example File- items.txt To sort the rows by column 1 and then by
column 2 in reverse and then by column 3 in
descending order-

$ sort –k1,1n –k2,2r –k3,3nr items.txt

Prepared by- Amitabh Srivastava


Sort: Practice Exercise
▪ Create a file data.txt with following data-

Q1. Sort the file by Profession in descending


order and then by age in descending order?
$ sort -t "," -k3,3r -k2,2nr data.txt

Q2. Sort the file by country in ascending order


and then by name in descending order?
$ sort -t "," –k4,4 –k1,1r data.txt
Q3. Sort the file by country in ascending order and then by profession in descending order
and then by age in descending order?
$ sort -t "," –k4,4 –k3,3r –k2,2nr data.txt
Prepared by- Amitabh Srivastava
wc command
▪ Stands for Word Count and used mainly for counting of characters in a file.
▪ By default, it produces 4 columnar output- no. of lines, word count, char(byte) count and filename,
respectively.
Options-

wc <filename>
It will produce 4 columnar output- no. of lines, word count, char(byte) count and filename, respectively.

wc <filename1> <filename2> <filename..N>


It will produce 4 columnar output for all the filename mentioned including totals of all files.

wc –l <filename1>
It will display no. of lines for the mentioned filename.

wc –w <filename1>
It will display no. of words for the mentioned filename.

wc –c <filename1> OR wc –m <filename>
It will display no. of characters (bytes) for the mentioned filename. Prepared by- Amitabh Srivastava
wc command...contd.
Options-

wc –L <filename>
It will display the length of longest line (characters count) of mentioned filename.

wc--version
It will display the version, author and copyright information of wc command.

Examples-

ls | wc –l
To count the no. of files and directories in the current directory.

cat /etc/passwd | wc –l
To count the total no. of users.

Prepared by- Amitabh Srivastava


cut command
▪ Used to cut out the specific sections from each line of files and display the result to standard output.
▪ It slices the line by character(byte) or column/field position and extracts the text.
Options and Examples-

$ cat Employees $ cut –c 1 Employees

$ cut –c 1,4 Employees $ cut –c 3-8 Employees

Prepared by- Amitabh Srivastava


cut command...contd.
Options and Examples-

$ cut –c 3- Employees $ cut –c -3 Employees

$ cut –d “,” –f 3 Employees $ cut –d “,” -f3,5 Employees

Prepared by- Amitabh Srivastava


cut command...contd.
Options and Examples-

$ cut –d “,” -f 3- Employees $ cut --complement –d “,” -f 3- Employees

$ cut –d “,” –f 3- --output-delimiter=“|” Employees $ cut --version


Displays the version, author and copyright
information.

$ cat /etc/passwd | cut –d “:” –f1


Display the names of all the user accounts.

Prepared by- Amitabh Srivastava


uniq command
▪ The uniq command displays duplicate lines only once from the sorted records/file.

Options and Examples-

$ cat fruits.txt $ sort fruits.txt | uniq $ sort fruits.txt | uniq -c


Displays the count of repeated lines.

$ sort fruits.txt | uniq –d (--repeated)


Print only lines that are repeated and discards non-duplicate lines.

Prepared by- Amitabh Srivastava


uniq command…cont.
Options and Examples-

$ cat fruits.txt $ sort fruits.txt | uniq –D (--all-repeated)

$ sort fruits.txt | uniq –u (--unique)


Print only non-duplicate lines.

$ sort fruits.txt | uniq –i (--ignore-case)


Ignores the case sensitivity of characters.

$ uniq --version
Prints the version details of uniq command.
Prepared by- Amitabh Srivastava
Practice Question-1
Consider the file ‘Employees’-

Question-
Display the fourth field (Dept.) in descending order only with the uniq values without
the heading (Dept).

Solution-
$ cut -d "," -f4 Employees | tail +2 | sort -r | uniq
Prepared by- Amitabh Srivastava
Practice Question-2
Consider the file ‘Emp’-

Question-
Display the employees firstname and Department whose firstname start with letter ‘D’
and department name starts with letter ‘S’ AND also the employees whose firstname
starts with either ‘G’ or ‘R’ and department name starts with letter ‘A’. Header row not
required.
Solution-
$ cut -d "," -f 1,4 Emp | tail +2 | grep -E '^D.*,[S]|^[GR].*,[A]'
Prepared by- Amitabh Srivastava
uptime command
▪ This command is used to find out the time for which the system has been in active(running) state.
▪ It returns a set of values that include the current time, and the amount of time the system is in
running state, number of users currently logged into, and the load time for the past 1, 5 and 15
minutes respectively.

$ uptime

Options-

$ uptime -p
Returns the total no. of hours and minutes of system active.

$ uptime -s
Returns the starting time by when the system is running.

$ uptime –V or uptime --version


Returns the version information.
Prepared by- Amitabh Srivastava
Process Management
▪ Involves controlling and monitoring the processes running on a Linux system.
▪ Includes managing process resources, scheduling processes to run on the CPU, and terminating
processes when necessary.
▪ Understanding the different types of processes, their states, and the available commands for process
management are important for managing processes effectively.
▪ Process-
A process is an instance of a program currently running on a computer system. In Linux, processes
are managed by the operating system's kernel, which allocates system resources and schedules
processes to run on the CPU.
In Linux, there are two types of processes-
Foreground processes- Processes that require input from the user and are characterized by their
interactivity. For instance, a foreground process would be a running Office application on the Linux
system.
Background processes- Non-interactive operations carried out in the background that do not require
any participation from the user. Antivirus software is an example of a Background Process.

Process can be a system process or user process. System processes are initiated by the kernel, while
users initiate User processes. Prepared by- Amitabh Srivastava
Process States in Linux
▪ The state of a running process at any point in time is called the context of a process.
▪ In Linux, a process can be in one of the five states-

▪ Running-
✓ The process is currently executing on the CPU.

▪ Sleeping-
✓ The process is waiting for a resource to become available.

▪ Stopped-
✓ The process has been terminated by a user.

▪ Zombie-
✓ The process has completed execution but has not yet been cleaned by the system.

▪ Orphan-
✓ The parent process of the current process has been terminated.
Prepared by- Amitabh Srivastava
Process Identity
▪ A process is identified by the process id (PID).

▪ When a process is created, it is assigned a unique identifier called the


process id.

▪ This process id is useful when the process needs to be referred to by a


user or by a program.

▪ The process id is used to specify processes to the operating system.

▪ When a process needs to be controlled, the process id is given as an


argument to the system calls/commands to identify the process.
Prepared by- Amitabh Srivastava
Process Management Commands
▪ Linux provides several commands for managing processes.
▪ Few important commands are-

Prepared by- Amitabh Srivastava


Foreground Processes

▪ By default, every process that you start runs in the foreground. It gets its
input from the keyboard and sends its output to the screen.

▪ For example, if you give the vi file1.txt command, it creates a new


process to run the vi- editor application which runs in the foreground
and wait for the user input.

▪ No other commands can be run in the foreground until the process


related to vi-editor finishes.

Prepared by- Amitabh Srivastava


Background Processes

▪ A background process runs without being connected to your keyboard.

▪ If the background process requires any keyboard input, it waits.

▪ The advantage of running a process in the background is that you can


run other commands; you do not have to wait until it completes.

▪ The simplest way to start a background process is to add an ampersand


(&) symbol at the end of the command.

Prepared by- Amitabh Srivastava


ps command
▪ The ps (Process Status) command is used to find the process ID (PID) of the process that needs to be
managed.
▪ For example, if we want to kill/terminate a process, we first need to know the PID of that exact
process.
▪ It reads the process information from the virtual files in /proc file-system.
Options and Examples- $ ps –u Shows username with memory & CPU
usage.
$ ps
$ ps -A or $ ps -e
Shows the processes of the current shell. Shows all running processes.

$ ps -x
Output contains four columns- Shows processes owned by currently logged user.
PID- the unique process ID
TTY- terminal type that the user is logged into $ ps -C <command_name>
TIME- amount of CPU in minutes and seconds Shows process by command name.
that the process has been running
CMD- name of the command that launched the process.
Prepared by- Amitabh Srivastava
top command
▪ The top command is used to display real-time information about processes running on a system,
including CPU and memory usage.
▪ It allows users to monitor the processes in real-time.
▪ it will open an interactive command mode where the top-half screen will contain the statistics of
processes and resource usage.
▪ The lower-half screen contains a list of the currently running processes.
▪ To kill a process, highlight it with up/down arrow keys and press ‘k’ key.
▪ To exit from the top view press ‘q‘ key.

$ top

Prepared by- Amitabh Srivastava


top command- Summary Area
➢ Uptime and Load Averages-
First line consists of program name, current time, length of time since last boot, total no. of users logged
in and system load average over the last 1, 5 and 15 minutes.

➢ Tasks and CPU State-


Second line shows the total tasks classified as running, sleeping, stopped and zombie.
Third line shows CPU state percentages based on the interval since last refresh. The percentages are
divided into following categories-
us- time running un-niced user processes sy- time running kernel processes
ni- time running niced user processes id- time spent in the kernel idle handler
wa- time waiting for I/O completion hi- time spent servicing hardware interrupts
si- time spent servicing software interrupts
st- time stolen from this virtual machine by the hypervisor

➢ Memory Usage-
Fourth line reflects Physical memory classified as total, free, used and buffered/cached.
Fifth line reflects Virtual memory classified as total, free, used and available.

Prepared by- Amitabh Srivastava


top command- Process Details
Top command displays a list of processes in following different columns-

PID: Unique Process ID given to each process.


User: Username of the process owner.
PR: Priority given to a process while scheduling.
NI: ‘nice’ value of a process.
VIRT: Amount of virtual memory used by a process.
RES: Amount of physical memory used by a process.
SHR: Amount of memory shared with other processes.
S: State of a process.
D- Uninterruptible process
R- Running
S- Sleeping
T- Stopped
Z- Zombie
%CPU: Percentage of CPU used by the process.
%MEM: Percentage of RAM used by the process.
TIME+: Total CPU time consumed by the process.
Command: Command used to activate the process. Prepared by- Amitabh Srivastava
top command options
➢ $ top –b
It starts top in Batch mode, which could be useful for sending output from top to other programs or to a
file. In batch mode, top will not accept input from user and runs until the iterations limit you've set
with the `-n’ option or until killed by pressing ctrl + c.
Example-
$ top –bn 3 this will run top in batch mode for only three iterations.

➢ $ top –d
Specifies the delay in seconds between screen updates.
Example-
$ top –d 2 this will refresh top screen every 2 seconds.
$ top--delay 2 same as –d option.

➢ $ top –E
Instructs top to force summary area memory to be scaled as kibibytes/mebibytes/gibibytes/tebibytes.
Example-
$ top –E k displays the memory usage summary in kilobytes. Prepared by- Amitabh Srivastava
nice/renice command
▪ nice and renice commands are used to manage the priority of a running process.

▪ The nice command is used to modify the priority of a process. It assigns a lower priority to a process
to reduce its resource usage.

▪ The renice command is used to modify the priority of an already running process. It can increase or
decrease the priority of a process which depends on the given value.

▪ To set the priority of a process-


$ nice -10 sleep 30 &

▪ To check the nice value of a process-


$ ps -el | grep sleep

The eighth column value is the nice value of the process.

▪ To set the nice value of already running process-


$ renice –n 2 -p 881
Prepared by- Amitabh Srivastava
fg/bg/jobs command
▪ To start any job/command/process in the background just add the ampersand (&) symbol at
the end of that command.
$ sleep 50 &
It will start sleep/delay timer of 50 seconds in the background.

▪ To check the background running jobs/processes, use jobs command-


$ jobs -l
It will show all the running and stopped jobs with their job number and PIDs.

▪ To resume the background job in foreground, use fg command-


$ fg <job_number>

▪ To stop a running foreground job, press CTRL+Z key. (complete/terminate CTRL+C)

▪ To resume the stopped job in background, use bg command-


$ bg <job_number>
Prepared by- Amitabh Srivastava
Terminating a Process (KILL)
▪ To stop a process in Linux, use the 'kill’ command. kill command sends a signal to the
process.

▪ There are different types of signals that you can send. However, the most common one is ‘kill
-9’ which is ‘SIGKILL’.

▪ The default signal is 15 (SIGTERM).

▪ You can list all the signals using:


$ kill –L

• Syntax for killing a process is-


kill pid

▪ If process ignore the default kill signal and doesn’t stop, you can send the SIGKILL signal to
terminate the process.
$ kill -9 pid Prepared by- Amitabh Srivastava
‘trap’ Command
The trap command run a certain piece of code in response to a particular signal. It
monitors signals and activates the specific code when the selected signal is received.

Example 1-
$ trap “echo You pressed CTRL+C” SIGINT

Example 2-
$cat > myscript.sh
echo “Hello Tech Guy!”
echo “How are you?”

$ chmod 744 myscript.sh

$ trap “./myscript.sh” SIGINT


OR
$ trap “./myscript.sh” SIGTSTP Prepared by- Amitabh Srivastava
sed command
▪ SED is a Stream Editor to perform inserting, deleting, searching and replacing text in a text file without
using any editor. In simple words, it is a powerful text manipulation utility.
Options and Examples-
$ sed ‘s/\bis\b/was/’ about.txt
It will replace the 1st occurrence of “is” as a
whole word with “was” in each line. \b is
used for word boundary.

$ sed ‘s/Linux/LINUX/’ about.txt


It will replace first occurrence of word “Linux” with “LINUX” in each line in the file called about.txt. Here, ‘s’ specifies
the substitution operation. The “/” is the delimiter symbol.

Note- By default, the sed command replaces the first occurrence of the pattern/word in each line and it wouldn’t
replace the second or other occurrences in the same line.

$ sed ‘s/is/was/g’ about.txt


It will replace all the occurrence of pattern “is” with “was” in each line in the file called about.txt. Here, “g” specifies
the “global” replacement.
Prepared by- Amitabh Srivastava
sed command...contd.
Options and Examples-

$ sed ‘s/is/was/2’ about.txt


It will replace second occurrence of word “is” with “was” in each line in the file called about.txt. Here, ‘2’ specifies
the 2nd occurrence of the pattern.

$ sed ‘s/is/was/2g’ about.txt


It will replace the pattern starting from second occurrence to all the occurrences in each line.

$ sed ‘3 s/is/was/’ about.txt


It will replace the first occurrence of the matching pattern in 3rd line only.

$ sed ‘3 s/is/was/2’ about.txt


It will replace the 2nd occurrence of the matching pattern in 3rd line only.

$ sed ‘3 s/is/was/g’ about.txt


It will replace all the occurrences of the matching pattern in 3rd line only.

$ sed ‘s/is/was/p’ about.txt


It will replace the 1st occurrence of the matching pattern in each line and prints the replaced line twice on the
terminal. If a line doesn’t have any matching pattern then it prints only once. Prepared by- Amitabh Srivastava
sed command...contd.
Options and Examples-

$ sed ‘1,3 s/is/was/’ about.txt


It will replace 1st occurrence of word “is” with “was” in lines 1 to 3.

$ sed ‘3,$ s/is/was/’ about.txt


It will replace 1st occurrence of word “is” with “was” from 3rd line to last line ($) in the file about.txt.

$ sed ‘5d’ about.txt $ sed ‘2d;4d’ about.txt


It will delete 5th line from the file about.txt. It will delete only 2nd and 4th line from the file
about.txt

$ sed ‘2, 4d’ about.txt


It will delete lines from 2nd to 4th from the file about.txt.

$ sed ‘2, $d’ about.txt


It will delete 2nd to last line from the file about.txt.

$ sed ‘/Linus/d’ about.txt


It will delete all the lines in which the matching pattern ‘Linus’ is found. Prepared by- Amitabh Srivastava
sed command...contd.
Options and Examples-

$ sed ‘G’ about.txt


It will insert a blank line after each line.

$ sed ‘2G’ about.txt


It will insert a blank line after 2nd line.

$ sed ‘G;G’ about.txt


It will insert 2 blank lines after each line.

$ sed ‘/Linux/G’ about.txt


It will insert a blank line after each line in which matching pattern ‘Linux’ found.

$ sed '/^$/d' about.txt


Deletes all blank/empty lines.

$ sed 's/^/ /' about.txt


It will insert 5 spaces at the beginning of every line.

$ sed = about.txt | sed 'N;s/\n/\t/’


Inserts line number at the beginning of each line. \t is used for a tab between number and sentence.Prepared by- Amitabh Srivastava
sed: Practice Question-1
Consider the below file fruitcolor.txt-

=====OUTPUT====➔

Q. Write the sed command to replace all occurrences of “Red” with


“Maroon” only on lines containing the pattern “Cherry” or “cherry”?

Solution-
$ sed '/[cC]herry/s/Red/Maroon/' fruitcolor.txt
Prepared by- Amitabh Srivastava
Sed: Practice Question-2
Consider the below file fruitcolor.txt-

=====OUTPUT====➔

Q. Write the sed command to add “ish” at the end of each line except for
the color “Orange”?

Solution-
$ sed '/Orange/!s/$/ish/' fruitcolor.txt
Prepared by- Amitabh Srivastava
Sed: Practice Question-3
Consider the below file fruitcolor.txt-

=====OUTPUT====➔

Q. Write the sed command to add heading in the file


as “Fruit Color”?
Solution-
$ sed -i '1i Fruit Color' fruitcolor.txt
The –i option edits the file in place and 1i tells the sed command to insert before the
first line. Prepared by- Amitabh Srivastava
Sed: Creating Backup Copy
Consider the below file fruitcolor.txt-

=====OUTPUT====➔

Q. Write the sed command to add heading in the file as “Fruit Color”
and also creates a backup copy as fruitcolor.txt.bak?
Solution-
$ sed –i.bak '1i Fruit Color' fruitcolor.txt
The –i option make the changes inside file and .bak creates a backup copy of original file
fruitcolor.txt as fruitcolor.txt.bak. Prepared by- Amitabh Srivastava
Sed: Practice Question-4
Q. Create a pipeline using grep and sed command to display all the lines
containing the pattern “error” (as a whole word and ignore case sensitivity)
from file /var/log/syslog and also insert a blank line after every matched
line.

Solution-
$ grep -iw "error" /var/log/syslog | sed 'G'

Prepared by- Amitabh Srivastava


Sed: Practice Question-5
Q. Consider below a file “contacts.txt”. Now write a pipeline command to
display the lines containing the mobile numbers only and also replace all
mobile number digits with # symbol.

===OUTPUT===➔

Solution-
$ grep '[0-9]' contacts.txt | sed 's/[0-9]/#/g'
Prepared by- Amitabh Srivastava
Sed: Practice Question-6
Q. Consider below a file “contacts.txt”. Now write a pipeline command to
display the lines containing the mobile numbers only and also replace all
mobile number digits with # symbol.

===OUTPUT===➔

Solution-
$ egrep "[0-9]{9}" contacts.txt | sed 's/[0-9]/#/g'

Prepared by- Amitabh Srivastava


Practice Question
Q. Consider below a file “contacts.txt”. Now write a pipeline command to
display only the domain names.

===OUTPUT===➔

Solution-
$ grep '@' contacts.txt | cut -d "@" -f 2

Prepared by- Amitabh Srivastava


awk command
▪ Awk is a scripting language used for manipulating data and generating reports. The awk command
programming language requires no compiling and allows the user to use variables, numeric functions,
string functions, and logical operators.

▪ Awk operations include-


• Scans a file line by line
• Splits each input line into fields
• Compares input line/fields to pattern
• Performs action(s) on matched lines

Options and Examples-


$ cat Employees $ awk ‘{print}’ Employees

Prepared by- Amitabh Srivastava


awk command...contd.
Options and Examples-
$ awk ‘/Sales/ {print}’ Employees (you can omit {print}) $ awk –F “, ” '{print $3, $5}' Employees

$ awk '{print NR, $2, $5}' Employees

$ awk –F “, ” '{print $2, $NF}' Employees


(NF- Last Field)
(NR- Record No.)

$ awk 'END { print NR }' Employees


5 (displays the line count)

$ awk 'length($0) > 28' Employees


Displays the line whose length is greater than 28 chars.

$ awk '{ if($5 == 45000) print}' Employees


Displays only those records where value of 5th field(Salary) is equal to 45000. Prepared by- Amitabh Srivastava
Awk: Practice Question-1
Consider the below file ‘Employees’-

Q. Write an awk statement/command to print all the employee names only whose
salary is greater than 45k?

Solution-
$ awk -F ", " '{ if($5 > 45000) print $3}' Employees

Prepared by- Amitabh Srivastava


Awk: Practice Question-2
Consider the below file ‘Employees’-

Q. calculate the total salary of all employees in the 'Sales' department?

Solution-
$ awk -F', ' '$4 == "Sales" {sum += $5} END {print sum}' Employees
53000

$ awk -F', ' '$4 == "Sales" {sum += $5} END {print "Total salary of Sales: " sum}' Employees
Total salary of Sales: 53000 Prepared by- Amitabh Srivastava
Awk: Practice Question-3
Consider the below file ‘Employees’-

Q. calculate the average salary of all employees in the ‘IT' department?


Solution-
awk -F', ' '$4 == "IT" {sum += $5; count++} END {print sum/count}' EmpLoyees
82000

$ awk -F', ' '$4 == "IT" {sum += $5; count++} END {print "Average Salary(IT): " sum/count}'
Employees
Average Salary(IT): 82000
Prepared by- Amitabh Srivastava
Awk: Practice Question-4
Consider the below file ‘Employees’-

Q. Print the name and salary of the employee with highest salary?

Solution-
$ awk -F', ' 'max < $5 {max = $5; name = $3} END {print name, max}' Employees
Gopal 86000

$ awk -F', ' 'BEGIN {max = 0} max < $5 {max = $5; name = $3} END {print name, max}' Employees
Gopal 86000

Prepared by- Amitabh Srivastava


Awk: Practice Question-5
Consider the below file ‘Employees’-

Q. Print the name and salary of the employee with minimum salary?

Solution-
$ awk -F', ' 'BEGIN {min = 999999} min > $5 {min = $5; name = $3} END {print name,
min}' Employees
Alok 25000

Prepared by- Amitabh Srivastava


Awk: Practice Question-6
Consider the below file ‘Employees’-

Q. Print the name and salary of the employees belongs to Sales and IT department?

Solution-
$ awk -F', ' '$4 == "Sales" || $4 == "IT" {print $3, $5}' Employees
Alok 25000
Firoz 28000
Ram 78000
Gopal 86000
Prepared by- Amitabh Srivastava
Awk: Practice Question-7
Consider the below file ‘Employees’-

Q. Print the name and salary of the employees belongs to Sales and IT department?
Show your output in the descending order of their salary?
Solution-
$ awk -F', ' '$4 == "Sales" || $4 == "IT" {print $3, $5}' Employees | sort –t “ “ –k2,2nr
Output-
Gopal 86000
Ram 78000
Firoz 28000
Alok 25000 Prepared by- Amitabh Srivastava
Shell Scripting
First Shell Script-
$ cat script1.sh
#!/bin/bash
echo “Hello World!”
The first line #! Is known as Shebang, a special comment which indicates what interpreter to use when
running this script. This comment is optional, if not used, the current shell is used as an interpreter.

$ sh script1.sh
Hello World!

$ ./script1.sh
-bash- ./script1.sh- Permission denied

$ chmod 744 script1.sh


This command sets the executable permission to the owner of the file.

$ ./script1.sh
Hello World! Prepared by- Amitabh Srivastava
User Defined Variables
Assignment with equal-to sign and returns value with $ sign as prefix in variable name.
$ x=10
$ y=20 Spaces are not allowed while assignment.
$ echo $x and $y $ x = 10
10 and 20 x- command not found

Variable names are case sensitive. To unset/delete the variable


$ rad=5 $ unset rad
$ RAD=45
$ echo rad- $rad and RAD- $RAD To make a variable read only
rad- 5 and RAD- 45 $ readonly rad
(read only variable cannot be unset)
Variables names can start with underscore sign.
$ _var1=100
$ echo $_var1
100 Prepared by- Amitabh Srivastava
Built-in Shell Variables
$PWD = Stores working directory name

$HOME = Stores home directory name with absolute path

$SHELL = Stores the path of the shell that is being used

$LOGNAME = Current user’s login name

$USER = Current user’s login name

$HOSTNAME = Stores host name

$PATH = Stores all the paths defined.

Prepared by- Amitabh Srivastava


Special Shell Variables

Prepared by- Amitabh Srivastava


Script to change directory
▪ In shell scripting, if you change the directory using the cd command within a script,
the change will only affect the script's current process, and once the script finishes,
the directory change will not persist in the user's shell session.

▪ To make sure the directory change persists even after the script finishes, you need to
ensure that the cd command is executed in the same shell session as the user
running the script.

▪ The trick is to source the script rather than running it. Sourcing the script will cause it
to run in the current shell session, so any changes to the environment (such as
changing directories) will persist even after the script finishes.

▪ To source the script, use following command-


$ . scriptfilename.sh
Prepared by- Amitabh Srivastava
The expr utility
▪ In shell scripting, is a command-line utility used to evaluate expressions, particularly
for arithmetic operations, string operations, and logical comparisons. It allows you to
perform calculations or check conditions directly within the script.

Prepared by- Amitabh Srivastava


Converting Case Letters
The ‘tr’ (translate) command is used to convert text from lower to upper case and vise
versa.

Example-1:
$ echo “hello how are you?” | tr ‘[:lower:]’ ‘[:upper:]’
HELLO HOW ARE YOU?

Example-2:
$ echo “LINUX IS AWESOME!” | tr ‘[:upper:]’ ‘[:lower:]’
Linux is awesome!

Prepared by- Amitabh Srivastava


Taking Input from User
The ‘read’ command is used to take the input from the user.
Example-3: (myscript.sh)
Example-1: (myscript.sh) read –p “Enter name: “ name
echo “Enter your name: “ echo $name
read name Output-
echo “Your name is: $name” Enter name: Alok Kumar
Alok Kumar

Example-2: (myscript.sh) Example-4: (myscript.sh)


read –p “Enter your name: ” name read –p “Enter name: “ name
echo “Your name is: $name” echo “$name”
Output-
Enter name: Alok Kumar
Alok Kumar
Prepared by- Amitabh Srivastava
Comparison Operators

Number Comparison

String Comparison

Prepared by- Amitabh Srivastava


Conditional Statements (if…else)
Example- Example-
$ cat myscript.sh $ cat myscript.sh

$ sh myscript.sh
or
$ ./myscript.sh

Prepared by- Amitabh Srivastava


Conditional Statements (if…elif)
Example-
read -p "Enter 1st no. " a
read -p "Enter 2nd no. " b

if [ $a -eq $b ]
then
echo "a is equal to b!"
elif [ $a -gt $b ]
then
echo "a is greater than b!"
elif [ $a -lt $b ]
then
echo "a is less than b!"
fi
Prepared by- Amitabh Srivastava
Conditional Statements...contd.
Example- Example-
$ cat myscript.sh $ cat myscript.sh

Solution-

$ sh myscript.sh
or
$ ./myscript.sh
Prepared by- Amitabh Srivastava
Practice Question - 1
Q. Write a script that asks from user to enter an age. If age is less than 14 it should
print “Swimming not allowed” AND if age is greater than or equals to 14, then print
“Eligible for swimming!”

Solution-
read -p "Enter the age: " age

if [ $age -ge 14 ]
then
echo "Eligible for Swimming!"
else
echo "Swimming not allowed!"
fi
Prepared by- Amitabh Srivastava
Practice Question – 2 (and ‘-a’)
Q. Write a script that asks from user to enter an age. If age is greater than or equals to
18 AND less than or equals to 45 it should print “Eligible to Swim!”. Else it should print
“Swimming not allowed!”

Solution-

Prepared by- Amitabh Srivastava


Practice Question – 3 (and ‘&&’)
Q. Write a script that asks from user to enter an age. If age is greater than or equals to
18 AND less than or equals to 45 it should print “Eligible to Swim!”. Else it should print
“Swimming not allowed!”

Solution-
read -p "Enter the age: " age

if [[ "$age" -ge 18 && "$age" -le 45 ]]


then
echo "Eligible to Swim!"
else
echo "Swimming not allowed!"
fi
Prepared by- Amitabh Srivastava
Practice Question – 4 (or ‘-o’)
Q. Write a script that asks from user to enter any number. If number is divisible by 2
OR 5 print “You guess correctly!” else print “Better luck next time!”?

Solution-
read -p "Enter any number: " num

if [ `expr $num % 2` -eq 0 -o `expr $num % 5` -eq 0 ]


then
echo "You guess correctly."
else
echo "Better Luck Next Time."
fi
Prepared by- Amitabh Srivastava
Practice Question – 5 (or ‘||’)
Q. Write a script that asks from user to enter any number. If number is divisible by 2
OR 5 print “You guess correctly!” else print “Better luck next time!”?

Solution-
read -p "Try your Guess: " num

if [[ $num%2 -eq 0 || $num%5 -eq 0 ]]


then
echo "You guess correctly."
else
echo "Better Luck Next Time."
fi
Prepared by- Amitabh Srivastava
Practice Question – 6 (‘&&’ AND ‘||’)
Q. Write a script that asks from user to enter any number. If number is between 10
and 50 OR if number is exactly 100 print “Condition met!” else print “Condition not
met!”
Solution-
read -p "Enter a number: " num

if [[ $num -ge 10 && $num -le 50 ]] || [[ $num -eq 100 ]]


then
echo "Condition met! "
else
echo "Condition not met!"
fi

Prepared by- Amitabh Srivastava


Practice Question – 7
Q. Write a script that asks from user to enter any username. If username exists it
should print “<username> already exists” else it should print “<username> does not
exist” ?
Solution- read -p "Enter the username: " username

# Check if the user already exists


user=`grep -w "$username" /etc/passwd`

if [ "$user" != ' ' ] OR if [ ! –z "$user" ]


then
echo "$username already exists! "
else
echo "$username does not exist!"
fi
Prepared by- Amitabh Srivastava
Practice Question – 8
Q. Write a script that asks from user to enter any word and prints whether it is a
Palindrome or not?

Solution- read -p "Enter any word: " word

rev_word=`echo $word | rev`


echo "Reverse is: $rev_word"
if [ "$word" == "$rev_word" ]
then
echo "$word is a Palindrome!"
else
echo "$word is NOT a Palindrome!"
fi
Prepared by- Amitabh Srivastava
Check if File Exists
Example-

▪ To check whether entered filename exists or not!


read -p "Enter filename: " fname

if [ -s $fname ]
then
echo "File $fname exists!"
else
echo "File $fname does not exist!"
fi

Prepared by- Amitabh Srivastava


Check if Directory Exists
Example-

▪ To check whether entered directory name exists or not!


read -p "Enter directory name: " dname

if [ -d $dname ]
then
echo "Directory $dname exists!"
else
echo "Directory $dname does not
exists!"
fi
Prepared by- Amitabh Srivastava
Case Statement
▪ A case statement in shell script is used when a decision has to be made against multiple
choices.
▪ It is useful when an expression has the possibility to have multiple values.
Example-

Prepared by- Amitabh Srivastava


Case Statement…..cont.
▪ Regular Expression Patterns can also be use to make the options of case.
Example-

Prepared by- Amitabh Srivastava


Practice Question - 1
Q.
Write a shell script that takes a filename as an input and determines its type using a
case statement. The script should handle the following cases-

1. If the file is a directory, print: “This is a directory.”

2. If the file is a regular file, further check:

a) If it is a shell script (ends in .sh), print: “This is a shell script file.”

b) If it is a text file (ends in .txt), print: “This is a text file.”

c) Otherwise, print: “This is some other type of regular file.”

3. If the file/directory does not exist, print: “File/Dir does not exist!.”

Prepared by- Amitabh Srivastava


Practice Question – 1
Solution
read -p "Enter file/directory name: " file case $file in
if [ -d "$file" ] dir)
then echo "This is a directory!"
file="dir" ;;
elif [ ! -s "$file" ] *.sh)
echo "This is a shell script file!"
then
;;
echo "File/Dir does not exist!" *.txt)
exit 1 echo "This is a text file!"
fi ;;
*)
exit 1 is used to terminate the script execution
echo "This is some other type of
with the exit status of 1, which typically indicates
file!"
an error or unsuccessful execution.
;;
exit 0 is used to terminate the script with exit
esac
status of 0 means successful execution. Prepared by- Amitabh Srivastava
Command Line Arguments
▪ In your shell script, apart from user input, we can also use command line
arguments-

Syntax-
./scriptname arg1 arg2 arg3 …

▪ $0, $1, $2, $3, …, $n are known as positional parameters corresponding to the
./scriptname arg1 arg2 arg3, …, argN respectively.

▪ $# is used for total number of command line arguments except the scriptname.

▪ $* means all of the arguments seen as a single word.

▪ $@ means all of the arguments as $* but each parameter is a quoted string.


Prepared by- Amitabh Srivastava
Command Line Arguments- Example1
Example-

$ cat myscript.sh
USAGE=" USAGE: ./scriptname <arg1> <arg2> <arg3> <arg4>"

if [ "$#" != 4 ]
then
echo $USAGE
exit 1
fi

echo "Total Number of Arguments: " $#


echo "List of Arguments: " $@
echo "Name of script that you are running: " $0

$ ./myscript.sh arg1 arg2 arg3 arg4


Prepared by- Amitabh Srivastava
Command Line Arguments- Example2
Example-

▪ $ ./myscript.sh Apple Banana


Syntax used to calculate length of string

Prepared by- Amitabh Srivastava


Practice Code – 1
Q. Write a shell script that accepts two arguments:
1. A filename
2. A number (N)
The script should check if the proper no. of arguments are provided or not.

✓ If the arguments are more than two, it should display the proper message to give only two
arguments.

✓ If no argument is supplied, it should assign following default values-


• Filename- /var/log/syslog
• Default number- 7

✓ If only one argument is supplied, the 2nd argument should take the default number value.

✓ If provided filename exists, display top N lines from that file, if it does not exist display
proper message that “file does not exist!”.
Prepared by- Amitabh Srivastava
Practice Code – 1: Solution
if [ $# -lt 2 -a $# -gt 0 ] elif [ $# -gt 2 ]
then then
if [ -s $1 ] echo "There must be exactly two arguments!"
then exit 1
head -n 7 $1 elif [ -s $1 ]
else then
echo "File $1 does not exist!" echo "Displaying first $2 lines of $filename: "
fi head -n $2 $1
elif [ $# -eq 0 ] else
then echo "File $1 does not exist!"
head -n 7 /var/log/syslog fi

Prepared by- Amitabh Srivastava


Practice Code – 2
Q. Write a shell script that accepts exactly three arguments:

1. A number (num1)
2. Any of the arithmetic operators: plus(+), minus(-), multiplication(\*) or division(/).
3. Another number (num2)

✓ If the arguments are more than three or less than three, it should display the proper
message to the user “Usage ./scriptname <num1> <operator> <num2>”.

✓ The script should perform the calculation (num1 operator num2) and display the
result.

✓ The script should also handle the division by zero.

✓ Write your script using case statements.


Prepared by- Amitabh Srivastava
Practice Code – 2: Solution
if [ $# -ne 3 ]; then \*) echo "$num1 * $num2 = $(($num1 *
echo "Usage: ./scriptname <num1> $num2))" ;;
<operator> <num2>" /)
exit 1 if [ "$num2" -eq 0 ]; then
fi echo "Error: Division by zero is not
allowed."
num1=$1 else
op=$2 echo "$num1 / $num2 = $(($num1 /
num2=$3 $num2))"
fi
case $op in ;;
+) echo "$num1 + $num2 = *) echo "Invalid operator! Use +, -, *, or /" ;;
$(($num1 + $num2))" ;; esac
-) echo "$num1 - $num2 = $(($num1
- $num2))" ;; Prepared by- Amitabh Srivastava
declare Command
The declare command is used to define/declare variables in shell scriping.

Example-

$ cat myscript.sh
declare -i x
x=10/5

echo $x

x=10*2
echo $x

Prepared by- Amitabh Srivastava


While Loop
▪ A while loop is a statement that iterates over a block of code till the condition specified is
evaluated to FALSE.
▪ We can use ‘while loop’ in our program when we do not know how many times the condition
is going to evaluate to TRUE before evaluating to false.
Example- reading contents of a file.

Example- returning numbers from 1 to 10.

Prepared by- Amitabh Srivastava


While loop: Practice Code - 1
▪ Write a shell script to print the table of an entered number using while loop?

Solution-

Prepared by- Amitabh Srivastava


While loop: Practice Code - 2
▪ Write a shell script to print the Fibonacci series up to the given number/limit N?
Solution- read -p "Enter the limit for Fibonacci series: " limit
a=0
b=1
echo "Fibonacci Series up to $limit:"
echo -n "$a $b "
while [ $((a + b)) -le $(($limit)) ]
do
sum=$((a + b))
echo -n "$sum "
a=$b
b=$sum
done
echo Prepared by- Amitabh Srivastava
For Loop
▪ The for loop operate on lists of items.
▪ It repeats a set of commands for every item in a list.

Example- Example-
(in seq, first number is START
point and second
number is END point)

(if 3 numbers are there in


seq, then second number is
considered as step)

Example- Example- (C style syntax)

Prepared by- Amitabh Srivastava


‘break’ & ‘continue’
▪ break and continue are used within loops to alter the flow of the loop and
terminate the loop(break) or skip the current iteration(continue).

Example (break) Example (continue)


i=1 nums="1 2 3 4 5 6 7“

while [ $i -le 10 ] for num in $nums


do do
echo "value of i: $i" if [ $num -eq 3 ]
if [ $i -eq 5 ] then
then continue
break else
fi echo "value of num: $num"
i=`expr $i + 1` fi
done done
Prepared by- Amitabh Srivastava
Example Code-1 (for loop)
Q. Write a shell script that should take an integer number from user and prints it’s table.
If the input is not a valid integer and less than or equals to zero, please show proper
messages to the user? read -p "Enter any number: " num
if [[ "$num" =~ 0 ]]
Solution- then
echo "Can not enter ZERO!"
elif [[ "$num" =~ ^[0-9]+$ ]]
then
for (( i=1; i<=10; i++ ))
do
prod=`expr $num \* $i`
echo "$num x $i = $prod"
done
else
echo "Not an Integer!"
fi Prepared by- Amitabh Srivastava
Practice Code-1 (for loop)
Q. Write a shell script that should ask number of rows from user and print the right-
angled triangle pattern as follows-
Enter rows: 4
*
** Solution-
*** read -p "Enter rows: " rows
****
for ((i=1; i<=rows; i++))
do
for ((j=1; j<=i; j++))
do
echo -n "* "
done
echo
done
Prepared by- Amitabh Srivastava
Practice Code-2 (for loop)
Q. Write a shell script that should ask number of rows from user and print the inverted
right-angled triangle pattern as follows-
Enter rows: 4
****
*** Solution-
** read -p "Enter rows: " rows
*
for ((i=rows; i>=1; i--))
do
for ((j=1; j<=i; j++))
do
echo -n "* "
done
echo
done
Prepared by- Amitabh Srivastava
Practice Code-3 (for loop)
Q. write a shell script to print the right-angled triangle pattern using odd numbers? The
script must ask two things from user- number of rows and starting odd number. If the
entered number is not an ODD number, print proper message to the user.

Enter rows: 4 read -p "Enter rows: " rows for ((j=1; j<=i; j++))
Starting number: 5 read -p “Starting number: " start do
5 echo -n "$num "
7 9 num=$start num=$((num + 2))
11 13 15 done
17 19 21 23 if [ `expr $num % 2` -ne 0 ] echo
then done
for ((i=1; i<=rows; i++)) else
do echo "Please enter an ODD
number!"
exit 1
fi
Prepared by- Amitabh Srivastava
Practice Code-4 (while loop)
Q. write a shell script to print the number of digits in a number entered by the user.

Output read -p "Enter a number: " num


count=0
Enter a number: 3214 temp=$num
The number of digits in 3214 is : 4
while [ $temp -ne 0 ]
do
temp=$((temp / 10))
((count++))
done

if [ $count -eq 0 ]; then


count=1
fi
echo "The number of digits in $num is: $count"
Prepared by- Amitabh Srivastava
Practice Code-5 (while loop)
Q. write a shell script to print the number of digits in a number entered by the user.

Output read -p "Enter a number: " num

Enter a number: 3214 reverse=0


The reverse of 3214 is : 4123 temp=$num

while [ $temp -ne 0 ]


do
digit=$((temp % 10))
reverse=$((reverse * 10 + digit))
temp=$((temp / 10))
done

echo "The reverse of $num is: $reverse"

Prepared by- Amitabh Srivastava


Practice Code-6 (loops)
Q. write a shell script to print all the three-digits Armstrong numbers.
echo "Armstrong numbers-"
Output for (( i=100; i<=999; i++ ))
do
Armstrong numbers- num=$i
153 sum=0
370 temp=$num
371 while [ $temp -gt 0 ]
407 do
digit=$((temp % 10))
sum=$((sum + digit * digit * digit))
temp=$((temp / 10))
done
if [ $sum -eq $num ]; then
echo "$num"
fi
done Prepared by- Amitabh Srivastava
cmp/diff command
cmp command
▪ cmp command is used to compare the two files byte by byte and helps to find out whether the two
files are identical or not.
▪ It reports the location of the first mismatch to the screen if difference is found.
▪ If the files compared are identical, it displays no message and simply returns the prompt.
Example-
$ cmp file1 file2
file1 file2 differ- byte 38, line 1

diff command
▪ diff stands for difference.
▪ It is used to display the differences in the files by comparing the files line by line.
▪ It also tells which lines in one file is to be changed to make the two files identical.
Example-
$ cat names $ cat names2 $ diff names names2

Prepared by- Amitabh Srivastava


Hard Links
▪ Every file on the Linux filesystem, by default, having one single hard link.
▪ The link is between the filename and the actual data stored on the filesystem.
▪ Using “ls –l” command you will observe the number of links in the 2nd column of
output.

▪ To create a new hard link of an existing file, use “ln” command-


$ ln about.txt newabout.txt

▪ When changes are made to any one of them, the other reflects those changes.
▪ The permissions, link count, ownership, timestamps and file content are exactly same
in all of the links and the original file.
▪ Files having hard-links together share same i-node number (use ls –li to see the inode)
▪ If original file is deleted, the data still exists under the hard link. Data will only
remove when all the links will be deleted including original file.

Prepared by- Amitabh Srivastava


Soft Links
▪ Commonly referred to as “Symbolic Links”.

▪ Soft link is a special file points to an existing file.

▪ To create a soft link, use the following command-


$ ln –s file1.txt file1_link.txt

▪ Observe that the letter “l” will be added to the permissions column.

▪ To check the soft link, use ls –l command-

▪ If the original file is deleted, the soft link is broken. This situation is known as a
dangling soft link.

Prepared by- Amitabh Srivastava


exec Command
▪ The exec command replaces the current shell process with the specified command.
Unlike other commands that spawn a new process, exec does not create a new
process — it replaces the current one.

▪ Syntax-
exec [COMMAND] [ARGUMENTS...]

▪ Example-
$ exec top

The above command replaces the current shell with top command. You’ll lose the
shell session— pressing ‘q’ key won’t return you to the shell because it was
replaced. You’ll have to open a new terminal.

Prepared by- Amitabh Srivastava


exec: in shell script
Example1.sh-
#!/bin/bash
echo "Before banner"
exec banner "Hello"
echo "After banner" # this line will never be executed

Output-

“After banner” is never printed because banner replaces the shell running the script.

Prepared by- Amitabh Srivastava


ifconfig command (obsolete)
▪ ifconfig(interface configuration) command is used to configure the network interfaces.
▪ It is used at the boot time to set up the interfaces as necessary.
▪ This command is used to assign the IP address and netmask to the network interface or to enable or
disable a network interface.
Options and Examples-
$ ifconfig -a
Display the information of all the interfaces available.
$ ifconfig -s
Display the short information of all the interfaces available.
$ ifconfig <interface> down
Deactivates/disables the driver for the given interface.
$ ifconfig <interface> up
Activates/enables the driver for the given interface.

$ ifconfig <interface> <ipaddress> netmask <subnetmask>


Set the specified ip-address and netmask to the given interface.
$ ifconfig --help
Prepared by- Amitabh Srivastava
Displays help of ifconfig command.
IP Addresses & Subnet Mask
▪ While configuring the TCP/IP protocol on a computer, the following three TCP/IP
configuration settings require:
• An IP address
• A subnet mask
• A default gateway

▪ The subnet mask is used by the TCP/IP protocol to determine whether a host is on
the local/same network or on a remote network.

▪ A router that is specified on a host, which links the host's subnet to other networks,
is called a default gateway.

▪ To send packets on remote host, it is the responsibility of the router to forward the
packets to the correct subnet.

Prepared by- Amitabh Srivastava


IP Addresses: Networks & Hosts
▪ An IP address is a 32-bit number.
▪ It uniquely identifies any computer or device (such as a printer or router) on a
network.
▪ IP addresses are normally expressed in dotted-decimal format, with four numbers
separated by periods, such as 192.168.123.132.
▪ IP address (in dotted-decimal format)- 192.168.123.132
▪ The binary notation (32-bit number)- 11000000.10101000.01111011.10000100
▪ These 8-bit sections are known as octets.
▪ This number when converted from binary to decimal format becomes-
192.168.123.132.
▪ In every IP address, the first part of an IP address is used as a network address, the
last part as a host address.
▪ In 192.168.123.132 the two parts are 192.168.123 known as Network and 132 is
Host.
▪ OR 192.168.123.0 is network address and 0.0.0.132 is host address.
Prepared by- Amitabh Srivastava
ping command
▪ PING (Packet Internet Groper) command is used to check the network connectivity between two hosts in a
network.
▪ It takes IP address or the URL as input and sends a data packet to the specified address with the message “PING”
and get a response from the server/host.
▪ The time is recorded which is called ‘latency’. Low latency(less time) means faster connection.
▪ Ping uses ICMP(Internet Control Message Protocol) to send an ICMP echo message to the specified host if that
host is available then it sends ICMP reply message.
Options and Examples-
$ ping www.google.com
Sends packets with echo message to the specified host URL.
$ ping -c 2 www.google.com
Sends 2 packets with echo message to the specified host URL.
$ ping -s 40 www.google.com
Sends packets of 40 bytes (+8 bytes) with echo message to the specified host URL.
$ ping -i 2 www.google.com
Sends each packet with the interval of 2 seconds. By default, ping waits for 1 sec. to send next packet.
$ ping –q (press ctrl+c to see) $ ping-V
Display only the summary information. Display the ping version information. Prepared by- Amitabh Srivastava
hostname command
▪ hostname command in Linux is used to obtain the DNS (Domain Name System) name and set the
system’s hostname.
▪ Its main purpose is to uniquely identify over a network.
Options-
$ hostname
Displays the system’s hostname.

$ hostname -f or --fqdn
Displays the system’s Fully Qualified Domain Name (FQDN).

$ hostname -F myhost.txt (need reboot)


Set the hostname as specified under the given filename. (must be ‘root’ or ‘sudo’ user).

$ hostname -h
Displays the help of hostname command.

Prepared by- Amitabh Srivastava


Setting ‘alias’ / ‘fqdn’ hostname
❑ To set the alias hostname, open /etc/hosts file and edit it in following format-
IP_ADDRESS HOSTNAME_ALIAS HOSTNAME
127.0.0.1 MY-LAPTOP LAPTOP-8F40GMIU

to check if the alias name is set properly, test it with ping command.
$ ping MY-LAPTOP

❑ To set the Fully Qualified Domain Name (FQDN) hostname, open /etc/hosts file and
edit it in following format-
IP_ADDRESS FQDN HOSTNAME
127.0.0.1 MY-LAPTOP.example.com LAPTOP-8F40GMIU

to check if the FQDN name is set properly, test it with ping command.
$ ping MY-LAPTOP.example.com Prepared by- Amitabh Srivastava
ip command
▪ ip command is used for network configuration and management.
▪ Allows user to interact with various networking components like network interfaces, routing tables,
addresses, etc.
▪ Is a part of iproute2 package.
▪ Replacement of older commands- ifconfig and route.
Options and Examples-

$ ip addr show (or, in short, you can type command- ip a)


Displays details such as interface names (lo, eth0, wlan0, etc.) MAC addresses, IPV4 and IPV6 addresses, subnet
mask, etc.

$ ip addr show dev eth0


Displays details only the specified device interface eth0.

$ sudo ip link set lo down


Disables the network interface “lo”. After this you cannot connect/ping to your loopback/localhost address.

$ sudo ip link set lo up


Enables the network interface “lo”. After this you cannot connect/ping to your loopback/localhost address.
Prepared by- Amitabh Srivastava
ip command…..cont.
Options and Examples-

$ sudo ip addr add 192.168.0.123/24 dev eth0


Set the new IP-address as specified to the interface eth0. After this command you have to delete the
previously assigned IP-address from eth0 interface, only then the new IP-address will be set.

$ sudo ip addr del 172.30.16.188/20 dev eth0


Deletes the assigned IP-address from the interface eth0.

$ ip route show
Shows all the network routes defined under routing table.

$ sudo ip route add 172.30.25.0/24 via 172.30.16.1 dev eth0


Adds another route to the routing table for interface eth0.

$ sudo ip route del 172.30.25.0/24 via 172.30.16.1 dev eth0


Deletes the specified route from the routing table for interface eth0.

Prepared by- Amitabh Srivastava


netstat command
▪ The netstat command is a tool for monitoring network connections.
▪ It displays active connections, their status, and related statistics.
▪ It shows which ports are open, which services are listening on them, and which IP
addresses are connected to your system.

netstat command options-

-a : Displays all active TCP & UDP connections and listening ports.
-t : Displays TCP (Transmission Control Protocol) connections.
-u : Displays UDP (User Datagram Protocol) connections.
-l : Displays only listening ports.
-n : Displays numerical addresses instead of resolving names.
-r : Displays the routing table information.
Prepared by- Amitabh Srivastava
traceroute command
▪ traceroute command prints the route that a packet takes to reach the host.
▪ This command is useful to know about the route and about all the hops (in between nodes) that a
packet takes.
▪ It sends three packets to each hop.
Options and Examples-
$ traceroute google.com
Displays the route information in different columns.
- First column is the hop count.
- Second column is the ip-address of that hop.
- Then the three times for three packets, respectively.
$ traceroute -f 10 google.com
Instead of first hop, it starts tracing route from the 10th hop.

$ traceroute -n google.com
Do not resolve the IP-addresses to their domain names.

$ traceroute google.com 100


Change the default packet length from 60 bytes to 100 bytes.
Prepared by- Amitabh Srivastava
netplan command
▪ netplan command is used to configure a static IP address or a DHCP IP address.
▪ Configuration file is located under /etc/netplan directory with .yaml extension.
Steps to configure IP address-
Open the configuration file situated under /etc/netplan with extension .yaml with root login or sudo
privilege. The layout of the file looks like this-

network:
version: 2
ethernets:
DEVICE-NAME:
renderer: NetworkManager
match:
name: INTERFACE-NAME
dhcp4: YES/NO
addresses: [IP/NETMASK]
gateway4: GATEWAY-ADDRESS
nameservers:
addresses: [NAMESERVER,NAMESERVER]
Prepared by- Amitabh Srivastava
▪ Edit the above file to fit your networking needs. Save and close the file.
Applying netplan settings
▪ Configure and save the .yaml file.
network:
version: 2
ethernets:
NM-2f3e1107-becd-43d2-852a-eece2133d91a:
renderer: NetworkManager
match:
name: "eth0"
dhcp4: no
addresses: [172.30.16.188/20]
gateway4: 172.30.16.1
nameservers:
addresses: [8.8.8.8,8.8.4.4]

▪ Before applying the changes, first test the configuration with the following command-
$ sudo netplan try
The above command will validate the configuration before applying it. If it succeeds, the configuration
accepted. If it fails, netplan will automatically revert to the previous configuration.
Prepared by- Amitabh Srivastava
nmcli command
▪ nmcli is a command-line tool used to-
➢ View network status
➢ Enable/disable network interfaces
➢ Connect to Wifi
➢ Configure static IP address
➢ Manage Ethernet and VPN connections
▪ Basic Syntax-
nmcli [OPTIONS] OBJECT { COMMAND | help }
Object: e.g. device, connection, network
Command: the action you want to perform
Prepared by- Amitabh Srivastava
nmcli command: Examples
▪ Example-1: to show all network interfaces/connections, their type and state.
$ nmcli device show

▪ Example-2: to show all available Wi-fi networks.


$ nmcli device wifi list

▪ Example-3: to connect to a Wi-fi network.


$ nmcli device wifi connect <"Network_Name"> password <"your_password">

▪ Example-4: to show all active connections.


$ nmcli connection show --active

▪ Example-5: bring an interface/connection UP or DOWN.


$ nmcli connection up static-eth0
$ nmcli connection down static-eth0
Prepared by- Amitabh Srivastava
nmcli command: Examples
▪ Example-6: to create new static IP Ethernet connection.
$ nmcli connection add type ethernet ifname eth0 con-name static-eth0
ip4 192.168.1.100/24 gw4 192.168.1.1

▪ Example-7: to turn ON/OFF networking or Wi-fi.


$ nmcli networking off
$ nmcli networking on
$ nmcli radio wifi off
$ nmcli radio wifi on

Prepared by- Amitabh Srivastava


Installing GCC Compiler
▪ GCC stands for GNU Compiler Collections.
▪ It is used to compile programs written in C and C++ language.
▪ It is an Open Source collection of compilers and libraries.
▪ To install GCC compiler follow the below steps-

$ apt-get update
It will update the packages by downloading the package’s information.

$ sudo apt install build-essential


It will install build-essential packages that contains the GCC compiler and all other essentials used to
compile the program written in C or C++.

$ gcc--version
It will show the version and copyright information of GCC compiler.

$ gcc prog1.c
It will compile the C program written inside prog1.c file. After the successful compilation, it will create an
executable file called a.out.

$ ./a.out
This will run the code just compiled in above step. Prepared by- Amitabh Srivastava
Extracting Substring
▪ Extracting a substring from a string is a basic and common operation of text
processing in Linux.

▪ Using Bash’s Substring Expansion we can easily extract a substring from a


string.

▪ Example:1-
$ str=“LinuxShell”
$ echo ${str:5} Output- Shell

▪ Example:2-
$ str=“LinuxShell”
$ echo ${var:5:3} Output- She
Prepared by- Amitabh Srivastava
Arrays in Shell Script
▪ Arrays in Shell Scripting allow us to store multiple values in a single
variable.

▪ Bash supports two types of arrays-


➢ Indexed arrays
➢ Associative arrays

▪ Elements of an Indexed array are accessed using numeric indices,


starting from 0.

▪ Associative arrays use string keys instead of numeric indices. They


require explicit declaration using declare –A command.
Prepared by- Amitabh Srivastava
Declaring Arrays
▪ The declare keyword can be used to explicitly declare arrays.
▪ The options –a and –A are used to define the type of array.
Example-
$ declare –A assoc_array
$ assoc_array[key]=value

$ declare –a indexed_array
$ indexed_array[0]=value

▪ Option uppercase A is used to declare an associative array while


lowercase a is used to declare an indexed array.
Prepared by- Amitabh Srivastava
Indexed Arrays: Initializing & Adding Elements
▪ Initializing an empty array-
numbers=()
strings=()
names=()

▪ Initializing an array with values-


numbers=(12 34 7)
strings=(“abc” “java” ‘linux’)
names=(“Alok” ‘Gourav’ “Gauri”)

▪ Adding/Appending elements to an array-


numbers+=(1)
strings+=(“hello”)
names+=(“James”) Prepared by- Amitabh Srivastava
Displaying Array & Array Length
Example-
declare -a names
names=("Alok" 'Gourav' "Gauri") Output-
echo "names: ${names[@]}" names: Alok Gourav Gauri
echo "value at index 1: ${names[1]}" value at index 1: Gourav
echo "length of names array: ${#names[@]}" length of names array: 3
echo "----------------------" ----------------------
echo "After deleting 1 element at index 1-" After deleting 1 element at index 1-
unset names[1] names: Alok Gauri
echo "names: ${names[@]}" ----------------------
echo "----------------------" Appending an element-
echo "Appending an element-" names: Alok Gauri Naresh
names+=("Naresh")
echo "names: ${names[*]}" Prepared by- Amitabh Srivastava
Practice Code - 1
Q- Write a shell script to copy all the odd numbers from an existing array to a new
array called “odd_array”?

Solution- original_array=(1 2 3 4 5 6 7 8 9 10)


odd_array=()
for num in "${original_array[@]}"
do
if [[ $((num % 2)) != 0 ]]
then
odd_array+=("$num")
fi
done
echo "Odd numbers: ${odd_array[@]}" Prepared by- Amitabh Srivastava
Practice Code - 2
Q- Write a shell script that takes numbers from an array and create two new
arrays- one having odd numbers and another for even numbers?
Solution- # Original array else
numbers=(12 7 3 8 10 5 19 6) odd_array+=("$num")
even_array=() fi
odd_array=() done

for num in "${numbers[@]}"; do # Output the results


if (( num % 2 == 0 )); then echo "Original array:
even_array+=("$num") ${numbers[@]}"
echo "Even numbers:
${even_array[@]}"
echo "Odd numbers:
${odd_array[@]}" Prepared by- Amitabh Srivastava
Practice Code - 3
Q- Write a shell script that take numbers from an array having even indexes and
print the multiplication table of those numbers?
Solution- numbers=(2 3 5 7 9 11)

for ((i=0; i<${#numbers[@]}; i+=2))


do
num=${numbers[$i]}
echo "Table of $num:"
for ((j=1; j<=10; j++))
do
echo "$num x $j = $((num * j))"
done
echo
done Prepared by- Amitabh Srivastava
Practice Code - 4
Q- Write a shell script to store array items in a new array from existing array only
once by removing the duplicate items?
found=1
break
Solution- original_array=(12 34 12 3 45 12 34)
fi
unique_array=() done
echo "Original array:
${original_array[@]}" if [[ $found -eq 0 ]]; then
for item in "${original_array[@]}"; do unique_array+=("$item")
found=0 fi
for unique in "${unique_array[@]}"; done
do
if [[ "$item" == "$unique" ]]; then echo "Unique items:
${unique_array[@]}"
Prepared by- Amitabh Srivastava
Associative Arrays: Initializing/Creating
▪ Declaring an array-
declare –A marks

▪ Declaring a read-only array-


declare –rA marks

▪ Creating array with values-


marks[“Alok”]=90
marks[“Geeta”]=87
marks[“Ram”]=75

▪ Initializing array with values-


declare -A marks=([“Alok"]=90, [“Geeta"]=87, [“Ram"]=75)
Prepared by- Amitabh Srivastava
Appending/Deleting/Displaying Array Values
▪ Example-
declare -A marks ▪ Output-
marks["Alok"]=90 Values: 87 75 90
marks["Geeta"]=87 Keys: Geeta Ram Alok
marks["Ram"]=75 Marks of Alok: 90
Length of array marks: 3
echo “Values: ${marks[@]}“
echo “Keys: ${!marks[@]}"
echo "Marks of Alok: ${marks["Alok"]}"
echo "Length of array marks: ${#marks[@]}"

▪ Appending new value- ▪ Deleting a key-


Marks["Gopal"]=100 unset marks[“Alok"]

Prepared by- Amitabh Srivastava


Iterating Associative Array
▪ Example-
declare -A marks ▪ Output-
marks["Alok"]=90 Geeta, 87
marks["Geeta"]=87 Ram, 75
marks["Ram"]=75 Alok, 90

for key in ${!marks[@]}


do
echo "$key, ${marks[$key]}"
done

Prepared by- Amitabh Srivastava


Check If Key Exists
▪ Example-
declare -A marks ▪ Output-
marks["Alok"]=90 Key exists!
marks["Geeta"]=87
marks["Ram"]=75

if [[ -n "${marks["Geeta"]}" ]]
then
echo "Key exists!"
else
echo "Key not found!"
fi

Prepared by- Amitabh Srivastava


Practice Code-1
Q. Write a shell script ask the user to input three names and phone numbers,
then store them in an array and show them all.
declare -A phonebook Output-
Enter name: Amitabh
for i in {1..3}; do Enter phone number: 12345
read -p "Enter name: " name Enter name: Deepak
read -p "Enter phone number: " phone Enter phone number: 23456
phonebook["$name"]="$phone" Enter name: Hari
done Enter phone number: 43215

echo -e "\nPhone Book:" Phone Book:


for name in "${!phonebook[@]}"; do Deepak - 23456
echo "$name - ${phonebook[$name]}" Amitabh - 12345
done Hari - 43215
Prepared by- Amitabh Srivastava
Practice Code-2
Q. Write a shell script ask the user to input a day abbreviation (e.g. Mon) and
then display the full name of day (Monday).

declare -A days
read -p "Enter a day abbreviation
(e.g., Mon): " input
# Define mapping
days["Mon"]="Monday"
if [[ -n "${days[$input]}" ]]
days["Tue"]="Tuesday"
then
days["Wed"]="Wednesday"
echo "Full name: ${days[$input]}"
days["Thu"]="Thursday"
else
days["Fri"]="Friday"
echo "Invalid day abbreviation."
days["Sat"]="Saturday"
fi
days["Sun"]="Sunday"

Prepared by- Amitabh Srivastava


Installing MySQL Server
▪ MySQL is an Open-Source database management system used to create/manage
database.
▪ To install mysql server run the following commands-
$ sudo apt update
It will update the packages by downloading the package’s information.

$ sudo apt install mysql-server


It will install mysql-server packages.
$ sudo service mysql start
It will start the mysql-server service.

$ sudo mysql –u root -p


It will ask the root password, which is ‘root’ by default. After giving the password,
the MySQL shell is started. Here, -u represents the username and -p is for entering
password. Prepared by- Amitabh Srivastava
MySQL: Creating User Account
▪ Log into mySQL as root user. [ sudo mysql –u root –p ]
(‘root’ is the default password of root user)

▪ To create a user account, run the following commands at mySQL prompt-


mysql> CREATE USER ‘amitabh'@'localhost' IDENTIFIED BY ‘amitabh’;
mysql> create database testdb;
mysql> GRANT ALL PRIVILEGES ON testdb.* TO 'amitabh'@'localhost’;
mysql> EXIT;

▪ To login with this new user account, run the following command at
Ubuntu terminal-
$ sudo mysql -u amitabh -p
Prepared by- Amitabh Srivastava
MySQL: Creating a Database
▪ After login to your mySQL account, first we need to confirm if the
database is successfully created, give the following command-

Prepared by- Amitabh Srivastava


MySQL: Creating a Table
▪ After creating a database ‘testdb’, next step would be to create a table
‘students’ under ‘testdb’ database. Give the following command to create a
table-
mysql> use testdb;
mysql> create table students(
-> roll varchar(5) not null,
-> name varchar(55) not null,
-> city varchar(30),
-> primary key(roll));

▪ To confirm if the table ‘students’ is successfully created, give the


following command-
mysql> describe students; Prepared by- Amitabh Srivastava
Connecting Database & Insert Record
# MySQL credentials
DB_USER="amitabh"
DB_PASS="amitabh"
DB_NAME="testdb"

# Prompt user for input


read -p "Enter student roll no.: " roll_no
read -p "Enter student name: " std_name
read -p "Enter student city: " std_city

# Insert into MySQL database


mysql -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" << EOF
INSERT INTO students(roll, name, city) VALUES('$roll_no', ‘$std_name', ‘$std_city');
EOF
echo "Record inserted successfully!"
Prepared by- Amitabh Srivastava
Remove ‘warning’ from output (Secure Method)
# Prompt user for input ~/.testdb.cnf-
read -p "Enter Roll Number: " roll [client]
read -p "Enter Name: " name user=amitabh
read -p "Enter City: " city password=amitabh

# Run the MySQL insert command


mysql --defaults-file=~/.testdb.cnf -D testdb -e \
"INSERT INTO students (roll, name, city) VALUES ('$roll', '$name', '$city');"

echo "Record inserted successfully."

Prepared by- Amitabh Srivastava


Edit Record through Script
# Prompt user for input read –p "Enter new name: " name
read -p "Enter Roll number: " roll_no read –p "Enter new city: " city
echo "roll number: $roll_no"
# Update the record
# Search record inside table mysql --defaults-file=~/.testdb.cnf -D
record=$(mysql --defaults-file=~/.testdb.cnf testdb -e \
-D testdb -e \ "UPDATE students SET name='$name',
"SELECT * FROM students WHERE city='$city' WHERE roll=$roll_no;"
roll=$roll_no;")
echo "Record updated successfully."
if [ -z "$record" ] fi
then
echo "No student found!!"
else
Prepared by- Amitabh Srivastava
Delete Record through Script
# Prompt user for input # Delete the record
read -p "Enter Roll number: " roll_no mysql --defaults-file=~/.testdb.cnf -D
echo "roll number: $roll_no" testdb -e \
“DELETE FROM students where
# Search record inside table roll=$roll_no;"
record=$(mysql --defaults-file=~/.testdb.cnf
-D testdb -e \ echo "Record deleted successfully."
"SELECT * FROM students WHERE fi
roll=$roll_no;")

if [ -z "$record" ]
then
echo "No student found!!"
else
Prepared by- Amitabh Srivastava
Shell Scripting: Functions
▪ Functions enables to break down the overall functionality of a script/code into
smaller, logical subsections, which can then be called upon to perform their
individual tasks when needed.
▪ Using functions to perform repetitive tasks is an excellent way to create code
reuse.
▪ Shell functions are similar to subroutines, procedures, and functions in other
programming languages.
▪ To declare a function, simply follow the below syntax-

function_name()
{
list_of_commands
}
Prepared by- Amitabh Srivastava
Function: Example
▪ Example-

# Function definition
Hello() {
echo "Hello World"
}

# Calling function
Hello

Output-
Hello World

Prepared by- Amitabh Srivastava


Passing Parameters
▪ Function parameters would be represented by $1, $2 and so on.

▪ Example-
greet() {
echo "Welcome $1 $2"
}

# Calling function with parameters


greet "Alok" "Kumar“

▪ Output-
Welcome Alok Kumar

Prepared by- Amitabh Srivastava


Returning a Value from a Function
▪ Use ‘return’ statement to return a value from the function.
▪ Use ‘$?’ to print the last returned value (use with return code 0 or 1).

Example-1 Example-2
# Function definition # Function definition
add() { add() {
echo -n "Sum of $1 and $2 is: " echo -n "Sum of $1 and $2 is: "
return `expr $1 + $2` sum=`expr $1 + $2`
} return $sum
}
# Calling function with parameters
add 10 20 # Calling function with parameters
echo $? add 20 30
echo $sum
Prepared by- Amitabh Srivastava
Using Return Codes 0 and 1
Example-

is_even_odd() { is_even_odd 43
if (( $1 % 2 == 0 ))
then if [[ $? -eq 0 ]]
return 0 then
else echo "It's even."
return 1 else
fi echo "It's odd."
} fi

Prepared by- Amitabh Srivastava


Practice Code-1
▪ Create a function that will ask the filename from user and checks whether it
exists or not?
Solution-
file_exists() {
if [[ -f "$1" ]]; then
echo "File '$1' exists."
else
echo "File '$1' does not exist."
fi
}

read -p "Enter filename: " fname


file_exists "$fname"

Prepared by- Amitabh Srivastava


Practice Code-2
▪ Create a function that will take all the .txt files from current directory and print
the line numbers of each file?
Solution-
process_file() {
echo "Processing: $1"

# Example: Count lines


wc -l "$1"
}

for file in *.txt


do
process_file "$file"
done
Prepared by- Amitabh Srivastava
Practice Code-3
▪ Create functions to perform Addition, Subtraction, Multiplication and Division. Use
Case-statement to call functions. Your program should be a menu driven program.
add() { echo "1. Addition"
echo -n "Enter two numbers: " echo "2. Subtraction"
read a b divide() { echo "3. Multiplication"
result=$((a + b)) echo -n "Enter two numbers: " echo "4. Division"
echo "Result: $a + $b = $result" } read a b echo "5. Exit"
if [ "$b" -eq 0 ]; then echo -n "Choose an option [1-5]: "
subtract() { echo "Error: Division by zero!" read choice
echo -n "Enter two numbers: " else case $choice in
read a b result=$(echo "scale=2; $a / $b" | bc) 1) add ;;
result=$((a - b)) echo "Result: $a / $b = $result" 2) subtract ;;
echo "Result: $a - $b = $result" } fi } 3) multiply ;;
while true 4) divide ;;
multiply() { do 5) echo "Goodbye!"; exit 0 ;;
echo -n "Enter two numbers: " echo "" *) echo "Invalid choice!" ;;
read a b echo "====== Calculator Menu ======" esac
result=$((a * b)) done
echo "Result: $a * $b = $result" } Prepared by- Amitabh Srivastava
Function Call from Prompt
▪ Put the definitions for commonly used functions inside .profile file. These
function definitions will be available whenever user log in and he can use
them at the command prompt.

▪ OR, put the function definitions in any .sh script and source the script. When
we source the file, no output will be displayed but we can now use the
functions defined under the .sh file from the command prompt.

▪ func2.sh-
sum() $ . func2.sh To remove a function from
{ $ sum 4 5 current shell-
echo "`expr $1 + $2`"
9
} $ unset –f sum
Prepared by- Amitabh Srivastava
Nested Functions
▪ A function can call another function.

Example-
# Calling one function from another
number_one () {
echo "This is number_one function!"
number_two
}

number_two () {
echo "This is number_two function!"
}
# Calling number_one
number_one
Prepared by- Amitabh Srivastava
Example: Recursive Function
▪ When a function call itself is known as Recursive function.

Example- count=0
number_one () {
count=$((count + 1))
if [ $count -le 5 ]
then
echo "Count: $count"
number_one
fi
}

# Calling number_one
number_one
Prepared by- Amitabh Srivastava
‘local’ Keyword
▪ The local command/keyword can only be used within a function.
▪ It makes the variable name have a visible scope restricted to that function and its
children only.
Example- name="Alok"

show_name(){
local name=$1
echo "Inside show_name(): name is set to $name"
}

echo "Before calling show_name name is set to $name"

show_name "Ram"

echo "After calling show_name name is set to $name"


Prepared by- Amitabh Srivastava
Practice Code-4
▪ Write a script where the outer function accepts two numbers and calls a
nested function to calculate the square of each number and return their sum.
Example- sum_of_squares() {
local a=$1
local b=$2
square() {
echo $(( $1 * $1 ))
}

local square1=$(square $a)


local square2=$(square $b)
echo "Sum of squares: $((square1 + square2))"
}

sum_of_squares 2 3 Prepared by- Amitabh Srivastava


Practice Code-5
▪ Write a script that takes a string from user using outer function and uses
a nested function to reverse it.

▪ Solution- string_input() {
read -p "Enter String: " input

reverse() {
echo "$1" | rev
}

echo "Reversed string: $(reverse $input)"


}

string_input
Prepared by- Amitabh Srivastava
fdisk command
▪ Short for ‘Format Disk’, a dialog driven utility used for creating and manipulating the
disk partition table.

▪ It allows to create maximum of four primary partitions and the number of


logical partitions depends on the size of the hard disk.

▪ The partition naming scheme follows the /dev/xxyN form:


➢ /dev is the directory where all device files lives.
➢ xx indicates the device type (which contains the partition).
➢ y indicates which device the partition is on.
➢ N is the partition number.

▪ Example- /dev/sdb, /dev/sdc1


Prepared by- Amitabh Srivastava
fdisk options
▪ fdisk -l
To view all partitions of specific hard-disk.

▪ fdisk –l /dev/sda
will display all disk partitions of device /dev/sda.

To view all commands which are available for fdisk, use the below command by
mentioning the hard disk name-

Prepared by- Amitabh Srivastava


fdisk options
▪ Once you press the ‘m’ key, the following command options will be displayed on your
screen-

Prepared by- Amitabh Srivastava


Logical Volume Manager (LVM)
▪ Logical Volume Manager is a system used in Linux to manage disk storage more
flexibly than traditional partitioning.

▪ it can sort raw storage (hard disks) into logical volumes, making it easy to configure
and use.

Why Use LVM?

▪ Traditional partitioning is rigid and hard to modify

▪ LVM allows dynamic resizing of volumes

▪ Allows one file system to span across multiple physical disks

▪ Useful for servers, VMs, and large-scale systems


Prepared by- Amitabh Srivastava
LVM Architecture- 3 Core Components
▪ Physical Volume (PV)-
Think of a PV as the raw hard disk or a disk partition that LVM will
manage. Example- /dev/sda1, /dev/sdb

▪ Volume Group (VG)-


A VG is a pool of storage created by combining one or more PVs. It is
like merging multiple water tanks into one big tank.

▪ Logical Volume (LV)-


These are slices of the VG that act like partitions. You can format LVs
and mount them like regular partitions. Think of LVs as drawers in
a big cabinet (VG).
Prepared by- Amitabh Srivastava
How to use LVM
▪ Install LVM tools-
$ sudo apt install lvm2

▪ Create Physical Volumes-


$ sudo pvcreate /dev/sdb /dev/sdc

▪ Create Volume Groups-


$ sudo vgcreate my_vg /dev/sdb /dev/sdc

▪ Create Logical Volumes-


$ sudo lvcreate –L 10G –n my_lv my_vg

▪ Format and Mount the Logical Volume-


$ sudo mkfs.ext4 /dev/my_vg/my_lv
$ sudo mount /dev/my_vg/my_lv /mnt Prepared by- Amitabh Srivastava
Resizing LVM
▪ Increase Size-
$ sudo lvextend –L +5G /dev/my_vg/my_lv
$ sudo resize2fs /dev/my_vg/my_lv

▪ Reduce Size- (more caution needed)


$ sudo umount /mnt
$ sudo e2fsck –f /dev/my_vg/my_lv
$ sudo resize2fs /dev/my_vg/my_lv 8G
$ sudo lvreduce -L 8G /dev/my_vg/my_lv

Prepared by- Amitabh Srivastava


tar command
▪ The Linux tar stands for Tape ARchive, which is used to create Archive and extract the Archive files.
▪ It provides archiving functionality in Linux. We can use it to create compressed or uncompressed
archive files.
Options and Examples-

$ tar -cvf dir1.tar dir1


It will create a compressed archive file called dir1.tar of the directory dir1.
Here, the option used are-
-c = create archive
-v = verbose mode, means to print all the file names on terminal those are added to archive file
-f = used to define the archive filename just after the –f option

$ tar -tvf dir1.tar


It will display all the contents(files) stored inside compressed archive file called dir1.tar.
Here, the option used is-
-t = to display all the files stored inside the archive file

$ tar -xvf dir1.tar


It will extract all the contents(files) stored inside compressed archive file called dir1.tar.
Here, the option used is-
-x = used to extract all files from compressed archive Prepared by- Amitabh Srivastava
gzip/gunzip command
▪ gzip command is used to compress files.
▪ Each single file is compressed into a single file.
▪ It is a very popular methods of compressing files, in order to save space, or to reduce the
amount of time needed to transmit the files across the network, or internet.
Options and Examples-
$ gzip about.txt
It will compress the original file about.txt as about.txt.gz.
$ gzip -k about.txt
It will compress the file about.txt as about.txt.gz and also keeps the original file.
$ gzip -9 about.txt
It will compress the file about.txt with highest compression level. Numbers 1 to 9 can be used from lowest to highest
compression levels.
$ gzip -L
It will display the gzip license information.
$ gunzip about.txt.gz
It will uncompress the compressed file about.txt.gz as about.txt.
Prepared by- Amitabh Srivastava
Mounting USB Drive
▪ The files and directories under Linux filesystem are arranged in a root directory called /.
▪ mount command is used to attach the filesystem of an external device to the filesystem of an Operating
System.
▪ It mounts (attaches) the external storage devices like hard disks, USB drives, etc. to the OS filesystem.
Steps-
➢ Open Powershell and type the following command-
notepad %UserProfile%\.wslconfig

➢ If the file exist edit it and if does not exist, create a new file as follows-
[automount] After performing all these steps all windows drives will
enabled = true mount correctly inside WSL Ubuntu OS.
options = "metadata"
mountFsTab = true To access your Windows drives under Ubuntu, give following
command-
➢ Restart WSL by giving following command- $ cd /mnt/
wsl –shutdown $ ls
And reopen Ubuntu. It will show the directories as ‘c’, ‘d’, ‘e’, etc. under Ubuntu
for Windows drives.
Prepared by- Amitabh Srivastava
Mount Specific Folder
▪ To mount a specific folder, we need to configure a file called /etc/fstab.

▪ /etc/fstab is a special file that tells Linux what to mount at startup.

▪ Open /etc/fstab file and add following line at the end of this file-
/mnt/e/my-folder /home/yourhomedirectory/new-folder none bind 0 0

▪ After adding the above line, save the file and run the following command-
$ sudo mount –a
If no error comes, your folder is mounted.

▪ Replace my-folder with your windows folder you want to mount.

▪ Replace new-folder with your directory name where you want it to appear inside Ubuntu.
Prepared by- Amitabh Srivastava
mount/umount command options
Examples-
mount –t type <device> <directory>
It will mount the filesystem of device at the specified directory. If the directory is not given, it will look for
the mount-point in /etc/fstab file. The /etc/fstab file contains information about which device should be
mounted where.

mount
It will display all currently mounted filesystems on a system.

mount –V or mount--version
It will display the version information of mount utility.

Umount <devicefile>
It will unmount (dettach) the filesystem of device.

Prepared by- Amitabh Srivastava


Cron: Automated Jobs
▪ A cron job is a task scheduled to run at regular intervals using the cron
daemon.

▪ We define cron jobs using a crontab (cron table) file, which lists jobs in a
specific format.

▪ Crontab Syntax-

Prepared by- Amitabh Srivastava


Creating/Editing Crontab File
▪ To open your user’s crontab-
$ crontab –e

▪ To view current cron jobs-


$ crontab –l

▪ Example-

➢ To run a script every day at 6 AM-


0 6 * * * /home/user/myscript.sh > /dev/pts/0

▪ By default, cron jobs have no terminal and do not show output unless
explicitly redirected to the terminal (/dev/pts/0).
Prepared by- Amitabh Srivastava
Cron Jobs Examples
▪ To run a command every 5 minutes-
*/5 * * * * echo “Hello” > /dev/pts/0

▪ To run a backup script every Sunday at 1 AM-


0 1 * * 0 /home/amitabh/backup.sh

Prepared by- Amitabh Srivastava


Firewall Basics
▪ Firewall is a virtual wall that is designed to protect our system from unwanted traffic
and unauthorized access to our system.
▪ iptables is a user-space utility program that allows a system administrator to
configure the IPv4 packet filtering rules of the Linux kernel firewall, which is part of
the netfilter framework.
▪ iptables is divided into 4 tables, each used for different types of packet processing-

Mostly used

Prepared by- Amitabh Srivastava


iptables- chains
▪ Each table contains chains (pipelines), which are actually the lists of rules applied to
packets.

▪ Each chain has a list of rules. A rule specifies-


✓ Matching conditions (e.g., source IP, port, protocol)
✓ Target action (e.g., ACCEPT, DROP, REJECT)

▪ Example rule-
iptables -A INPUT -p tcp --dport 22 -j ACCEPT Prepared by- Amitabh Srivastava
iptables- rules:target
▪ Following are the targets we can use while writing rules to define what happens if a
packet is matches a rule-

Prepared by- Amitabh Srivastava


iptables- examples
▪ To check existing rules-
$ sudo iptables -L -v –n
L: list, v: verbose, n: no DNS lookup

▪ To block all incoming traffic-


$ sudo iptables -P INPUT DROP
now, no incoming connections are allowed.
(Check it with ping command.)

▪ Allow only SSH (port 22)-


$ sudo iptables -P INPUT DROP
$ sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
$ sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

➢ The above rules will block everything except SSH and existing/related
connections. Prepared by- Amitabh Srivastava
iptables- more examples
▪ Block specific IP Address-
$ sudo iptables -A INPUT -s 192.168.1.100 -j DROP
This rule will block all traffic from specified IP.

▪ Allow Web Server (port 80)-


$ sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
This rule will allow incoming HTTP traffic.

▪ Allow Loopback Interface-


$ sudo iptables -A INPUT -i lo -j ACCEPT
This rule will allow many local apps to access loopback address.

▪ Flush All Rules-


$ sudo iptables -F
Prepared by- Amitabh Srivastava
iptables- Reset All Rules
➢ To fully reset the firewall to accept all traffic by default, do the following
steps-

1. Flush all rules-


$ sudo iptables -F

2. Set default policies to ACCEPT-


$ sudo iptables -P INPUT ACCEPT
$ sudo iptables -P FORWARD ACCEPT
$ sudo iptables -P OUTPUT ACCEPT

Prepared by- Amitabh Srivastava


ufw (Uncomplicated FireWall)
▪ UFW (Uncomplicated Firewall) is a command-line tool in Linux, primarily used in
Debian and Ubuntu systems, to manage and configure a firewall based on iptables.

▪ It simplifies firewall management by providing a more user-friendly interface than


iptables.

▪ UFW allows to control network traffic by allowing or denying connections based on


ports, services, or IP addresses.

▪ Installation- sudo apt update; sudo apt install ufw

▪ Status check- sudo ufw status

▪ Enabling ufw- sudo ufw enable Disabling ufw- sudo ufw disable
Prepared by- Amitabh Srivastava
ufw: Allowing/Denying Connections
▪ Allow a specific port-
$ sudo ufw allow <port>

▪ Allow a service-
$ sudo ufw allow <service_name>
example- $ sudo ufw allow ssh

▪ Allow connection from specific IP address-


$ sudo ufw allow from <ip_address>

▪ Deny a specific port-


$ sudo ufw deny <port>

▪ Deny a service-
$ sudo ufw deny <service_name>
Prepared by- Amitabh Srivastava
Secure Shell (SSH)
▪ SSH (Secure Shell) is used to securely access and manage remote systems over a
network. It encrypts the connection and allowing us to Log into the remote
system/server, execute commands remotely, transfer files securely, manage
automated tasks.

▪ To setup SSH, first we need to install


OpenSSH Server package-
$ sudo apt update
$ sudo apt install openssh-server

▪ To check that the SSH server is running-


$ sudo systemctl status ssh

▪ To start the SSH service- To start the SSH service at boot-


$ sudo systemctl start ssh $ sudo systemctl enable ssh Prepared by- Amitabh Srivastava
SSH Client- Connecting to the Server
▪ On the client machine (another Ubuntu system or same system using
Localhost), use the following command syntax to SSH into the server-
ssh username@server_ip

Example-
$ ssh [email protected]
OR
$ ssh student07@localhost
▪ It will ask for the user’s password. After entering the correct user’s
password, the user logs into the server remotely through client
machine.
Prepared by- Amitabh Srivastava
Docker
▪ Docker is a software program that creates containers.

▪ Containers are built to run fully developed applications that could be


shared and run on other computers.

▪ For example, if one computer contains the source code for a Word
Generator application, it could be distributed through other computers
by first converting the source code into an image and running it to make
a container to access the application.

▪ The image can be copy and load onto another system and run it.

Prepared by- Amitabh Srivastava


Installing Docker Desktop (Windows)
Installation Steps-
1. Go to the website https://docs.docker.com/docker-for-windows/install/ and
download the docker file.
2. Then, double-click on the Docker Desktop Installer.exe to run the installer.
3. Once you start the installation process, always enable Hyper-V Windows
Feature on the Configuration page.
4. Then, follow the installation process to allow the installer and wait till the
process is done.
5. After completion of the installation process, click Close and restart the
system.
6. Open the Ubuntu terminal and type the command- docker

Prepared by- Amitabh Srivastava


Installing Docker in Ubuntu
Installation Steps-
1. $ sudo apt update

2. $ sudo apt install docker.io -y

3. $ sudo systemctl enable docker --now

4. Open the Ubuntu terminal and check docker version-


$ docker --version

Prepared by- Amitabh Srivastava


Download Docker Image
To download an existing image-
Syntax- docker pull <image:tag>
$ docker pull ubuntu:latest

To check the downloaded images-


$ docker images

To run an existing image-


Syntax-
docker run -i --name=<name_of_container> -t <image_name> /bin/bash
$ docker run -i --name=my_ubuntu -t ubuntu /bin/bash
Prepared by- Amitabh Srivastava
Saving and loading Docker Image
To save an existing/created image-
Syntax- docker save –o <filename.tar> <image_name>
$ docker save –o my_ubuntu.tar ubuntu

To load an image from saved file-


Syntax- docker load –i <filename.tar>
$ docker load -i my_ubuntu.tar

To check a loaded image-


$ docker images
Prepared by- Amitabh Srivastava

You might also like