530 likes | 666 Views
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
E N D
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 • Create directory • mkdir sample • rmdir • Remove directory • rmdir sample
Unix Commands • cd • Change directory • cd sample • touch • create empty file • touch file1 • cat • create file • cat > file1 • cat file1 • cat file1 file2 > newfile
Unix Commands • ls • display contents of directory • ls • ls –a • ls –l • ls t*
Objective To understand unix commands: cp rm mv ln date 6
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 7
Unix Commands ln Create link for a file ln file1 file2 date Display date and current time 8
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 9
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 10
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
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
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,...
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
File Comparison • $cmp • Compares files • $cmp partaa partab • partaa partab differ: byte 1, line 1
File Comparison • $comm • Compares files, displays what is common • Requires sorted files as input $comm sample1 sample2 moon planet star sun sample1 moon planet sun sample2 moon star
File Comparison • Difference between two files • $diff sample1 sample2 $diff sample1 sample2 2,3c2,3 < planet < sun --- > star > tree sample1 moon planet sun sample2 moon star tree
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)
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
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 20
vi Editor • Screen-based editor for Unix • Invoke using • vi filename
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
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.
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.
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.
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.
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
(Shell Programming) Basics Commands of Shell Programming Conducted by Abhishek Singh Verma 29
Objective To understand Shell Programming concepts: Linux Kernel Linux Shell Shell Script User-Defined Variables Test Condition 30
Linux Kernel • Heart of Linux OS • Acts as an intermediary between the computer hardware and various programs/application/ shell.
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
Shell Script • A series of command written in plain text file • Just like a batch file in MS-DOS
Running a Shell Script • Write a shell script – vi editor • Assign execute permission to it • chmod u+x test.sh • Execute it • ./test.sh
User Defined Variables • Syntax • Variable=Value • Example • count=2 • name=ritu • echo $count
Numeric Comparison: test • $ x=5; y=7 $ test $x –lt $y; echo $? 0 • Operators:
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 37
User Defined Variables • Syntax • Variable=Value • Example • count=2 • name=ritu • echo $count
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
Shell Arithmetic • Syntax: • expr operand1 operator operand2 • Examples: • $ expr 1 + 3 • $ expr 2 – 1
Exercises – 2 • Try the following. What do you observe? • $ expr 10 / 2 • $ expr 20 % 3 • $ expr 10 \* 3 • $ echo `expr 6 + 3`
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?"
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
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?
Numeric Comparison: test • $ x=5; y=7 $ test $x –lt $y; echo $? 0 • Operators:
True/False in Linux Shell • Usually, • True -> 1 [Non-Zero value] • False -> 0 • But in Linux Shell, • True -> 0 • False -> 1 [Non-Zero value]
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
if..then..else • Syntax: if condition then statement(s) else statement(s) fi
Multilevel if..then..else • Syntax: if condition then statement(s) elif condition then statement(s) elif condition then statement(s) else statement(s) fi
for { variable name } in { list } do Statements done Example: for i in 1 2 3 4 5do echo "Welcome $i times"done Output: Welcome 1 times Welcome 2 times Welcome 3 times Welcome 4 times Welcome 5 times For Loop