You are on page 1of 20

UNIX Learnings

Intoduction of UNIX Shell


1.What is Shell? ->Shell is the command interpreter for Unix systems. 2.What is shell-scripting? A shell scripting is a text file with set of Unix commands in it. Shell scripts usually begin with a #! and a shell name(Complete pathname of shell). Example:

#!/bin/ksh (Called as magic line.Must be first line in the shell scripting.) We can interact with Kernel using different shell commands such as sort,ls,grep etc.

Fig : UNIX Architecture

Basic Unix Commands:

Unix commands are categorize into different types as shown in fig.

Fig : Categorizes UNIX Commands Descriptions of Unix commands Sample files :


1. Sample.txt Content:
In Xanadu ,did Kubla Khan Where Alph , the sacred river, ran Through, caverns measureless to man

2.cal.txt Content:

7*8 quit 3.

1.txt Content: aaa bbb ccc ddd eee fff ggg 2.txt content: bbb cc ddd eee fff ggg hhh sdf sdf dsf sdf

5. Uniq.txt content : ram ram shyam shyam abhijit pooja

Type

Com man d Nam e

Descript ion

Syntax

Example

Result

Gener al Purpo se

Comm ands date print or date set the system date and time Display echo message Text/exp on screen ression An bc arbitrary expressio precision n calculator language bc filename Mailx is used for sending mail/atta chment to particular email id mail x s Subj ect infor matio n email id Tue Oct 27 15:35:08 CDT 2009 echoW elcome bc 7*8 quit Welcome

echo

bc

56

Mailx

bc cal.txt 56 (mentioned on top) mailx s Test Mail send to Subject abc@gmail.com abc@gmail.co m

Handl ing Files Comm ands pwd Short for print working directory the pwd comman d displays the name of the pwd pwd / appldata/d ownload Gives Current Directory

cd

current working directory Change Directory

cd pathnam e

cd /appldata/dow nload

Directory change to download

cd ~ or cd !

Directory shortcut to change home dir(~) to Home directory Back one step Current directory is previous directory tmp directory is created in the current directory path tmp directory removed from current directory path cat In Xanadu ,did Kubla Khan on
Where Alph , the sacred river, ran Through caverns measureless to man

cd .. mkd Create a ir directory mkdir tmp


mkdir

director y name

rmdi r

Remove a directory

rmdir tmp
rmdir directory name

cat

The cat command concatenat es and display files

cat filename

sample.txt (content top)

cp

Copies files from one location to another. Rename s a file or moves it from one director y to another director y.

cp /appldata/dow cp oldpathnam nload/sample.t xt e /appldata/ON newpathna COP me

File sample.txt copy to /appldata/ ONCOP

mv

mv oldname newname

mv abc.txt Rename bcd.txt abc.txt to bcd.txt

Move the file myfile.txt to newdirector y

mv myfile.txt newdirectory Myfile.txt move to /


newdirecto ry

wc

Short for word wc count, filename wc displays a count of lines, words, and characte rs in a file.

wc sample.txt

3 17 98 sample.txt5

wc l (Total 3 = Lines number of 13 = Words lines) wc c (Total 98=Character number of Character)

cmp

Compar es two cmp files and file1 tells you file2 what line number s are different Displays two files diff file1 and file2 prints the lines that are different .

cmp 1.txt 2.txt

1.txt 2.txt differ: char 1, line 1

diff

diff 2.txt

1.txt

1d0 < aaa 3c2 < ccc --> c c 7a7 > hhh

Hints:
d a line was deleted c a line was changed a a line was added lines with< or > are the difference themselves

com m

For Commo comm n data file1 between files

comm 2.txt

1.txt

aaa bbb

c c

file2
ccc ddd eee fff ggg hhh ---------------Hint: 1)comm -1 fil1.txt file2.txt ->select lines common to

file1
2) comm -2 fil1.txt file2.txt ->select lines common to

file2 3) comm -3
fil1.txt file2.txt ->select lines not common to

both files

gzi Gzip redu ces the gzip size of filename the named files using LempelZiv coding (LZ77). Wheneve r possible, each file is replaced by one with the extension .gz, whil e keeping the same ownershi p modes, access and modificati on times

gzip 1.txt

1.txt.gz

gunzi To decompre p ss files.

gunzip 1.txt.gz 1.txt

gunzip filename .gz OR gzip d filename

.gz OR gzcat filename .gz


zip

To compres zip s files zipfilena me.zip file1.ht ml file2.txt

zip

