IT Infrastructure & Management Lab

52
1 IT Infrastructure & Management Lab Basics Commands of Unix Conducted by Abhishek Singh Verma

description

IT Infrastructure & Management Lab. Basics Commands of Unix Conducted by Abhishek Singh Verma. Objective. To understand unix commands: who who am I mkdir rmdir cd touch cat ls. Unix Commands. who am i Identifies user who All users who have logged in currently mkdir - PowerPoint PPT Presentation

Transcript of IT Infrastructure & Management Lab

Page 1: IT Infrastructure & Management  Lab

1

IT Infrastructure & Management

Lab

Basics Commands of Unix

Conducted by Abhishek Singh Verma

Page 2: IT Infrastructure & Management  Lab

2

Objective To understand unix commands:

who who am I mkdir rmdir cd touch cat ls

Page 3: IT Infrastructure & Management  Lab

3

Unix Commands who am i

Identifies user who

All users who have logged in currently mkdir

Create directory mkdir sample

rmdir Remove directory rmdir sample

Page 4: IT Infrastructure & Management  Lab

4

Unix Commands cd

Change directory cd sample

touch create empty file touch file1

cat create file cat > file1 cat file1 cat file1 file2 > newfile

Page 5: IT Infrastructure & Management  Lab

5

Unix Commands ls

display contents of directory ls ls –a ls –l ls t*

Page 6: IT Infrastructure & Management  Lab

6

Objective To understand unix commands:

cp rm mv ln date

Page 7: IT Infrastructure & Management  Lab

7

Unix Commands cp

Copy one file to another cp file1 file2

rm Remove file rm sample rm –i rm –f rm –r

mv Rename a file mv file1 file2

Page 8: IT Infrastructure & Management  Lab

8

Unix Commands ln

Create link for a file ln file1 file2

date Display date and current time

Page 9: IT Infrastructure & Management  Lab

9

Objective To understand unix commands:

Basic Unix Commands clear cal wc script ispell pwd bc

File Manipulation Commands file split

File Comparison cmp comm diff

File Permissions chmod

Page 10: IT Infrastructure & Management  Lab

10

Unix Commands $clear

Clear the terminal screen Can also be achieved by command $tput clear

$cal Display calendar of present month $cal $cal 1 2010 $cal 2010

$wc Compute word count (number of lines, words,

characters) wc -l wc -w wc –c

Page 11: IT Infrastructure & Management  Lab

Unix Commands $ls | wc

Standard output of ls passed as input to wc connected through pipe (|)

$script Records terminal session to file typescript Type exit to stop logging results

$ispell Apply spell-check on a file

$pwd Print working directory /home/ritu/documents

$echo $HOME /home/ritu

Page 12: IT Infrastructure & Management  Lab

Unix Commands $bc

Starts calculator $bc 5 + 7 12 12 * 12 144 Type quit to exit

$file Determine type of file $file sample sample: ASCII English text

Page 13: IT Infrastructure & Management  Lab

File manipulation commands

$split Split a file into smaller files Generates files of 1000 lines (default

size) $split -10 sample

Creates files xaa, xab, xac,... $split -10 sample part

Creates files partaa, partab, partac,...

Page 14: IT Infrastructure & Management  Lab

File manipulation commands $more Display file one screen at a time $more sample

$head Display first 10 lines of a file $head -n 5 sample

$tail Display last 10 lines of a file $tail -n 5 sample

Page 15: IT Infrastructure & Management  Lab

File Comparison

$cmp Compares files

$cmp partaa partab partaa partab differ: byte 1, line 1

Page 16: IT Infrastructure & Management  Lab

File Comparison $comm

Compares files, displays what is common Requires sorted files as input

sample1 moonplanet

sun

sample2 moonstar

$comm sample1 sample2 moon

planet star

sun

Page 17: IT Infrastructure & Management  Lab

File Comparison Difference between two files $diff sample1 sample2

sample1 moonplanet

sun

sample2 moonstartree

$diff sample1 sample22,3c2,3< planet< sun---> star> tree

Page 18: IT Infrastructure & Management  Lab

File Permissions $ ls -l sample1

