Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Linux environment
BCA – VI
Paper Code 306
Introduction: What is Unix?
• An operating system
• Developed at AT&T Bell Labs in the 1960’s
• Command Line Interpreter
• GUIs (Window systems) are now available
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Introduction: Unix vs. Linux
• Unix was the predecessor of Linux
• Linux is a variant of Unix
– So is Mac OS X, so much of this tutorial applies to
Macs as well
• Linux is open source
• Most of the machines you’ll use in the
Bioinformatics program are running the Linux
OS
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Introduction: Why Unix/Linux?
• Linux is free
• It’s fully customizable
• It’s stable (i.e. it almost never crashes)
• These characteristics make it an ideal OS for
programmers and scientists
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Connecting to a Unix/Linux system
• Open up a terminal:
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Connecting to a Unix/Linux system
• Open up a terminal:
The “prompt”
The current directory (“path”)
The host
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
What exactly is a “shell”?
• After logging in, Linux/Unix starts another program
called the shell
• The shell interprets commands the user types and
manages their execution
• The shell communicates with the internal part of the operating
system called the kernel
• The most popular shells are: tcsh, csh, korn, and bash
• The differences are most times subtle
• For this tutorial, we are using bash
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Help!
• Whenever you need help with a command
type “man” and the command name
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Help!
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Help!
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Help!
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Unix/Linux File System
/home/john/portfolio/
/home/mary/
The Path
NOTE: Unix file names
are CASE SENSITIVE!
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: pwd
• To find your current path use “pwd”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: cd
• To change to a specific directory use “cd”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: cd
• “~” is the location of your home directory
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: cd
• “..” is the location of the directory below
current one
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: ls
• To list the files in the current directory use “ls”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: ls
• ls has many options
– -l long list (displays lots of info)
– -t sort by modification time
– -S sort by size
– -h list file sizes in human readable format
– -r reverse the order
• “man ls” for more options
• Options can be combined: “ls -ltr”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: ls -ltr
• List files by time in reverse order with long listing
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
General Syntax: *
• “*” can be used as a wildcard in unix/linux
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: mkdir
• To create a new directory use “mkdir”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: rmdir
• To remove and empty directory use “rmdir”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: rmdir
• To remove and empty directory use “rmdir”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Creating files in Unix/Linux
• Requires the use of an Editor
• Various Editors:
1) nano / pico
2) vi
3) emacs
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Displaying a file
• Various ways to display a file in Unix
– cat
– less
– head
– tail
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: cat
• Dumps an entire file to standard output
• Good for displaying short, simple files
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: less
• “less” displays a file, allowing
forward/backward movement within it
– return scrolls forward one line, space one page
– y scrolls back one line, b one page
• use “/” to search for a string
• Press q to quit
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: head
• “head” displays the top part of a file
• By default it shows the first 10 lines
• -n option allows you to change that
• “head -n50 file.txt” displays the first 50 lines
of file.txt
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: head
• Here’s an example of using “head”:
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: tail
• Same as head, but shows the last lines
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
File Commands
• Copying a file: cp
• Move or rename a file: mv
• Remove a file: rm
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: cp
• To copy a file use “cp”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: mv
• To move a file to a different location use “mv”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: mv
• mv can also be used to rename a file
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: rm
• To remove a file use “rm”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: rm
• To remove a file “recursively”: rm –r
• Used to remove all files and directories
• Be very careful, deletions are permanent in
Unix/Linux
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
File permissions
• Each file in Unix/Linux has an associated
permission level
• This allows the user to prevent others from
reading/writing/executing their files or
directories
• Use “ls -l filename” to find the permission
level of that file
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Permission levels
• “r” means “read only” permission
• “w” means “write” permission
• “x” means “execute” permission
– In case of directory, “x” grants permission to list
directory contents
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
File Permissions
User (you)Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
File Permissions
Group
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
File Permissions
“The World”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: chmod
• If you own the file, you can change it’s permissions with
“chmod”
– Syntax: chmod [user/group/others/all]+[permission] [file(s)]
– Below we grant execute permission to all:
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Running a program (a.k.a. a job)
• Make sure the program has executable
permissions
• Use “./” to run the program
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Running a program: an example
• Running the sample perl script “hello_world.pl”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Ending a program
• To end a program use “ctrl-c”. To try it:
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: ps
• To view the processes that you’re running:
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: top
• To view the CPU usage of all processes:
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: kill
• To terminate a process use “kill”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Input/Output Redirection (“piping”)
• Programs can output to other programs
• Called “piping”
• “program_a | program_b”
– program_a’s output becomes program_b’s input
• “program_a > file.txt”
– program_a’s output is written to a file called “file.txt”
• “program_a < input.txt”
– program_a gets its input from a file called “input.txt”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
A few examples of piping
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
A few examples of piping
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: wc
• To count the characters, words, and lines in a
file use “wc”
• The first column in the output is lines, the
second is words, and the last is characters
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
A few examples of piping
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Command: grep
• To search files in a directory for a specific
string use “grep”
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
What is a Regular Expression?
• Pattern to match all or part of a line of text
– Expressed in a formal, albeit weird, language
– For example:
^.*:Mike
matches lines that start (^) with any string (.*) and contain a colon
followed by Mike
• ^, . and * are called meta characters
– They do not represent themselves, but have other special meaning.
• :, M, i, k and e are normal characters
– The do represent themselves
55
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Where are
Regular Expressions Used?
• Special commands that “know” about them
– Vi-Uses them in searching for a string: /regexp/
• Uses them in substitite command: s/regexp/replace/sed
– Edit a file (like vi) as a filter in a pipeline
… | sed editing-commands | …
– grep , fgrep (fixed grep) , egrep (extended grep)
• Print lines of a file that match a regexp
– awk
• Process input files looking for lines that match a regexp and
processing those lines
56
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Regular expression
• A regular expression is a pattern of characters
used for describing sets of strings
• A pattern or sequence of characters
– Upper and lower case
– Digits
– Space, underscore, etc
• Metacharacters
57
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Basic Regular Expressions
• Patterns that match a single character
– All regular letters match themselves
a b z T Q 0 1 9
– . (a single dot) matches any single character except
newline.
• In awk, dot can match newline also
• (like ? in filename generation)
– A set of characters that matches any single character from
the set (just like filename generation)
[aeiou]
[a-z0-9]
[A-Za-z-]
[a-m]
58
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Metacharacter
* Matches 0 or more occurrences of the
preceding char
[…] Matches any one of characters enclosed
between the brackets.
- dash indicates a range when inside sq bkts.
[^ - negates what's inside brackets]
59
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Metacharacter (con’t)
 - backslash - escape character - just like before.
– . means match a dot
– This means  is a meta character
–  means match 
Positional indicators:
• ^ anchor to beginning of line
• $ anchor to end of line
60
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
• "Some people, when confronted with a Unix
problem, think ‘I know, I’ll use sed.’ Now they
have two problems.”
-
61
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Regular expression examples
• Peach
• a*c
– cxxx, acxxx, aaacxxxx
• a.c
– a+c, abc, match, a3c
• [tT]he
– The, the
• Ch[^0-9]
– Chapter, Chocolate
• ^the
– Start with the
• Friends$
– End with Friends
62
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Regular expression examples
• L..e
• $[0-9]*.[0-9]
• ^[0-9]file.dat
• [^0-9]file.dat
• MM-DD-YY or MM/DD/YY
– [0-1][0-9][-/][0-3][0-9][-/][0-9][0-9]
63
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Named classes of characters
• [0-9] ---OR --- [[:digit:]]
• [a-z] --- OR --- [[:lower:]]
• [A-Z] --- OR --- [[:upper:]]
• [a-zA-Z] --- OR --- [[:alpha:]]
• [a-zA-Z0-9] ---OR --- [[:alnum:]]
• egrep ‘^[[:lower:]]*$’
64
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Extended Metacharacter (egrep and awk)
Available in egrep and awk NOT in vi, sed, grep or fgrep
? matches zero or one occurrence of the preceding
char
+ Matches 1 or more occurrences of the preceding
char
| Specifies that either the preceding or following
regular expression can be matched
( ) Groups regular expressions
65
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Examples
• “.*” matches all characters between the quotations
• ^$ matches blank lines
• ^.*$ matches the entire line
• Big( Computer)?
• Compan(y|ies) # note: the | is a pipe symbol
• SSN: [0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]
• grep ‘.H[123]’ ch0[12]
– ch01: .H1 “Contents of Distribution Tape”
– ch02: .H2 “A Quick Tour”
66
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Repetition Examples
• Consider a file named text containing:
We're off to see the wizard.
The wonderful wizard of oz.
What a wonderful wizard he was.
The end.
• Try the following commands and explain the
output:
egrep 'f+' text
grep 'ff*' text
grep 'f{2}' text
egrep 'z?' text
67
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Some More Examples
• A price: $[0-9]*.[0-9][0-9]
• A filename, at the start of a line, that starts with a digit
^[0-9]file.dat
• A filename, anyplace in the line, that starts with a non-digit
[^0-9]file.dat
• A social security number
[0-9]{3}-[0-9]{2}-[0-9]{4}
• From 4 to 6 digits: [0-9]{4, 6}
• A date - MM-DD-YY or MM/DD/YY:
[0-1][0-9][-/][0-3][0-9][-/][0-9][0-9]
• A line containing only upper case letters:
^[A-Z]*$
68
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Replacement in sed and vi
• Applies to substitute command:
– s/regexp/replacement/
– Replacement string can contain the metacharacter
‘&’ which means the string that was matched
• Try this
STRING=“Start Again”
echo $STRING | sed 's/Again/& &/'
69
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Process and Thread
• Process
– Execution context
– Program code
– Resources
– Associated threads
• Threads: units of execution
• Process and threads are objects
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Thread Scheduling
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Linux Architecture
• Monolithic kernel
– Contains modular components, however
• UNIX-like or UNIX-based operating system
• Six primary subsystems:
– Process management
– Interprocess communication
– Memory management
– File system management
• VFS: provides a single interface to multiple file systems
– I/O management
– Networking
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Linux Kernel Architecture
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Process and Thread Organization
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Process information
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
System Calls
• Interface between user-level processes and
hardware devices.
– CPU, memory, disks etc.
• Make programming easier:
– Let kernel take care of hardware-specific issues.
• Increase system security:
– Let kernel check requested service via syscall.
• Provide portability:
– Maintain interface but change functional
implementation.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
POSIX APIs
• API = Application Programmer Interface.
– Function defn specifying how to obtain service.
– By contrast, a system call is an explicit request to kernel made via a
software interrupt.
• Standard C library (libc) contains wrapper routines that make system calls.
– e.g., malloc, free are libc routines that use the brk system call.
• POSIX-compliant = having a standard set of APIs.
• Non-UNIX systems can be POSIX-compliant if they offer the required set of
APIs.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Linux System Calls (1)
Invoked by executing int $0x80.
– Programmed exception vector number 128.
– CPU switches to kernel mode & executes a kernel function.
• Calling process passes syscall number identifying system
call in eax register (on Intel processors).
• Syscall handler responsible for:
– Saving registers on kernel mode stack.
– Invoking syscall service routine.
– Exiting by calling ret_from_sys_call().
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Linux System Calls (2)
• System call dispatch table:
– Associates syscall number with corresponding
service routine.
– Stored in sys_call_table array having up to
NR_syscall entries (usually 256 maximum).
– nth entry contains service routine address of
syscall n.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
Initializing System Calls
• trap_init() called during kernel initialization
sets up the IDT (interrupt descriptor table) entry
corresponding to vector 128:
– set_system_gate(0x80, &system_call);
• A system gate descriptor is placed in the IDT,
identifying address of system_call routine.
– Does not disable maskable interrupts.
– Sets the descriptor privilege level (DPL) to 3:
• Allows User Mode processes to invoke exception handlers
(i.e. syscall routines).
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
The system_call() Function
• Saves syscall number & CPU registers used by
exception handler on the stack, except those
automatically saved by control unit.
• Checks for valid system call.
• Invokes specific service routine associated
with syscall number (contained in eax):
– call *sys_call_table(0, %eax, 4)
• Return code of system call is stored in eax.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)