archive.zip
1.txt 2.txt sample.txt

archive.zi p Hint: If a.zip already exists .Zip command does not overwrite . If file exists then file will either updated or appended to archieve. zip 1.txt 2.txt Sample.t xt

unzi To decompre p ss files.

unzip archive. zip

unzip archive.zip

Basic Files Comm ands

ls

ls l Listing File ls attribute (option) s filename (optiona l)

apdmd51 7:/home/ myscetl>l s -l total 12434 -rw-r--r-1 myscetl dstage 28 Oct 17 06:09 1.txt -rw-r--r-1 myscetl dstage 28 Oct 17 06:09 2.txt drwxr-xrx 3 myscetl dstage 96 Sep 29 23:44 Abhijit Hint: The list preceded by the words total

ls lrt filename
ls lrt 1.txt

12434

-rw-r--r-1 myscetl dstage 28 Oct 17 06:09 1.txt

Simpl e Filteri ng

hea Displays

head 1.txt

aaa bbb ccc ddd eee fff ggg

the first few lines of a file

head filename

head -2 1.txt

aaa bbb Hint : head n filename Display only n number of lines ggg (Last line only)

tail

Displays the last tail few filename lines of a file.

tail -1 1.txt

Hint: Take only last line only --------------------------------

bbb
tail +2 1.txt ccc Hint: Line start from ddd 3 to last line

eee

fff
cut

Helps split the lines of a file(verti cally)

cut delimiter -field number filename

ggg cut d , -f1 In sampl.txt Xanadu


Hint: Delimiter Where is comma (,). Alph Default delimiter is Through tab. ----------------Cut c 1-5 In Xa sample.txt Hint: -c option is for specific column only (1 to 5 column).

Wher Thro

past e

Joins the lines of different files

paste file1 file2 file3

paste 2.txt

1.txt aaa

bbb bbb ccc ddd ddd eee eee fff ggg hhh fff ggg cc

Sorts files, merges files that are already sorted, and checks files to determi ne if they have been sorted uni Reports q or deletes repeate d lines in a file.
sort

sort sample.txt Through

sort filename

caverns measurel ess to man Where Alph , the sacred river, ran In Xanadu , did Kubla Khan
uniq uniq.txt

Ram shyam abhijit pooja

Uniq filename

tr

Translat es tr characte options rs. expr1 expr2 standard input

[Only unique values ] tr "[A-Z]" "[a- in xanadu z]" ,did kubla <sample.txt khan
Hint: where Replace all alph , the capital character with sacred river, ran small character

through caverns measurel ess to

man
Filteri ng Using Regul ar Expre ssion egre Searche p s a file

for a pattern. Extende d version of grep.Egr ep is basically used for regular expressi on. gre Used for grep i AAA aaa p filename pattern grep matchin option g filename
sed

egrep option filename

egrep 1.txt

"a+" aaa

A stream editor

sed pattern {Action; Hint: Replace } Khan with Don filename

sed 's/Khan/Don/' sample.txt

In Xanadu , did Kubla Don Where Alph , the sacred river, ran

Through caverns measurel ess to man


Proce ss Comm ands ps

Shows status of process es Runs a comman d without hangups .

ps f

Ps -option

nohu p

Hint: Full listing showing the PPID of each process nohup abc.sh nohup.ou

nohup sciptna me.sh & Hint: Gives process id (ps id) and create nohup.o ut for output of the running status. We can cat nohup.o ut in

t (for output)

order to see the status


Shell progr ammi ng comm ands read read name The read read stateme variable nt is the name shells internal tool for taking input from the user. exist 0 To exist exist Hint: number( exit 0 0/1) Everthin g is fine

echo $name

exit

expr

exit 1 When somethi ng went wrong The expr expr comman number d reads the

expr 3 + 5

if

Expressi on paramet er, evaluate s it, and writes the result to standar d output. To check the conditio n

1 (space) operator (+/-/*) (space) number 2

if [conditio n] then Expr fi

if [ $count = 10 ]; then echo "Number=10" fi;

Number= 10

case

The case st atement is always preferred when there are many items to select from instead of using a large if/elif/else statement

Case expr value In Case1) expr ; Case2: expr; esac

NUM =10 case $NUM in "nine" ;; 10) echo "ten" ;; *) echo "INVALID NUMBER!" ;; esac

ten

while

While {conditi on} do expressi

Alphabet=a b c d e Count=0 while[$Count lt -5]

on done

do Count=`expr $Count + 1` done

You might also like