-rw-r--r-- 1 deepti deepti 16 2010-02-03 12:54 sample1

First column indicates file permissions: Read (r) Write (w) Execute (x)

Permissions assigned to: User (u) Group (g) Others (o)

Page 19: IT Infrastructure & Management  Lab

Using chmod to change file permissions

Use chmod command to change file permissions

$chmod u+x sample1 #Assign execute permission to user

$chmod g+w sample1 #Assign write permission to group

$chmod a+rw sample1 #Assign read-write permission to all

$chmod a=r sample1 #Keep only read permission for all

$chmod 777 sample1 #Assign rwx permission for all

Page 20: IT Infrastructure & Management  Lab

20

Objective To understand:

What is vi editor? Modes of vi editor vi editor commands

Adding Text in vi Cutting and Pasting in vi Moving around text in vi Saving and Quitting vi

Page 21: IT Infrastructure & Management  Lab

vi Editor

Screen-based editor for Unix Invoke using

vi filename

Page 22: IT Infrastructure & Management  Lab

vi modes

Input Mode Lets you type text into your

document. Command Mode

Lets you enter vi commands Last Line Mode

Lets you enter commands that appear on the bottom line of the screen

Page 23: IT Infrastructure & Management  Lab

vi Modes

Page 24: IT Infrastructure & Management  Lab

Adding Text in vi a Add text after the cursor. A Add text to the end of the current line. i Insert text at the cursor. I Insert text at the beginning of the current line. o Open a new line below the current line and

add text. O Open a new line above the current line and

add text. s Substitute the letter underneath the cursor

with letter you type, and insert text. S or c Delete the current line and substitute it

with text you type. R or C Replace current text with text you type.

Page 25: IT Infrastructure & Management  Lab

Cutting and Pasting Text in vi

x Delete the letter beneath the cursor. dw Delete the letter beneath the cursor and the rest of the word. #dw Delete the following number of words, including the current

word. D Delete from the cursor to the end of the line. dd Delete the current line. #dd Delete the following number of lines, including the current

line. yy Copy or "yank" the current line. #yy Copy or "yank" the following number of lines, including the

current line. p Paste the current copied or deleted letters, words, or lines after

the cursor. J Join the next line with the current line (erases a carriage return). u Undo the last edit. . Redo the last editing command.

Page 26: IT Infrastructure & Management  Lab

Moving Around in a vi Text File

j Move down to the next line. k Move up to the previous line. h Move backward by letter. l Move forward by letter. w Move forward by word. b Move backward by word. e Move forward to the end of a word.

Page 27: IT Infrastructure & Management  Lab

Pattern Search /word Search for specified word in

forward direction. ?word Search for specified word

on backward direction. n Search for next occurrence of

specified word. N Search for previous occurrence

of specified word.

Page 28: IT Infrastructure & Management  Lab

Saving and Quitting vi

:wq Save and quit out of the file :w Save the current file without

quitting :q Quit if no edits have occurred :q! Quit without saving edits

Page 29: IT Infrastructure & Management  Lab

29

(Shell Programming)Basics Commands of Shell Programming

Conducted by Abhishek Singh Verma

Page 30: IT Infrastructure & Management  Lab

30

Objective To understand Shell

Programming concepts: Linux Kernel Linux Shell Shell Script User-Defined Variables Test Condition

Page 31: IT Infrastructure & Management  Lab

Linux Kernel

Heart of Linux OS Acts as an

intermediary between the computer hardware and various programs/application/ shell.

Page 32: IT Infrastructure & Management  Lab

Linux Shell Shell is a command language

interpreter that executes commands read from the standard input device (keyboard) or from a file.

Types of shells: Bash C-Shell Ksh

$ echo $SHELL

Page 33: IT Infrastructure & Management  Lab

Shell Script

A series of command written in plain text file

Just like a batch file in MS-DOS

Page 34: IT Infrastructure & Management  Lab

Running a Shell Script

Write a shell script – vi editor Assign execute permission to it

chmod u+x test.sh Execute it

./test.sh

Page 35: IT Infrastructure & Management  Lab

User Defined Variables

Syntax Variable=Value

Example count=2 name=ritu

echo $count