Linux environment

  • 1.
    Chanderprabhu Jain Collegeof Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India) Linux environment BCA – VI Paper Code 306
  • 2.
    Introduction: What isUnix? • An operating system • Developed at AT&T Bell Labs in the 1960’s • Command Line Interpreter • GUIs (Window systems) are now available Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 3.
    Introduction: Unix vs.Linux • Unix was the predecessor of Linux • Linux is a variant of Unix – So is Mac OS X, so much of this tutorial applies to Macs as well • Linux is open source • Most of the machines you’ll use in the Bioinformatics program are running the Linux OS Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 4.
    Introduction: Why Unix/Linux? •Linux is free • It’s fully customizable • It’s stable (i.e. it almost never crashes) • These characteristics make it an ideal OS for programmers and scientists Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 5.
    Connecting to aUnix/Linux system • Open up a terminal: Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 6.
    Connecting to aUnix/Linux system • Open up a terminal: The “prompt” The current directory (“path”) The host Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 7.
    What exactly isa “shell”? • After logging in, Linux/Unix starts another program called the shell • The shell interprets commands the user types and manages their execution • The shell communicates with the internal part of the operating system called the kernel • The most popular shells are: tcsh, csh, korn, and bash • The differences are most times subtle • For this tutorial, we are using bash Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 8.
    Help! • Whenever youneed help with a command type “man” and the command name Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 9.
    Help! Chanderprabhu Jain Collegeof Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 10.
    Help! Chanderprabhu Jain Collegeof Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 11.
    Help! Chanderprabhu Jain Collegeof Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 12.
    Unix/Linux File System /home/john/portfolio/ /home/mary/ ThePath NOTE: Unix file names are CASE SENSITIVE! Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 13.
    Command: pwd • Tofind your current path use “pwd” Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 14.
    Command: cd • Tochange to a specific directory use “cd” Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 15.
    Command: cd • “~”is the location of your home directory Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 16.
    Command: cd • “..”is the location of the directory below current one Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 17.
    Command: ls • Tolist the files in the current directory use “ls” Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 18.
    Command: ls • lshas many options – -l long list (displays lots of info) – -t sort by modification time – -S sort by size – -h list file sizes in human readable format – -r reverse the order • “man ls” for more options • Options can be combined: “ls -ltr” Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 19.
    Command: ls -ltr •List files by time in reverse order with long listing Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 20.
    General Syntax: * •“*” can be used as a wildcard in unix/linux Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 21.
    Command: mkdir • Tocreate a new directory use “mkdir” Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 22.
    Command: rmdir • Toremove and empty directory use “rmdir” Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 23.
    Command: rmdir • Toremove and empty directory use “rmdir” Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 24.
    Creating files inUnix/Linux • Requires the use of an Editor • Various Editors: 1) nano / pico 2) vi 3) emacs Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 25.
    Displaying a file •Various ways to display a file in Unix – cat – less – head – tail Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 26.
    Command: cat • Dumpsan entire file to standard output • Good for displaying short, simple files Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 27.
    Command: less • “less”displays a file, allowing forward/backward movement within it – return scrolls forward one line, space one page – y scrolls back one line, b one page • use “/” to search for a string • Press q to quit Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 28.
    Command: head • “head”displays the top part of a file • By default it shows the first 10 lines • -n option allows you to change that • “head -n50 file.txt” displays the first 50 lines of file.txt Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 29.
    Command: head • Here’san example of using “head”: Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 30.
    Command: tail • Sameas head, but shows the last lines Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 31.
    File Commands • Copyinga file: cp • Move or rename a file: mv • Remove a file: rm Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 32.
    Command: cp • Tocopy a file use “cp” Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 33.
    Command: mv • Tomove a file to a different location use “mv” Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 34.
    Command: mv • mvcan also be used to rename a file Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 35.
    Command: rm • Toremove a file use “rm” Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 36.
    Command: rm • Toremove a file “recursively”: rm –r • Used to remove all files and directories • Be very careful, deletions are permanent in Unix/Linux Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 37.
    File permissions • Eachfile in Unix/Linux has an associated permission level • This allows the user to prevent others from reading/writing/executing their files or directories • Use “ls -l filename” to find the permission level of that file Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 38.
    Permission levels • “r”means “read only” permission • “w” means “write” permission • “x” means “execute” permission – In case of directory, “x” grants permission to list directory contents Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 39.
    File Permissions User (you)ChanderprabhuJain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 40.
    File Permissions Group Chanderprabhu JainCollege of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 41.
    File Permissions “The World” ChanderprabhuJain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 42.
    Command: chmod • Ifyou own the file, you can change it’s permissions with “chmod” – Syntax: chmod [user/group/others/all]+[permission] [file(s)] – Below we grant execute permission to all: Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 43.
    Running a program(a.k.a. a job) • Make sure the program has executable permissions • Use “./” to run the program Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 44.
    Running a program:an example • Running the sample perl script “hello_world.pl” Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 45.
    Ending a program •To end a program use “ctrl-c”. To try it: Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 46.
    Command: ps • Toview the processes that you’re running: Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 47.
    Command: top • Toview the CPU usage of all processes: Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 48.
    Command: kill • Toterminate a process use “kill” Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 49.
    Input/Output Redirection (“piping”) •Programs can output to other programs • Called “piping” • “program_a | program_b” – program_a’s output becomes program_b’s input • “program_a > file.txt” – program_a’s output is written to a file called “file.txt” • “program_a < input.txt” – program_a gets its input from a file called “input.txt” Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 50.
    A few examplesof piping Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 51.
    A few examplesof piping Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 52.
    Command: wc • Tocount the characters, words, and lines in a file use “wc” • The first column in the output is lines, the second is words, and the last is characters Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 53.
    A few examplesof piping Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 54.
    Command: grep • Tosearch files in a directory for a specific string use “grep” Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 55.
    What is aRegular Expression? • Pattern to match all or part of a line of text – Expressed in a formal, albeit weird, language – For example: ^.*:Mike matches lines that start (^) with any string (.*) and contain a colon followed by Mike • ^, . and * are called meta characters – They do not represent themselves, but have other special meaning. • :, M, i, k and e are normal characters – The do represent themselves 55 Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 56.
    Where are Regular ExpressionsUsed? • Special commands that “know” about them – Vi-Uses them in searching for a string: /regexp/ • Uses them in substitite command: s/regexp/replace/sed – Edit a file (like vi) as a filter in a pipeline … | sed editing-commands | … – grep , fgrep (fixed grep) , egrep (extended grep) • Print lines of a file that match a regexp – awk • Process input files looking for lines that match a regexp and processing those lines 56 Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 57.
    Regular expression • Aregular expression is a pattern of characters used for describing sets of strings • A pattern or sequence of characters – Upper and lower case – Digits – Space, underscore, etc • Metacharacters 57 Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 58.
    Basic Regular Expressions •Patterns that match a single character – All regular letters match themselves a b z T Q 0 1 9 – . (a single dot) matches any single character except newline. • In awk, dot can match newline also • (like ? in filename generation) – A set of characters that matches any single character from the set (just like filename generation) [aeiou] [a-z0-9] [A-Za-z-] [a-m] 58 Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 59.
    Metacharacter * Matches 0or more occurrences of the preceding char […] Matches any one of characters enclosed between the brackets. - dash indicates a range when inside sq bkts. [^ - negates what's inside brackets] 59 Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 60.
    Metacharacter (con’t) -backslash - escape character - just like before. – . means match a dot – This means is a meta character – means match Positional indicators: • ^ anchor to beginning of line • $ anchor to end of line 60 Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 61.
    • "Some people,when confronted with a Unix problem, think ‘I know, I’ll use sed.’ Now they have two problems.” - 61 Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 62.
    Regular expression examples •Peach • a*c – cxxx, acxxx, aaacxxxx • a.c – a+c, abc, match, a3c • [tT]he – The, the • Ch[^0-9] – Chapter, Chocolate • ^the – Start with the • Friends$ – End with Friends 62 Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 63.
    Regular expression examples •L..e • $[0-9]*.[0-9] • ^[0-9]file.dat • [^0-9]file.dat • MM-DD-YY or MM/DD/YY – [0-1][0-9][-/][0-3][0-9][-/][0-9][0-9] 63 Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 64.
    Named classes ofcharacters • [0-9] ---OR --- [[:digit:]] • [a-z] --- OR --- [[:lower:]] • [A-Z] --- OR --- [[:upper:]] • [a-zA-Z] --- OR --- [[:alpha:]] • [a-zA-Z0-9] ---OR --- [[:alnum:]] • egrep ‘^[[:lower:]]*$’ 64 Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 65.
    Extended Metacharacter (egrepand awk) Available in egrep and awk NOT in vi, sed, grep or fgrep ? matches zero or one occurrence of the preceding char + Matches 1 or more occurrences of the preceding char | Specifies that either the preceding or following regular expression can be matched ( ) Groups regular expressions 65 Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 66.
    Examples • “.*” matchesall characters between the quotations • ^$ matches blank lines • ^.*$ matches the entire line • Big( Computer)? • Compan(y|ies) # note: the | is a pipe symbol • SSN: [0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9] • grep ‘.H[123]’ ch0[12] – ch01: .H1 “Contents of Distribution Tape” – ch02: .H2 “A Quick Tour” 66 Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 67.
    Repetition Examples • Considera file named text containing: We're off to see the wizard. The wonderful wizard of oz. What a wonderful wizard he was. The end. • Try the following commands and explain the output: egrep 'f+' text grep 'ff*' text grep 'f{2}' text egrep 'z?' text 67 Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 68.
    Some More Examples •A price: $[0-9]*.[0-9][0-9] • A filename, at the start of a line, that starts with a digit ^[0-9]file.dat • A filename, anyplace in the line, that starts with a non-digit [^0-9]file.dat • A social security number [0-9]{3}-[0-9]{2}-[0-9]{4} • From 4 to 6 digits: [0-9]{4, 6} • A date - MM-DD-YY or MM/DD/YY: [0-1][0-9][-/][0-3][0-9][-/][0-9][0-9] • A line containing only upper case letters: ^[A-Z]*$ 68 Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 69.
    Replacement in sedand vi • Applies to substitute command: – s/regexp/replacement/ – Replacement string can contain the metacharacter ‘&’ which means the string that was matched • Try this STRING=“Start Again” echo $STRING | sed 's/Again/& &/' 69 Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 70.
    Process and Thread •Process – Execution context – Program code – Resources – Associated threads • Threads: units of execution • Process and threads are objects Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 71.
    Thread Scheduling Chanderprabhu JainCollege of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 72.
    Linux Architecture • Monolithickernel – Contains modular components, however • UNIX-like or UNIX-based operating system • Six primary subsystems: – Process management – Interprocess communication – Memory management – File system management • VFS: provides a single interface to multiple file systems – I/O management – Networking Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 73.
    Linux Kernel Architecture ChanderprabhuJain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 74.
    Process and ThreadOrganization Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 75.
    Process information Chanderprabhu JainCollege of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 76.
    System Calls • Interfacebetween user-level processes and hardware devices. – CPU, memory, disks etc. • Make programming easier: – Let kernel take care of hardware-specific issues. • Increase system security: – Let kernel check requested service via syscall. • Provide portability: – Maintain interface but change functional implementation. Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 77.
    POSIX APIs • API= Application Programmer Interface. – Function defn specifying how to obtain service. – By contrast, a system call is an explicit request to kernel made via a software interrupt. • Standard C library (libc) contains wrapper routines that make system calls. – e.g., malloc, free are libc routines that use the brk system call. • POSIX-compliant = having a standard set of APIs. • Non-UNIX systems can be POSIX-compliant if they offer the required set of APIs. Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 78.
    Linux System Calls(1) Invoked by executing int $0x80. – Programmed exception vector number 128. – CPU switches to kernel mode & executes a kernel function. • Calling process passes syscall number identifying system call in eax register (on Intel processors). • Syscall handler responsible for: – Saving registers on kernel mode stack. – Invoking syscall service routine. – Exiting by calling ret_from_sys_call(). Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 79.
    Linux System Calls(2) • System call dispatch table: – Associates syscall number with corresponding service routine. – Stored in sys_call_table array having up to NR_syscall entries (usually 256 maximum). – nth entry contains service routine address of syscall n. Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 80.
    Initializing System Calls •trap_init() called during kernel initialization sets up the IDT (interrupt descriptor table) entry corresponding to vector 128: – set_system_gate(0x80, &system_call); • A system gate descriptor is placed in the IDT, identifying address of system_call routine. – Does not disable maskable interrupts. – Sets the descriptor privilege level (DPL) to 3: • Allows User Mode processes to invoke exception handlers (i.e. syscall routines). Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
  • 81.
    The system_call() Function •Saves syscall number & CPU registers used by exception handler on the stack, except those automatically saved by control unit. • Checks for valid system call. • Invokes specific service routine associated with syscall number (contained in eax): – call *sys_call_table(0, %eax, 4) • Return code of system call is stored in eax. Chanderprabhu Jain College of Higher Studies & School of Law Plot No. OCF, Sector A-8, Narela, New Delhi – 110040 (Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)