2. Linux Intro
2. Linux Intro
By Mr. Ashok
Windows OS
-> Developed by Microsoft Company
-> It is having GUI (Graphical user interface)
-> It is single user based Operating System (only one person can use this at a time)
-> It is commercial (paid)
-> Less Security
-> It is recommended for personal use
- Watch Movies
- Using Internet
- Playing Games
- Attending Online classes
History of Linux
-> In 1991, a student 'Linus Torvalds' developed this Linux OS
-> Linux Torvalds identified some challenges in UNIX OS and he suggested some changes
for Unix OS but UNIX OS Team rejected 'Linux Torvalds 'suggestions
-> Linus Torvalds used Minux OS to develop Linux
Linus + Minux
--> First Two letters from his name and last 3 letters from Minux OS
LI + NUX => LINUX
-> Linus Torvalds released LINUX OS with source code into market so that anybody can
modify LINUX OS that’s why it is called as Open-Source Operating System.
-> As Linux OS is open source, so many people and companies taken that Linux OS and
modified according to their requirements and released into market with different names
those are called as Linux Distributions.
Environment Setup
We can setup Linux machine in multiple ways
1) We can install Linux OS directly in computer
2) We can setup Linux OS in windows machine using Virtual Box
3) We can setup Linux Virtual Machine Cloud Platform like AWS and Azure
Note: In this note we will see how to setup Linux VM in AWS Cloud
Linux Commands
$ whoami : It will display currently logged in username
$ pwd : present working directory
$ date : To display current date
$ cal : To display calendar
$ rm -r dirname
-> To display file content we will use 'cat' command
Grep Command
-> 'grep' stands for global regular expression print.
-> 'grep' command will process the text line by line and prints any lines which matches
given pattern.
Ex: I want to print all lines which contains 'NullPointerException'
$ grep -i 'NullPointerException' *
Note: We can install grep using below command
$ sudo yum install grep
//search for the lines which contains given word in the given filename
$ grep 'word' filename
//search for the lines which are having exception keyword in server.log file
$ grep -i 'exception' server.log
• When vi starts up, it is in Command Mode. This mode is where vi interprets any
characters we type as commands and thus does not display them in the window
• This mode allows us to move through a file, and to delete, copy, or paste a piece of
text.
• To enter into Command Mode from any other mode, it requires pressing the [Esc]
key. If we press [Esc] when we are already in Command Mode, then vi will beep or
flash the screen.
• vi filename: Creates a new file if it already not exist, otherwise opens existing file.
• vi -R filename : Opens an existing file in read only mode.
• view filename : Opens an existing file in read only mode.
$ vi f1.txt
=> After making changes if we want to save those changes then execute :wq
=> After making changes if we don't want to save those changes then execute: q!
SED command:
• SED command in LNIX stands for stream editor and it can perform lots of functions
on file like searching, find and replace, insertion or deletion.
• Though most common use of SED command in LNIX is for substitution or for find
and replace.
• By using SED you can edit files even without opening them, which is much quicker
way to find and replace something in file, than first opening that file in VI Editor
and then changing it.
• SED is a powerful text stream editor. Can do insertion, deletion, search and replace
(substitution).
• SED command in Linux supports regular expression which allows it perform
complex pattern matching.
Note: If any permission is not available then it will represented with hypen ( - )
Note: In every Linux machine by default one 'root' account will be available
(super account)
-> When we launch EC2 instance with Amazon Linux OS, we got by default 'ec2-user' user account
# Delete user
$ sudo userdel <username>
# Delete Group
$ sudo groupdel <group-name>
Chown command:
-> The chown command changes user ownership of a file & directory in Linux
-> Every file is associated with an owning user or group
-> We can check the ownership of a file using 'ls -l'
# Changing owner of a file
$ chown NewUser FILE
Note: we can change owner using userid also
$ chown 1001 filename
# changing group of a file
$ chown :NewGroup FILE
# We can change group using group id also
$ chown :1003 sample
# Change Owner and the Group
$ chown NewUser:NewGroup FILE
locate command:
The locate Command find will search for data in local db
$ sudo yum install mlocate
$ locate apache
$ locate -c apache
$ locate -c *.txt
Note: when we create new files it will take some time to update those files in mlocate db
find command:
-> find command will search for the files in entire linux file system.
-> find command providing advanced searching technique
-> Using find command, we can search for the files based on name and type also.
-> Find Files Under Home Directory
$ find /home -name f1.txt
-> Find Files With 777 Permissions
$ find . -type f -perm 0777 -print
-> Find all Empty Files inside home directory
$ find /home -type f -empty
-> Find all Empty Directories inside home directory
$ find /home -type d -empty
# Display 30 days old files in linux
$ sudo find . -mtime 30 -print
# Delete 30 days old files in linux inside home directory
$ sudo find /home -mtime 30 -delete
# Delete 1 hour old files in linux
$ sudo find /home -mmin +60 -delete
-> 'man' command is like a help command. It is used to understand command syntax and
options.
$ man cat
-> To see ip address we will use 'ifconfig' command
$ ifconfig
-> 'ping' command is used to check connectivity
$ ping <ip>
-> 'curl' command is used to get response from the server
$ curl <url>
-> 'wget' command is used to download resources from internet
$ wget <url>
They handle dependency resolution, ensuring that all required libraries and components are
installed correctly. Here are some commonly used package managers in Linux:
• YUM (Yellowdog Updater, Modified) is the default package manager for Red Hat-
based distributions such as CentOS and Fedora.
• Commands: yum
DNF:
What is shell?
-> Shell is responsible for reading commands given by user
-> Shell will verify command and will give instructions to kernel to process that command
-> If command is invalid shell will give error
-> Kernel will execute our command with System Hard Components
-> Shell acts as mediator between User and Kernel
What is Scripting?
Types of Shells
Shell Script - 2
#! /bin/bash
echo "Welcome to Scripting"
echo "Scripting is used to automate regular work"
echo "Scripting requires lot of practise"
Shell Script - 3
#! /bin/bash
echo "Enter your name:"
read name
echo "Good Morning $name"
Shell Script - 4
#! /bin/bash
a=10
b=20
c=$(($a + $b))
#! /bin/bash
echo "Enter First Number"
read a
echo "Enter Second Number"
read b
c=$(($a + $b))
echo "Sum of $a and $b is = $c"
END
Variables
-> Variables are place-holders to store the value
-> Variables are key-value pairs
-> In Shell Scripting there is no concept of Data Type.
-> Every value will be treated as text/string
Ex:
name=ashok
age=30
[email protected]
phno=1234
-> Variables are divided into 2 types
1) Environment Variables or System variables
2) User Defined Variables
-> The variables which are already defined and using by our system are called as
Environment/System variables
Ex:
$ echo $USER
$ echo $SHELL
Variable Rules
-> We should not use special symbols like -, @, # etc....
-> Variable name should not start with digit
Note: It is recommended to use uppercase characters for variable name
-> we can use 'readonly' for variable so that variable value modification will not be
allowed
Conditional Statements
-> Conditional statements are used to execute commands based on condition
Syntax:
if [ conition ]
then
stmts
else
stmts
fi
-> If given condition satisfied then if statements will be executed otherwise else statements will be
executed
if [ condition ]
then
stmts
elif [ condition ]
#!/bin/bash
echo "Enter Your Favorite Color"
read COLOR
if [ $COLOR == 'red' ]
then
echo "Your are cheerful"
elif [ $COLOR == 'blue' ]
then
echo "You are joyful"
else
echo "You are lucky"
fi
#! /bin/bash
for ((i=1; i<=10; i++))
do
echo "$i"
done
While loop Example
#! /bin/bash
i=10
while [ $i -ge 0 ]
do
echo "$i"
let i--;
done
Infinite Loop
#! /bin/bash
while true
do
echo "This is my loop stmt"
done
Note: To stop infinite loop we will use 'ctrl + c'
Functions
-> The big task can be divided into smaller tasks using functions
-> Function is used to perform an action / task
-> Using functions we can divide our tasks logically
-> Functions are re-usable
5) Write a shell script to read user to address, mail subject and mail body then send email
with given details.
What is CRON?
Cron is the most useful utility in a Linux or UNIX-like operating system that allows running
commands or scripts on a given schedule without any user intervention.
It is mostly used for automating recurring jobs like running scheduled backups, cleaning
temporary files, system maintenance, and various other recurring jobs. It is similar to the
Task Scheduler in Windows OS.
What is Crond?
Crond is the daemon in the Linux system that runs in the background and checks every
minute to see if there is any job scheduled at that time. If there is, it performs that job, else
it remains inactive.