Page 36: IT Infrastructure & Management  Lab

Numeric Comparison: test $ x=5; y=7

$ test $x –lt $y; echo $?0

Operators:

-eq is equal to

-ne is not equal to

-lt is less than

-le is less than or equal to

-gt is greater than

-geis greater than or equal to

Page 37: IT Infrastructure & Management  Lab

37

Objective To understand Shell

Programming concepts: User-Defined Variables Shell Arithmetic Read statement Command line arguments Test condition if condition if..then..else Multilevel if..then..else For loop

Page 38: IT Infrastructure & Management  Lab

User Defined Variables

Syntax Variable=Value

Example count=2 name=ritu

echo $count

Page 39: IT Infrastructure & Management  Lab

Exercises – 1 Q.1. Define variable x with value 10 and print

it on screen. Q.2. Define variable xn with value John and

print it on screen Q.3. Print sum of two numbers, let's say 6

and 3? Q.4. Define two variable x=20, y=5 and then

to print division of x and y (i.e. x/y) Q.5. Modify above and store division of x and

y to variable called z

Page 40: IT Infrastructure & Management  Lab

Shell Arithmetic

Syntax: expr operand1 operator operand2

Examples: $ expr 1 + 3 $ expr 2 – 1

Page 41: IT Infrastructure & Management  Lab

Exercises – 2

Try the following. What do you observe? $ expr 10 / 2 $ expr 20 % 3 $ expr 10 \* 3 $ echo `expr 6 + 3`

Page 42: IT Infrastructure & Management  Lab

Read statement Use to get input (data from user)

from keyboard and store (data) to variable.

Syntax: read variable1, variable2,...variableN

Example:echo “Please enter your name"read nameecho "Hello $name, how are you?"

Page 43: IT Infrastructure & Management  Lab

Command Line Arguments $# holds number of arguments

specified on command line And $* or $@ refer to all arguments

passed to script Example:

sh test.sh hello world test.sh -> $0 hello -> $1 world -> $2

Page 44: IT Infrastructure & Management  Lab

Exercises – 3 Try the following: test.sh

echo "Total number of command line argument are $#“echo "$0 is script name“echo "$1 is first argument“echo "$2 is second argument"

What happens when you say $1=5 in the shell scipt?

Page 45: IT Infrastructure & Management  Lab

Numeric Comparison: test $ x=5; y=7

$ test $x –lt $y; echo $?0

Operators:

-eq is equal to

-ne is not equal to

-lt is less than

-le is less than or equal to

-gt is greater than

-geis greater than or equal to

Page 46: IT Infrastructure & Management  Lab

True/False in Linux Shell

Usually, True -> 1 [Non-Zero value] False -> 0

But in Linux Shell, True -> 0 False -> 1 [Non-Zero value]

Page 47: IT Infrastructure & Management  Lab

Decision Making: if condition Syntax:

if condition then statement(s) fi Example:

if cat $1 then echo “cat command was successful” fi test.sh myfile

Page 48: IT Infrastructure & Management  Lab

if..then..else

Syntax:if condition

then statement(s)

else statement(s)

fi

Page 49: IT Infrastructure & Management  Lab

Multilevel if..then..else Syntax:

if condition then statement(s)

elif condition then

statement(s)elif condition then

statement(s)else

statement(s)fi

Page 50: IT Infrastructure & Management  Lab

For Loopfor { variable name } in { list } do

Statements

done

Example:for i in 1 2 3 4 5do

echo "Welcome $i times"done

Output:Welcome 1 timesWelcome 2 timesWelcome 3 timesWelcome 4 timesWelcome 5 times

Page 51: IT Infrastructure & Management  Lab

For loop Example:

for (( i = 0 ; i <= 5; i++ ))do echo "Welcome $i times"done

Output:Welcome 0 timesWelcome 1 timesWelcome 2 timesWelcome 3 timesWelcome 4 timesWelcome 5 times

Page 52: IT Infrastructure & Management  Lab

Exercises – 4

Using for loop, do the following: Q1. Print multiplication table for a given

number Q2. Generate following pattern:

1 1 1 1 12 2 2 2 23 3 3 3 34 4 4 4 45 5 5 5 5