Linux Material
Linux Material
Linux Material
By Mr. Ashok
-> Operating System is acting as mediator between users and computer hardware components
Windows OS
-> Developed by Microsoft Company
-> It is single user based Operating System (only one person can use this at a time)
- Watch Movies
- Using Internet
- Playing Games
- Store data
1
Linux OS
-> Linux is Community Based OS
Note: In real-time, we will use Linux OS only to setup infrastructure required to run our
application
-> Linux OS is not only for Administrators, even developers and testers also will use Linux OS in
real-time to monitor our application and application servers and application logs.
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
Linus + Minux -
-> First Two letters from his name and last 3 letters from Minux OS
-> Linus Torvalds released LINUX OS with source code into market so that anybody can modify
LINUX OS thats 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.
2
Environment Setup
We can setup Linux machine in multiple ways
Note: In this note we will see how to setup Linux VM in AWS Cloud
3
4
5
6
7
8
9
Create Linux VM using AWS EC2 service
10
11
12
13
Download Mobaxterm software to connect with Linux VM
14
➔ Once we connect with EC2 instance using Mobaxterm we can see screen like below
Linux Commands
$ whoami : It will display currently logged in username
15
Linux File System
-> In Linux everything will be represented as a file
-> Directory file is equal to folder (it can contain files and folders)
$ touch f1.txt
$ touch f2.txt
$ ls
//write data
//write data
$ mkdir dirname
$ rm filename
$ rmdir dirname
16
-> 'ls' is used to list out all files & directories available in the given directory
-> ls -t : It will display all files based on last modified date and time. Most recent file will be display
at top and old files will display at bottom.
-> ls -rt : It will display all files based on reverse of last modified date and time. Old files will
display at top and recent files will display bottom.
-> ls -a : It will display all files including hidden files (hidden files will start with .)
-> ls -lR : It will display all files and directories along with sub directoris content
Note: We can use several options for ls command at a time. When we are using multiple options
order of the options is not important
$ ls -ltr
$ ls -tlr
$ ls -l -t -r
$ ls -trl
$ ls -l <dirname>
$ rm <filename>
$ rmdir <dirname>
-> To delete non-empty directory we will use 'rm' command like below
$ rm -r dirname
17
$ cat filename
-> To display file content with line numbers we will use '-n' option
$ cat -n filename
-> To display multiple files content at a time execute command like below
-> Copy one file data into another file using 'cat' command
-> Copy more than one file data into another file
$ tac filename
-> 'rev' command is used to reverse each line content of the file
$ rev filename
-> head command is used to display file data from top (default 10 lines)
$ head filename
-> tail command is used to display file data from bottom (default 10 lines)
$ tail +25 filename (it will display data from 25th line to bottom)
-> It is used to count no.of lines, no.of words and no.of characters in the file
$ wc f1.txt
-> We can't copy morethan one file data using 'cp' command. To copy multiple files data we
should go for 'cat' command
18
$ cat f1.txt f2.txt > f3.txt
$ mv f1.txt f1111.txt
$ mv dirname dirnewname
Note: We can use 'mv' command for renaming and moving files
-> cmp command will display only first difference in given 2 two files
-> diff command will display all the differences in the content
-> 'grep' command will process the text line by line and prints any lines which matches given
pattern.
$ grep -i 'NullPointerException' *
//search for the lines which contains given word in the given filename
//search for the lines which are having exception keyword in server.log file
//search for the given text in present directory and in sub-directories also
$ grep -R 'exception'
-c : This prints only the count of files that matches give pattern
-i : ignore case-sensitivity
19
Working with Text Editors in Linux
-> The default editor that comes with the LNIX operating system is called vi (visual editor).
-> Using vi editor, we can edit an existing file or we create a new file from scratch
-> We can also use this vi editor to just read a text file.
1) command mode
2) insert mode
3) escape mode
Command Mode:
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.
Insert mode:
Everything that’s typed in this mode is interpreted as input and finally, it is put in the file.
The vi always starts in command mode. To enter text, you must be in insert mode.
To get out of insert mode, press the Esc key, which will put you back into command mode.
The cursor will jump to the last line of the screen and vi will wait for a command.
This mode enables you to perform tasks such as saving files, executing commands.
vi filename: Creates a new file if it already not exist, otherwise opens existing file.
20
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!
To move around within a file without affecting text must be in command mode (press Esc twice).
Here are some of the commands can be used to move around one character at a time.
colon along with x : Colon followed by a number would position the cursor on line number
represented by x.
Usecase:
21
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 lnix supports regular expression which allows it perform complex pattern
matching.
Example:
Sed command is mostly used to replace the text in a file. The below simple sed command replaces
the word “unix” with “linux” in the file
By default, the sed command replaces the first occurrence of the pattern in each line and it won’t
replace the second, third…occurrence in the line.
Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below
command replaces the second occurrence of the word “unix” with “linux” in a line.
The substitute flag /g (global replacement) specifies the sed command to replace all the
occurrences of the string in the line.
SED command can also be used for deleting lines from a particular file. SED command is used for
performing deletion operation without even opening the file
22
To Delete a particular line say n in this example
=> To make SED command changes to file permanently we will use '-i' option.
Note: With above command 'unix' keyword will be replaced with 'linux' keyword in the file
permanently.
User: the owner of the file (person who created the file).
Therefore, all users in that group will have the same permissions. It makes things easier than
assign permission for every user you want.
Other: any person has access to that file, that person has neither created the file, nor are they in
any group which has access to that file.
‘r’ = read.
‘w’ = write.
‘x’ = execute.
‘-’ = no permission.
-rw-r--r--
-: It represents file
rw: User
r: Group
r: Other
23
As we see above, the empty first part means that it is a file. If it were a directory then it will be the
letter “d” instead.
The second part means that the user “Home” has read and write permissions but he does not have
the execute one.
This command will add the write permission for other users to my text file “section.txt”.
“o” refers to others, “g” for the group, “u” for the user, and “a” for all.
If you want to remove the permission, you can use the same method but with “-” instead of “+”.
For example, let’s remove the execute permission from the user by:
Also, you can use Symbolic Mode to modify permissions like the following:
Number Permission
0 No permission
1 Execute
2 Write
4 Read
Let’s remove the execute from the group and the write from other by:
24
$ chmod 765 section.txt
-> Within one Linux machine we can create multiple user accounts
-> Multiple users can access single linux machine and can perform multi-tasking
-> When we launch EC2 instance with Amazon Linux OS, we got by default 'ec2-user' user account
Create a user:
-> After creating user account we can verify user account details using 'id' command
$ id <uname>
$ ls -l
(We can see ec2-user folder and newly created user folder)
-> After creating a new user and setting a password to it, you can log in from the terminal
$ su - <uname>
Delete a user:
If you try that command, you will notice that the user directory has not been deleted and you
need to delete it by yourself.
25
User groups:
-> A group is a collection of users.
-> The primary purpose of the groups is to define a set of privileges like read, write, or execute
permission for a given resource that can be shared among the users within the group.
Create a group:
You can see all of the groups you have by opening the following file:
$ cat /etc/group
$ id username
Delete a group:
locate command:
$ 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.
-> Using find command, we can search for the files based on name and type also.
26
2. Find Files Under Home Directory
Note: As find command is scanning entire file system, it will take more time to give search results.
Chown command:
-> The chown command changes user ownership of a file & directory in Linux
-> 'man' command is like a help command. It is used to understand command syntax and options.
$ man cat
$ ifconfig
27
$ ping <ip>
$ curl <url>
$ wget <url>
Note: Amazon Linux / CentOS / Red Hat : yum is the package manager
$ git --version
# install maven
$ mvn –version
28
Installing Web Server in Linux VM (Amazon Linux):
1) Connect to EC2 instance
Note: Check accessing apache server from browser using EC2 VM public IP
-> If server is not accessible then, enable HTTP port in Security Group of our EC-2 VM
-> After adding security group try accessing EC2 instance using IP
Ex: http://52.66.101.3/
$ cd /var/www/html
$ sudo vi index.html
-> Save the content and close the file ( press Esc -> :wq)
Shell Scripting
What is shell?
-> Shell will verify command and will give instructions to kernel to process that command
-> Kernel will execute our command with System Hard Components
What is Scripting?
-> For example i want to execute below commands every day as a linux user
29
$ date
$ cal
$ whoami
$ pwd
$ ls -l
-> Instead of executing all these commands manually we can keep them in a file and we can
execute that file.
-> The file which contains set of commands for execution is called as 'Script file'
-> When we execute script file then shell will read those commands and will verify commands
syntax
-> Kernel will give instructions to hardware components to perform actual operation
Types of Shells
1) Bourne Shell
2) Bash Shell
3) Korn Shell
4) CShell
5) TShell
6) ZShell
$ cat /etc/shells
$ echo $SHELL
30
Working with First Shell Script Program
# Create a script file
$ vi task.sh
whoami
pwd
date
$ sh task.sh
Note: If we get permission denied then we should provide 'execute' permission using 'chmod'
command
$ ./task
Syntax :
#! /bin/bash
#! /bin/bash
#! /bin/bash
read name
31
********************* Shell Script - 4 ******************************
#! /bin/bash
a=10
b=20
c=$(($a + $b))
#! /bin/bash
read a
read b
c=$(($a + $b))
END
Variables
-> Variables are place-holders to store the value
Ex:
name=ashok
age=30
email=ashokitschool@gmail.com
phno=1234
-> The variables which are already defined and using by our system are called as
Environment/System variables
32
Ex:
$ echo $USER
$ echo $SHELL
-> Based on our requirement we can define our own variables those are called as user defined
variables
Ex:
name=ashok
age=30
Variable Rules
-> We should not use special symbols like -, @, # etc....
-> we can use 'readonly' for variable so that variable value modification will not be allowed
-> Cmd args are used to supply the values dynamically to the script file
Ex:
$ sh demo.sh ashokit 30
$# - no.of args
echo 'hi'
#echo 'hello'
33
-> We can comment multiple lines also in script file like below
<<COMMENT
.....................
COMMENT
-> We can hold script execution for some time using 'sleep' command
#! /bin/bash
echo $#
echo $0
echo $1
sleep 30s
echo $2
#echo $*
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 statments will be executed otherwise else statements will be
executed
if [ condition ]
then
stmts
elif [ condition ]
then
stmts
34
else
stmts
fi
Ex:
#!/bin/bash
read COLOR
if [ $COLOR == 'red' ]
then
then
else
fi
35
For Loop Example
#! /bin/bash
do
echo "$i"
done
#! /bin/bash
i=10
while [ $i -ge 0 ]
do
echo "$i"
let i--;
done
Infinite Loop
#! /bin/bash
while true
do
done
36
Functions
-> The big task can be divided into smaller tasks using functions
Syntax:
function functionaName( ) {
// commands to execute
#! /bin/bash
function welcome(){
welcome
#! /bin/bash
function welcome ( ) {
echo "$1";
# call function
welcome Linux
welcome AWS
welcome DevOps
37