Linux Commands and Shell Scripting
Linux Commands and Shell Scripting
Linux Fundamentals
Comprehensive Guide to System Administration
Exploring operating system concepts, Linux architecture, commands, editors, permissions, and
shell environments
Introduction to Operating Systems
Understanding the foundation of computer systems management
Definition
System software that manages computer hardware, software resources, and provides common services for
applications.
Functions
An operating system serves as an intermediary between users and computer hardware, managing processes,
memory, file systems, and device drivers. It provides a platform for applications to run, ensures system security,
handles multitasking, and facilitates user interaction through interfaces. Key functions include resource
allocation, process scheduling, memory management, and providing APIs for software development.
Types of Operating Systems
Classification and Characteristics
Specialized OS designed for smartphones and tablets, featuring touch interfaces, app ecosystems,
power management, and connectivity features for portable devices.
Network OS manages distributed computing resources across networks, while embedded OS runs on
specialized hardware with limited resources in IoT devices and appliances.
Linux vs Windows
Comparing two dominant operating system paradigms
Linux Windows
Monolithic kernel providing higher performance and direct Windows uses hybrid kernel balancing performance with
hardware access with efficient resource management. modularity, supporting various hardware configurations.
User Access
Security Model
Permission-based security with root access control, Administrator privileges with UAC (User Account
making it inherently more secure against malware. Control) providing security prompts for system changes.
Supports multiple file systems (ext4, Btrfs, XFS) with NTFS file system with case-insensitive naming,
case-sensitive naming and robust permissions. advanced features like compression and encryption.
Linux vs Windows Comparison Matrix
Key differences across various system aspects
Cost Customization
Linux offers extensive customization from kernel to
Linux is free and open-source with no licensing fees, while
desktop environments, while Windows provides limited
Windows requires paid licenses for commercial use. Linux
customization options. Users can modify Linux source
reduces total cost of ownership significantly for enterprises and
individual users seeking budget-friendly solutions. code and choose from hundreds of distributions tailored
to specific needs.
Software Performance
Windows has broader commercial software support
Linux typically uses fewer system resources and offers
and gaming compatibility, while Linux excels in
better performance on older hardware, while Windows
development tools and server applications. Linux
provides optimized performance for consumer applications
alternatives exist for most Windows software with
and gaming with extensive hardware driver support and
growing application ecosystem.
automatic optimization features.
=============================
=================
Linux Commands
=================
$ rm -r <dirname>
cd : to change directory
cat : create files with data , append data to existing file, display file
content
Note: cat command will display file data from top to bottom.
$ cp f1.txt f2.txt
Note: To copy data from multiple files we need to use 'cat' command.
rm : to delete file
$ rm filename
$ mv old-name new-name
wc : word count
=======================
Text Editor in Linux
=======================
vi : visual editor
$ vi ashokit.txt
=============
SED command
=============
=> We can replace one word with another word using SED command without opening
the file
Note: By default, SED command will not make changes in the original file. It
will just print output on terminal.
=================
File Permissions
=================
r : read
w : write
x : execute
-rwxrwxrwx f1.txt
-rw-rw-r-- f2.txt
-r--rw-rw- f3.txt
dr-xr--rwx linux
0 : No Permission
1 : Execute
2 : Write
3 : Execute + Write
4 : Read
5 : Read + Execute
6 : Read + Write
-> In Every Linux machine by default 'root' user account will be available
-> When we launch EC2 instance ( Amazon Linux AMI ) We got 'ec2-user' account
also.
-> For every user account one home directory will be available under /home
directory
ec2-user : /home/ec2-user
john : /home/john
$ id <username>
$ sudo su - (switch to root user and point to root user home directory)
=======================================
Working with Users and Groups in Linux
=======================================
$ sudo su -
$ useradd <uname>
$ passwd <uname>
$ cat /etc/passwd
$ su <username>
# Delete user
$ userdel <uname>
$ cat /etc/group
$ groupadd <group-name>
$ groupdel <group-name>
# change username
$ passwd <uname>
==================
chown command
=================
# change owner-group
============================
locate and find commands
============================
=> 'locate' and 'find' commands are used for file search
=> In every linux machine locate db will be available which stores linux file
system details.
=> When we execute locate command it will fetch details from locate db.
# search for the files under home directory with given name
$ sudo find /home -name f1.txt
=================================
Working with Zip files in Linux
=================================
# Unzip
$ unzip <zip-file-name>
$ ping 13.12.109.21
$ wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.10/bin/apache-tomcat-
10.1.10-windows-x64.zip
$ curl 192.168.1.123
$ ifconfig
==========================
Package Managers in Linux
==========================
Ex: Ubuntu
dnf : DANDIFIED YUM (It is replacing yum pkg manager in latest versions of
fedora and Cent OS)
==================================
How to install softwares in linux
==================================
============================================
How to host static website in Amazon Linux
============================================
2) Dynamic Website
=> Static websites will give same response for all users
Note: After starting httpd service, we can access our webserver using EC2-VM
public IP address.
Note: Enable HTTP Protocol in Security Group Inbound Rules to allow incoming
traffic.
=> After enabling HTTP port then access EC2-VM public IP in browser
URL : http://65.1.132.215/
http://65.1.132.215:80/
$ cd /var/www/html
$ sudo vi index.html
Note: Write website content in index.html then save and close that file then
access
Ec2-vm public ip.
=======================
What is Grep Command
=======================
=> The grep command is a powerful utility in Linux used for searching and
filtering text
=> The grep command searches for patterns or regular expressions in files or
input streams and displays lines that match the specified pattern.
# Ignore case
$ grep -i "pattern" file.txt
# Invert match (print files which are not matching given pattern)
$ grep -v "pattern" file.txt
Shell Programming Constructs
Building blocks of effective shell scripting
Overview
Shell programming provides powerful constructs for automating system tasks and creating
interactive scripts.
Core Elements
Shell programming includes variables for data storage, control structures for decision making
and iteration, functions for code reusability, operators for computations, and input/output
mechanisms for user interaction. These elements work together to create powerful automation
scripts that can handle file operations, text processing, system monitoring, and complex
typed and declared without explicit type command outputs. Environment variables are
specification. Assignment uses the equals sign accessible system-wide using export command.
without spaces: variable_name=value. String Local variables exist only within the current
values should be enclosed in quotes for safety. shell session. Special variables like $0, $1, $2
Variables are referenced using the dollar sign represent script name and arguments. Built-in
prefix: $variable_name or ${variable_name} for variables include $HOME, $PATH, $USER for
Simple assignment (=), compound assignments (+=, AND (&&), OR (||), NOT (!) for combining multiple
-=, *=, /=) for variable manipulation. conditions in complex expressions.
Arithmetic operations in shell require specific syntax for Comparison operators enable conditional logic in scripts.
proper evaluation. Using double parentheses: result=$((5 + Numerical comparison: if [ $age -ge 18 ]; then echo
3)) performs addition. The expr command: result=`expr 5 + \"Adult\"; fi checks age threshold. String comparison: if [
3` provides alternative arithmetic evaluation. For floating- \"$name\" = \"admin\" ]; then echo \"Administrator\"; fi
point operations, bc calculator is used: result=`echo \"5.5 validates user identity. File existence: if [ -f
+ 3.2\" | bc`. Variable arithmetic: counter=$((counter + 1)) \"/etc/passwd\" ]; then echo \"File exists\"; fi checks
increments values. Modulus operation: system files. Multiple conditions: if [ $score -gt 80 ] && [
remainder=$((number % 2)) checks for even/odd numbers. $attendance -ge 75 ]; then echo \"Pass\"; fi combines
Purpose
Decision making statements allow programs to execute different code paths based on specific conditions and
user input.
Implementation
Shell provides multiple decision-making constructs including if-then-else statements for binary choices, nested
if statements for complex conditions, case statements for multiple options, and logical operators for combining
conditions. These constructs enable scripts to respond dynamically to different scenarios, validate user input,
check system states, and implement sophisticated program logic that adapts to varying runtime conditions and
environmental factors.
Decision Making Types
Available conditional constructs
Implementation Details
Overview
Loop statements enable repetitive execution of code blocks until specific conditions are met or collections are processed.
Applications
Loops are essential for processing arrays, iterating through files, automating repetitive tasks, and handling user input validation. Shell
provides for loops for iterating over lists and ranges, while loops for condition-based repetition, and until loops for negative condition
checking. These constructs enable efficient batch processing, system monitoring scripts, and automated deployment procedures that
Implementation Details
While loops continue execution based on true conditions. Until loops execute until condition becomes true. Example:
Example: counter=1; while [ $counter -le 5 ]; do echo attempts=0; until ping -c 1 google.com &> /dev/null; do
"Count: $counter"; counter=$((counter + 1)); done. This echo "Waiting for connection..."; attempts=$((attempts +
pattern is perfect for reading files line by line: while IFS= 1)); if [ $attempts -gt 10 ]; then break; fi; sleep 5; done.
read -r line; do echo "Processing: $line"; done < file.txt. This approach is ideal for waiting for services to start,
While loops are essential for monitoring system states, polling for file creation, and implementing retry
processing streaming data, and implementing interactive mechanisms with built-in timeout controls.
menus.
Nested Loops Example
Implementation Details
operations.
Advanced Loop Examples
Complex iteration patterns and optimizations
Loops excel at batch file operations. Example: for file in Loop control statements provide flow management. Break
*.txt; do if [ -f \"$file\" ]; then echo \"Processing $file\"; example: for num in {1..10}; do if [ $num -eq 5 ]; then
mv \"$file\" \"processed_$file\"; fi; done. This pattern break; fi; echo $num; done outputs 1-4. Continue
processes all text files in a directory, checking existence example: for num in {1..10}; do if [ $((num % 2)) -eq 0 ];
before operations. Advanced pattern: find /path -name then continue; fi; echo $num; done prints odd numbers
\"*.log\" -print0 | while IFS= read -r -d '' file; do echo only. These controls enable sophisticated filtering and
\"Analyzing $file\"; grep ERROR \"$file\" >> early termination in complex processing scenarios.
error_summary.txt; done for handling files with spaces.
Arrays
Structured data storage and manipulation
Arrays store multiple values in indexed collections. Declaration Elements are accessed using array[index] syntax with ${array[0]}
syntax: array_name=(value1 value2 value3) creates an indexed notation. All elements: ${array[@]} or ${array[*]} returns complete
array. Individual assignment: array[0]="first" sets specific array. Array length: ${#array[@]} counts elements. Index ranges:
elements. Arrays support mixed data types and dynamic sizing. ${array[@]:start:length} extracts subarrays. Iteration: for element
Associative arrays use declare -A for key-value pairs with string in "${array[@]}" processes each element safely with proper
Arrays can be created using parentheses notation: fruits=(apple orange banana), through individual assignment:
colors[0]="red", or by reading from files: readarray -t lines < file.txt. Command substitution arrays: files=($(ls *.txt)) capture
command output as array elements.
Implementation Details
shell scripts.
Advanced Array Examples
Complex array operations and real-world applications
Implementation Details
"files=(*.txt); for i in \"${!files[@]}\"; do echo \"File expands to all text files in the current directory, creating
processing.
Functions
Reusable code blocks and modular programming
Functions can return values through echo or return statements. Example: calculate_sum() { local result=$(($1 + $2)); echo $result; }. This pattern enables
mathematical operations, data transformations, and result passing between different script components for complex processing workflows.
Basic Function Example
Implementation Details
— Simple Parameterized Function reusable code blocks that can be customized through
maintainability.
Advanced Function Examples
Complex function implementations and design patterns
Functions can call themselves for recursive algorithms. Creating reusable function libraries improves code
Example: factorial() { if [ $1 -le 1 ]; then echo 1; else local organization. Example library file utils.sh: log_message() {
temp=$(factorial $(($1 - 1))); echo $(($1 * temp)); fi; }. echo \"[$(date)] $1\" >> logfile.txt; }; validate_email() { [[
Fibonacci sequence: fib() { if [ $1 -le 1 ]; then echo $1; else $1 =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$ ]];
echo $(($(fib $(($1-1))) + $(fib $(($1-2))))); fi; }. Recursive }. Source in scripts: source utils.sh; log_message \"Script
functions solve mathematical problems, tree traversal, and started\". Libraries enable consistent functionality across
nested data structure processing efficiently. multiple scripts, promoting code reuse and standardization
in development workflows.
Function with Return Value
Implementation Details
"add_numbers() { local sum=$(($1 + $2)); echo $sum; function performs addition using local variables for
}; result=$(add_numbers 15 25); echo \"Sum: calculation and returns the result via echo. The calling
Overview
Effective function development requires attention to error handling, documentation, and testing for robust implementations.
Implementation
Functions should validate input parameters, handle edge cases gracefully, and provide clear error messages. Include comprehensive
documentation with purpose, parameters, return values, and usage examples. Implement test cases to verify functionality under
normal and exceptional conditions. Use meaningful function names, local variables for scope control, and consistent return codes for
error handling. These practices ensure maintainable, reliable, and reusable code that follows professional development standards.
===================
Linux Architecture
===================
1) Applications
2) Shell
3) Kernal
4) Hardware
=> When we execute any command in linux then Shell will process our command execution.
=> Shell will convert our command into 'kernel' understandable format
============================
What is Shell Scripting ?
============================
=> Shell will act as mediator between Users and Kernel. Shell will convert commands into kernal
understandable format.
##### The process of executing set of commands available in the file using SHELL is called as
Shell Scripting #####
====================
Script-1 : work.sh
====================
#! /bin/bash
whoami
pwd
date
=====================
Script-2 : msg.sh
=====================
#! /bin/bash
========================
Script-3 : NameDemo.sh
========================
#! /bin/bash
===================
Script-4 : Sum.sh
===================
#! /bin/bash
a=10
b=20
c=$(($a+$b))
============================
Script-5 : DynamicValSum.sh
============================
#! /bin/bash
c=$(($a+$b))
============
Variables
============
id = 101
gender = male
=======================
Command Line Arguments
=======================
=> The arguments which we we will pass to script file at the time of execution.
$# - No.of args
#! /bin/bash
result=$(($1+$2))
Run : sh DynamicValSum.sh 10 20
==================================================================
==================================================================
3) Functions
===========================
Conditional Statements
==========================
=> Conditional Statements are used to execute 'commands' one time based on condition
Syntax:
if [ condition ]
then
statements
else
statements
=======================
Script-7 : if-else.sh
=======================
#! /bin/bash
if [ $name == 'john' ]
then
echo "Hi"
else
echo "Bye"
fi
===============================
Script - 8 : if-elif-else.sh
===============================
#! /bin/bash
if [ $name == 'john' ]
then
echo "Hi $name"
else
echo "Bye $name"
fi
====================
Loops
====================
echo "hi"
echo "hi"
echo "hi"
echo "hi"
echo "hi"
=> If we want to print "hi" for 1000 times, can we write echo for 1000 times ? Not recommended
=> To execute same command multiple times then we will use "Loops"
=> Print "hi" message 10 times (Here we know the range to print msg - we can use 'for' loop)
=> Print "good night" message till 10 PM (Here we don't range but we know condition - we can use
while loop)
=========================
Script-10 : for-loop1.sh
=========================
#! /bin/bash
=========================
Script-11 : for-loop2.sh
=========================
#! /bin/bash
============================
Script-12 : while-loop.sh
============================
#! /bin/bash
i=10
while [ $i -ge 1 ]
do
echo "$i"
let i--;
done
=====================
Functions / Methods
====================
=> The big task can be divided into smaller tasks using functions
Syntax:
-------
# writing function
function functionName ( ) {
// function body
}
# calling function
functionName
=======================
Script - 13 : fun1.sh
=======================
#! /bin/bash
function welcome ( ) {
# calling function
welcome