Introduction to Unix & Basic Commands
TCS- INTERNAL
Structure of a computer system
⚫Hardware-CPU,Memory,I/O devices
⚫Operating system-
⚫Application programs-word processors,browsers
⚫Users-people,machines,other computers
Document Name
TCS Internal
Responsibilities of OS
⚫Program execution
⚫I/O operations
⚫File system manipulation
⚫Communication
⚫Error detection
Document Name
TCS Internal
Types of OS
⚫Single User Single Tasking OS- eg : MS-DOS
⚫Single User Multitasking OS – eg: Windows
⚫Multiprogramming OS- eg : Unix
Document Name
TCS Internal
Functions of OS
⚫Process management
⚫Memory management
⚫Storage management
⚫Device management
Document Name
TCS Internal
Unix-introduction
Unix was originally developed in 1969 by a group of AT&T employees Ken Thompson,
⚫
Dennis Ritchie, Douglas McIlroy, and Joe Ossanna at Bell Labs.
There are various Unix variants available in the market. Solaris Unix, AIX, HP Unix and
⚫
BSD are a few examples. Linux is also a flavor of Unix which is freely available.
Document Name
TCS Internal
Unix Architecture
Document Name
TCS Internal
Unix Architecture
⚫Kernel – it is a collection of programs written in C that runs directly on hardware.
It interacts with the hardware and most of the tasks like memory management,
task scheduling and file management.
⚫Shell − The shell is the utility that processes your requests. When you type in a
command at your terminal, the shell interprets the command and calls the program
that you want.
Document Name
TCS Internal
Unix Architecture
⚫Types of shell:
⚫ Bourne Shell: executable file name: sh
⚫ C shell: csh
⚫ Korn shell:ksh
⚫ Restricted shell: restricted version of Bourne shell
Document Name
TCS Internal
Unix File System
'/' is used to denote the separation of directories and files.
Document Name
TCS Internal
Unix Architecture
⚫Types of shell:
⚫ Bourne Shell: executable file name: sh
⚫ C shell: csh
⚫ Korn shell:ksh
⚫ Restricted shell: restricted version of Bourne shell
Document Name
TCS Internal
How to work in Unix
Document Name
TCS Internal
Directory & File management
HolyAngelsSchool – Teachers, Students
Top Students details Class Teachers details Class Room details
/home/u390341/HolyAngelsSchool
Amrutha 5A Deepthi 5A 100 5A
Neethu 5B Jeena 5B 101 5B
Sara 6A David 6A 102 6A
Sarath 6B Mridula 6B 103 6B
Sandra 7A Sonam 7A 104 7A
Noorah 7B Sophia 7B 105 7B
Document Name
TCS Internal
Pipe
Pipe is used to connect or combine two or more commands. In Pipe, the output of left
1) echo “My name is Nisha” | cat > new.txt
2) a=30
b=20
echo “$a + $b” | bc
3) echo “$a + $b” | bc | cat > new.txt
Document Name
TCS Internal
Date/bc/cal
Document Name
TCS Internal
Filters
cat (Concatenate File)
cat, cat >, cat>> – Viewing all records and editing files
cat -A filename – To show all
cat -b filename – To show the numbered non-blanks
cat -n filename – To show numbered display
cat -T filename – To show the tab letters
Document Name
TCS Internal
Filters – wc/head/tail/
wc – To take the count
O/p format 1 6 27 newfile.txt
wc -l filename – To print the count of lines
wc -c/m/w filename – To print the number of bytes, chars,
head – To print the data from the top
head -n filename n = no of lines to be printed
tail – To print the data from the bottom
tail -n filename n = no of lines to be printed
Concatenate to select a particular record –>
head -3 filename | tail -1
Document Name
TCS Internal
Filters – sort/uniq
sort – To sort and print a file with different
sort [option] filename
Sort filename
Sort -r filename
uniq – To filter only unique lines and remove the
uniq inputfile outputfile
uniq -c i/pfile – Number of repetition
uniq -d i/pfile – Repeated line
uniq -D i/pfile – Duplicate lines
uniq -i i/pfile – ignore case
Document Name
TCS Internal
Filters – cut/paste/join
cut – To cut a particular part from the file
cut -b1 filename 1st byte
cut -b1-7 filename 1-7 byte
cut -c1 filename 1st char
cut -d ',' -f1 filename cut field wise using delimiter
paste – To merge 2 files
paste inputfile outputfile
join – To merge two files using an identical field
join file1 file2 both files should have common field
Document Name
TCS Internal
Filters – grep
Grep – Global Regular Expression print
Used to search a string in a given file
grep <search value> filename
Eg: grep “Mumbai” places.txt
grep -e “Mumbai” places.txt
grep -i “mumbai” places.txt
grep -c “Mum” places.txt
grep -w “Mumbai” places.txt
grep “Mumbai” place*
grep -v “Mumbai” places.txt
Document Name
TCS Internal
Filters – sed
sed – sed is used to replace searched value in a file with
sed [option] script inputfile
Eg: sed -e '/Mumbai/g places.txt Space out the line with Mumbai
sed -n '/Mumbai/p places.txt print the line with Mumbai
sed -e d places.txt delete the entire lines
sed -e 3d places.txt delete the 3rd line
sed -e '/Mumbai/d' places.txt search and delete line
sed G places.txt To insert line
sed 's/Chennai/Trichy/g' places.txt Replace Chennai with Trichy
Document Name
TCS Internal
Filters – awk
awk – Interpreted programming language
⚫Used to analyse and process text files
⚫Pattern scanning and processing
⚫View the text files in Records and fields
⚫Conditional statements and loops
⚫Formatted reports can be created
⚫Search one or more files to see if data present
awk Blocks
BEGIN END
Declare & initialize ⚫Print the end result
⚫Execute before read ⚫Execute after read
Document Name
TCS Internal
Filters – awk
awk – Some examples for file reading
⚫awk '{print}' places.txt print the content of file
⚫awk -F "," '{print$1, $3}' ManagerDetailsBkp Print some fields
⚫awk '/Chennai/' places.txt search & print
⚫awk '/Jackson/' ManagerDetailsBkp| awk -F "," '{print$1, $2}'
⚫awk '/Clara/' ManagerDetailsBkp| awk -F ";" '{print$1, $3}'
Document Name
TCS Internal
Filters – Calculation & Conditions
BEGIN & END blocks are used to perform the calculation, conditions and print result.
⚫awk -F "," 'BEGIN {S=0}{S=S+$5} END {print "Total Salary per month ", S}' EmployeeD
⚫awk '/10000/ {count++} END {print " Count of Employees with salary 10K ", count}' Em
⚫awk -F "," '/10000/ {print " Name of Employees with salary 10K ", $1}' EmployeeDetail
⚫awk 'BEGIN{FS=",";OFS="$"}{print $1,$2}' EmployeeDetails.txt
Document Name
TCS Internal
Filters – Calculation & Conditions
awk 'END{print FNR}' EmployeeDetails.txt
⚫ print total number of lines in a file
awk 'BEGIN{FS=","}{print length($1)}' EmployeeDetails.txt
⚫ Uses builtin string funct
awk 'BEGIN{s=1;while (s<102) {print s;++s}}'
⚫ print 1 to 101
awk 'BEGIN{FS=",";OFS="$"}{print $1,$2}' EmployeeDetails.txt
⚫
awk 'BEGIN{FS=OFS=","}{if($3=="Kolkota") {(gsub($5,"25000"))}} 1' EmployeeDetail
⚫
tail -n5 input1|awk 'BEGIN{FS=","}{c=$1" "$2;print "name&age:";print c;print "loc:";pri
⚫
Document Name
TCS Internal
Shell Scripting
Shell scripting or Shell program is a file containing a list of commands in a given seq
Benefits:
⚫Automation of repetitive task
⚫Create own tools or utilities
⚫Automation of command input
⚫Create simple application
⚫Shell script editor – vi or vim
⚫format → vi file.sh
⚫First command of shell script is → #!/bin/sh
Document Name
TCS Internal
Shell Scripting
To execute shell script → ./file.sh, sh file.sh
To save the vi file → esc + :wq
Example:
vi list.sh
#!/bin/sh
ls
vi print.sh
#!/bin/sh
echo “Hello World”
vi calc.sh
#!/bin/sh
a=200 b=100
Echo “$a+$b” | bc
Document Name
TCS Internal
Shell Scripting
vi listcity.sh
#!/bin/sh
grep Idukki empcity
vi calc.sh
#!/bin/sh
echo "Value 0f A = $1"
echo "Value of B = $2"
echo "$1 + $2" | bc
sh calc.sh 100 300
Document Name
TCS Internal
Shell Scripting – File conditions
-f & -e options can be used to make sure that the file used is a existing and its a regular
-f filename ==> To make sure that the file is a regular file
-e file name ==> To make sure that the file exists.
Sample code:
#!/bin/sh
if [ -e places.txt ] then echo "File exists"
else echo "File doesn't exist"
fi
if [ -f places.txt ] then echo "File is a regular file"
else echo "File is a Dictionary"
fi
cat places.txt
Document Name
TCS Internal
Shell Scripting
vi condition.sh
#!/bin/sh
a=355
b=353
if [ $a -eq $b ]
then
echo "a & b are equal"
else
echo "a & b are not equal"
fi
sh condition.sh
Document Name
TCS Internal
Thank You!!
TCS- INTERNAL