Professional Documents
Culture Documents
Filter is a programm in unix . It takes its input from another program, performs some
operation on that input, and writes the result to the standard output. Thus the common
use of filters is to modify or restructure output.
head
This command list the beginning of a file to standard output. The default is 10 lines,
but a different number can be specified. The command has a number of options.
Syntax:
Options:
-c Prints the first N bytes of file; with leading -, prints all but the last N bytes of the
file.
-n Prints first N lines; with leading - print all but the last N lines of each file.
Example: To display the first 10 lines of the file myfile.txt. $head myfile.txt
tail
List the (tail) end of a file to stdout. The default is 10 lines, but this can be changed
with the -n option. Commonly used to keep track of changes to a system log-file,
using the -f option, which outputs lines appended to the file.
Syntax:
Example:
$tail myfile
more
more command allows to view text files or other output in a scrollable manner. When
can command is used to view a very long file, all the output scrolls off the top of your
screen and only the last page can be viewed. more command solves this problem by
allowing the output of cat command one screenful of data at a time.
Syntax:
Options:
-num This option specifies an integer which is the screen size (in lines).
-d more will prompt the user with the message "[Press space to continue, 'q' to quit.]"
and will display "[Press 'h' for instructions.]" instead of ringing the bell when an
illegal key is pressed.
-l more usually treats ^L (form feed) as a special character, and will pause after any
line that contains a form feed. The -l option will prevent this behavior.
-p Do not scroll. Instead, clear the whole screen and then display the text.
tr
tr command automatically translates or substitute characters.
Syntax:
Translate, squeeze, and/or delete characters from standard input, writing to standard
output.
Options:
$ echo "Hello"|tr -c e a
Output : aeaaaa
In the above example , except the character “e” other characters are replaced by a
sort
sort command reorders the lines of a file in ascending or descending order.
Syntax:
Options:
-n sort numerically
Examples:
Below examples will help you to understand sort used with different options:
Example 1:
To sort on the 2nd field of file named “list” we have to use the below command:
Output:
19
20
49
00
If we sort it as below:
$sort list
Output is :
19
20
200
49
$sort –n list
Output:
19
20
49
200
Output :
200
49
20
19
Syntax:
$sort –u filename
Unix
Unix
Linux
Linux
Solaris
Axis
Axis
$sort –u list
Output:
Unix
Linux
Solaris
Axis
uniq
uniq command is used to suppress the duplicate lines from a file. It discards all the
successive identical lines except one from the input and writes the output.
Syntax:
Options:
The default behavior of the uniq command is to suppress the duplicate line. Note that,
you have to pass sorted input to the uniq, as it compares only successive lines.
If the lines in the file are not in sorted order, then use the sort command and then pipe
the output to the uniq command.
Count of lines:
The -c option is used to find how many times each line occurs in the file. It prefixes
each line with the count.
You can print only the lines that occur more than once in a file using the -d option.
The -D option prints all the duplicate lines.
cut
This command is used for text processing. You can use this command to extract
portion of text from a file by selecting columns.
Syntax:
The following example displays 2nd character from each line of a file test.txt.
Range of characters can also be extracted from a file by specifying start and end
position delimited with -.
The following example extracts first 3 characters of each line from a file called test.txt
Either start position or end position can be passed to cut command with -c option.
Following example extracts from 3rd character to end of each line from test.txt file.
The option -d specifies what delimiter that is used in the input file.
The following example displays only first field of each lines from /etc/passwd file
using the field delimiter: (colon). In this case, the 1st field is the username.
paste
This is the command for merging together different files into a single, multi-column
file. In combination with cut, useful for creating system log files.
Syntax:
join
This utility allows merging two files in a meaningful fashion, which essentially
creates a simple version of a relational database.
The command join operates on exactly two files, but pastes together only those lines
with a common tagged field (usually a numerical label), and writes the result to
standard output.
The files to be joined should be sorted according to the tagged field for the matchups
to work properly.
Example:
100 Shoes
200 Laces
300 Socks
$cat file2
100 $40.0
200 $1.00
300 $2.00
Pipe
In unix , you can connect two commands together so that the output from one program
becomes the input of the next program. Two or more commands connected in this
way form a pipe. In shell the symbol '|’ is used to represent pipe.
Purpose of Pipes :
Using pipe you can construct powerful unix command lines by combining basic unix
commands. UNIX commands are powerful; however by using pipe you can combine
them together, to accomplish complex tasks with ease.
Through the standard output of one command (the command to the left of the pipe)
gets sent as standard input to another command (the command to the right of the
pipe). Pipe functions in a similar manner like the output redirection in UNIX (using >
symbol to redirect the standard output of a command to a file. However, the pipe is
different because it is used to pass the output of a command to another command, not
a file.
Example:
$ cat apple.txt | wc
3 4 21
In this example, the contents of the file apple.txt are sent through pipe to wc (word
count) command. The wc command then does its job and counts the lines, words, and
characters in the file.
You can combine many commands with pipes on a single command line. Here's an
example where the characters, words, and lines of the file apple.txt is sent to wc and
then the output of wc mailed to nobody@december.com with the subject line "The
count."
awk
awk is a scripting language which is used for processing or analyzing text files.
awk is used for grouping of data based on either a column or field, or on a set of
columns.
It derives its name from the first letter of the last name of its three authors namely
Alfred V. Aho, Peter J.Weinberger and Brian W. Kernighan.
awk can be used for reporting data in a useful manner. It searches one or more files to
see if they contain lines that match specified patterns and then perform associated
actions. awk is an advanced filter.
Simple awk Filtering
Syntax of awk:
By default, awk prints all lines of a file, so to print every line of above created file ,
use below command:
~$ awk '{print}' awk_file
Name,Marks,Max_Marks
Peter,200,1000
Sam,500,1000
Greg,1000
Abharam,800,1000
Henry,600,1000
Peter,400,1000
BEGIN and END blocks are helpfull in displaying information before and after
executing actual awk script.
BEGIN block is evaluated before awk starts processing the actual awk script; it’s an
excellent place to initialize the FS (field separator) variable, print a heading, or
initialize other global variables.
• Declaring variables.
• Initialization variables for doing increment/decrements operations in main
AWK code.
• Printing Headings/info before actual AWK code output.
END block is evaluated after all the lines in the awk script have been processed.
Typically, the END block is used to perform final calculations or print summaries that
should appear at the end of the output stream.
awk tool is mainly used for reporting some data in useful manner. Without these
BEGIN and END
blocks the output will be meaningless.
Consider db.txt which contains below data:
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122234 38 37
This is a block of code which is executed before executing actual awk script.
BEGIN block Syntax
awk ‘BEGIN{awk initializing code}{actual AWK code}’ filename.txt
Example: Print a meaning full info before actual AWK output.
~$ awk ‘BEGIN{print “########################\nThis is the output of
filtered
data\n########################”}{print $0}’ db.txt
Output:
##########################
This is the output of filtered data
##########################
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122234 38 37
Edwin 253734 87 97 95
Dayan 24155 30 47
This is the block which is executed after executing all the awk code.
Example:
Output:
Jones 21 78 84 77
Gondrol 23 56 58 45
RinRao 25 21 38 37
Edwin 25 87 97 95
Dayan 24 55 30 47
#########################
Completed printing filtered data
#########################
Example:
Output:
#########################
This is the output of filtered data
#########################
Jones 21 78 84 77
Gondrol 23 56 58 45
RinRao 25 21 38 37
Edwin 25 87 97 95
Dayan 24 55 30 47
########################
Completed printing filtered data
awk is supplied with good number of built-in variables which comes in handy when
working with data files. We will see usages of awk built-in variables with one or two
examples . These variable are used to format the output of an awk command.
In order to make it simple we can divide above inbuilt variables in to groups on basis
of their operations.
Group1: FS(input field separator), OFS(Output Field Separator)
Group2: RS(Row separator) and ORS(Output Record Separator)
Group3: NR, NF and FNR
Group4: FILENAME variable
This variable is useful in storing the input field separator. By default AWK can
understand only spaces, tabs as input and output separators. But if your file contains
some other character as separator other than these mention one’s, awk cannot
understand them.
For example UNIX password file which contain ‘:’ as a separator. So in order to
mention the input filed separator we use this inbuilt variable. We will see what issue
we face if we don’t mention the field separator for our db.txt.
Output:
John,29,MS,IBM,M,Married
Barbi,45,MD,JHH,F,Single
Mitch,33,BS,BofA,M,Single
Tim,39,Phd,DELL,M,Married
Lisa,22,BS,SmartDrive,F,Married
OFS (Output Field Separator)
This variable is useful for defining the output field separator for the expected output
data.
Example:
Display only 1st and 4th column and with $ as field separator for the output .
~$ awk ‘BEGIN{FS=”,”;OFS=” $ “}{print $1,$4}’ db.txt
Output:
John $ IBM
Barbi $ JHH
Mitch $ BofA
Tim $ DELL
Lisa $ SmartDrive
Note: Space is give before and after $ in OFS variable to show better output.
RS (Row separator)
Row Separator is helpful in defining separator between rows in a file. By default awk
takes row separator as new line. We can change this by using RS built-in variable.
Example:
Convert a sentence to a word per line. We can use RS variable for doing it.
~$ echo “This is how it works” | awk ‘BEGIN{RS=” ”}{print $0}’
Output:
This
is
how
it
Works
Example:
Print all the company names in single line which are in 4th column.
~$ awk -F’,’ ‘BEGIN{ORS=” “}{print $4}’ db.txt
Output:
IBM JHH BofA DELL SmartDrive
NF
This variable keeps information about total fields in a given row. The final
value of a row can be represented with $NF.
Output:
5
5
4
5
4
NR
This variable keeps the value of present line number. This will come handy when you
want to print line numbers in a file.
Example:
Print line number for each line in a given file.
~$ awk ‘{print NR, $0}’ abc.txt
Output:
1 Jones 2143 78 84 77
2 Gondrol 2321 56 58 45
3 RinRao 2122234 38 37
4 Edwin 253734 87 97 95
5 Dayan 24155 30 47
This can be treated as cat command -n option for displaying line number for a file
FNR
This variable keeps count of number of lines present in a given file/data. This will
come handy when
you want to print no of line present in a given file. This command is equivalent to wc
-l command.
Example:
Print total number of lines in a given file.
~$ awk ‘END{print FNR}’ abc.txt
Output:
5
FILENAME
This variable contain file awk command is processing.
Example:
Print filename for each line in a given file.
~$ awk ‘{print FILENAME, NR, $0}’ abc.txt
Output:
abc.txt 1 Jones 2143 78 84 77
abc.txt 2 Gondrol 2321 56 58 45
abc.txt 3 RinRao 2122234 38 37
abc.txt 4 Edwin 253734 87 97 95
abc.txt 5 Dayan 24155 30 47
Arithmetic Functions
Nine of the built-in functions can be classified as arithmetic functions. Most of them
take a numeric argument and return a numeric value. Below table summarizes these
arithmetic functions with some Examples.
Examples:
~$ awk 'BEGIN{
print sqrt(16);
print sqrt(0);
print sqrt(-12);
}'
Output:
4
0
nan
Here nan stands for not a valid number.
String Functions
The built-in string functions are much more significant and interesting than the
numeric functions. Because awk is essentially designed as a string-processing
language, a lot of its power derives from these functions. Below table lists the string
functions found in awk. awk's Built-In String Functions
Filters and Regular Expression
grep
grep command allows you to search one file or multiple files for lines that contain a
pattern.
Syntax:
grep pattern [file...]
For example, let us copy the demo_file to demo_file1and use the grep on both the
files to search the pattern this.
The output will include the file name in front of the line that matched the specific
pattern as shown below.
When the Linux shell sees the meta character, it does the expansion and gives all the
files as input to grep.
Case insensitive search
We can use grep to search for the given string/pattern case insensitively. So it matches
all the words such as “the”, “THE” and “The” case insensitively as shown below.
$ grep -r "ramesh" *
Invert match
If you want to display the lines which does not matches the given string/pattern, use
the option -v as shown below. This example will display all the lines that did not
match the word “Two”.
Displaying the lines which does not matches the entire given pattern.
Syntax:
grep -v -e pattern -e pattern
Apple
Banana
Cauliflower
Grapes
Orange
Displaying only the file names which matches the given pattern
The -l option is used to display only the file names which matched the given pattern.
When you give multiple files to the grep as input, it displays the names of file which
contains the text that matches the pattern, will be very handy when you try to find
some notes in your whole directory structure.
Syntax:
grep -n pattern filename
Example:
grep -n "this" demo_file
2: this line is the 1st lower case line in this file.
6: Two lines above this line is empty.
sed
sed is a stream editor used to perform basic text transformations on an input stream (a
file, or input from a pipeline).
Working methodology
sed works by making only one pass over the input(s) s called as one execution cycle.
Cycle continues till end of file/input is reached.
Syntax:
sed -n 'ADDRESS'p filename
sed -n '/pattern/p' filename
Examples:
Let us assume the demo_file has the following content
The process is
• It reads the first line and places in its pattern buffer
• checks whether supplied command is true for this line , if true, deletes pattern space
buffer and starts next cycle and reads the next line.
• If supplied command is not true, it prints the content of the pattern space buffer.
To delete the 3rd line and print other lines from the file demo_file
Syntax:
$sed 'ADDRESSs/REGEXP/REPLACEMENT/FLAGS' filename
$sed 'PATTERNs/REGEXP/REPLACEMENT/FLAGS' filename
1. s is substitute command
2. / is a delimiter
3. REGEXP is regular expression to match
4. REPLACEMENT is a value to replace
Syntax:
$sed -e 'command' e 'command' filename
To Delete the first,last and all the blank lines from input
A few utilities like awk and egrep use the extended expression. Most use the "regular"
regular expression. From now on, if I talk about a "regular expression," it describes a
feature in both types.
The Anchor Characters: ^ and $ Anchores are used when we want to search for a
pattern that is at one end or the other, of a line. The character "^" is the starting
anchor, and the character "$" is the end anchor.Following list provides a summary:
Pattern Matches
^A "A" at the beginning of a line
A$ "A" at the end of a line
A^ "A^" anywhere on a line
$A "$A" anywhere on a line
^^ "^" at the beginning of a line
$$ "$" at the end of a line
• A character set matches only a single character. In case of the above example,
gr[ae]y does not match graay, graey or any such thing.
• The order of the characters inside a character set does not matter. The results
are identical.
• Some characters have a special meaning in regular expressions. If we want to
search for such a character, we have to escape it with a backslash.
Examples:
Expression Matches
Go*gle Gogle,Google,Gooogle, and so on.
"[0-9]*" zero or more numbers.
For example , A user name is a string beginning with a letter followed by at least two,
but not more than seven letters or numbers followed by the end of the string. Then the
regular expression is
^[A-z][A-z0-9]{2,7}
▪ A repetition modifier must be combined with other patterns; the modifier has no
meaning by itself.
For example , modifiers like "*" and "\{1,5\}" only act as modifiers if they follow a
character set. If they were at the beginning of a pattern, they would not be a modifier.
Character classes
Searches for all lines matching '--test--' using -e option . Without -e, grep would
attempt to parse '--test--' as a list of options
grep -e '--test--' filename
Test Sequence
grep OR Operator
Suppose the file “employee” has the following data:
To find the records of those who are either from Tech or Sales dept.
We can use the following syntaxes :
• grep -E option.
Syntax : grep -E 'word1.*word2 ' filename
grep -E 'word1.*word2|'word2.*word1' filename
Environment Variables:
Values of some of these variables are set by the system, some can be set by the user,
some of them by the shell, or any program that loads another program.
When a user logs in to the system, the shell undergoes a twostep process
called initialization to set up the environment. This process involves the shell reading
two files −/etc/profile and .profile in the home directory of the user.
The file /etc/profile is maintained by the system administrator of the Unix machine
and contains shell initialization information required by all users on a system.
The file .profile is under the control of the user. User can add as much shell
customization information as he/she wants to this file. The minimum set of
information that a user need to configure includes −
A global shell environment pointer called environ is maintained by UNIX kernel and
it can be used by a process to access its own table
The shell does not directly use this table, but it creates a child process and calls exec()
system call to execute a command program that uses the table inherited from the shell
parent. Children inherit the entire execution environment from the parent.
Some examples of the environment variables are the USER, LOGNAME, HOME,
PATH, PS1, PS2, TERM MAIL, etc.
The PS2 Variable: prompt string for continued command line (default ‘> ‘).
$IFS: String of characters which are used as word separators in command line ( space,
tab, newline chars).
Some of above variables like HOME and LOGNAME are set automatically each time
the user logs in. The others, however, have to be set. The .profile is used for this
purpose as it is executed as soon the user logs in. A sample .profile file would look
like:
PATH=.:/bin:/usr/bin
export HOME PATH PS1 MAIL
Filters
Filter is a programm in unix . It takes its input from another program, performs some
operation on that input, and writes the result to the standard output. Thus the common
use of filters is to modify or restructure output.
head
This command list the beginning of a file to standard output. The default is 10 lines,
but a different number can be specified. The command has a number of options.
Syntax:
Options:
-c Prints the first N bytes of file; with leading -, prints all but the last N bytes of the
file.
-n Prints first N lines; with leading - print all but the last N lines of each file.
Example: To display the first 10 lines of the file myfile.txt. $head myfile.txt
tail
List the (tail) end of a file to stdout. The default is 10 lines, but this can be changed
with the -n option. Commonly used to keep track of changes to a system log-file,
using the -f option, which outputs lines appended to the file.
Syntax:
Example:
$tail myfile
more
more command allows to view text files or other output in a scrollable manner. When
can command is used to view a very long file, all the output scrolls off the top of your
screen and only the last page can be viewed. more command solves this problem by
allowing the output of cat command one screenful of data at a time.
Syntax:
Options:
-num This option specifies an integer which is the screen size (in lines).
-d more will prompt the user with the message "[Press space to continue, 'q' to quit.]"
and will display "[Press 'h' for instructions.]" instead of ringing the bell when an
illegal key is pressed.
-l more usually treats ^L (form feed) as a special character, and will pause after any
line that contains a form feed. The -l option will prevent this behavior.
-p Do not scroll. Instead, clear the whole screen and then display the text.
tr
tr command automatically translates or substitute characters.
Syntax:
Translate, squeeze, and/or delete characters from standard input, writing to standard
output.
Options:
$ echo "Hello"|tr -c e a
Output : aeaaaa
In the above example , except the character “e” other characters are replaced by a
sort
sort command reorders the lines of a file in ascending or descending order.
Syntax:
Options:
-n sort numerically
Examples:
Below examples will help you to understand sort used with different options:
Example 1:
To sort on the 2nd field of file named “list” we have to use the below command:
Output:
19
20
49
00
If we sort it as below:
$sort list
Output is :
19
20
200
49
$sort –n list
Output:
19
20
49
200
Output :
200
49
20
19
Syntax:
$sort –u filename
Unix
Unix
Linux
Linux
Solaris
Axis
Axis
$sort –u list
Output:
Unix
Linux
Solaris
Axis
uniq
uniq command is used to suppress the duplicate lines from a file. It discards all the
successive identical lines except one from the input and writes the output.
Syntax:
Options:
The default behavior of the uniq command is to suppress the duplicate line. Note that,
you have to pass sorted input to the uniq, as it compares only successive lines.
If the lines in the file are not in sorted order, then use the sort command and then pipe
the output to the uniq command.
Count of lines:
The -c option is used to find how many times each line occurs in the file. It prefixes
each line with the count.
You can print only the lines that occur more than once in a file using the -d option.
The -D option prints all the duplicate lines.
the -f option is used to skip the first N columns in comparison. Here the fields are
delimited by the space character.
cut
This command is used for text processing. You can use this command to extract
portion of text from a file by selecting columns.
Syntax:
The following example displays 2nd character from each line of a file test.txt.
Range of characters can also be extracted from a file by specifying start and end
position delimited with -.
The following example extracts first 3 characters of each line from a file called test.txt
Either start position or end position can be passed to cut command with -c option.
Following example extracts from 3rd character to end of each line from test.txt file.
The option -d specifies what delimiter that is used in the input file.
The following example displays only first field of each lines from /etc/passwd file
using the field delimiter: (colon). In this case, the 1st field is the username.
paste
This is the command for merging together different files into a single, multi-column
file. In combination with cut, useful for creating system log files.
Syntax:
join
This utility allows merging two files in a meaningful fashion, which essentially
creates a simple version of a relational database.
The command join operates on exactly two files, but pastes together only those lines
with a common tagged field (usually a numerical label), and writes the result to
standard output.
The files to be joined should be sorted according to the tagged field for the matchups
to work properly.
Example:
100 Shoes
200 Laces
300 Socks
$cat file2
100 $40.0
200 $1.00
300 $2.00
Pipe
In unix , you can connect two commands together so that the output from one program
becomes the input of the next program. Two or more commands connected in this
way form a pipe. In shell the symbol '|’ is used to represent pipe.
Purpose of Pipes :
Using pipe you can construct powerful unix command lines by combining basic unix
commands. UNIX commands are powerful; however by using pipe you can combine
them together, to accomplish complex tasks with ease.
Through the standard output of one command (the command to the left of the pipe)
gets sent as standard input to another command (the command to the right of the
pipe). Pipe functions in a similar manner like the output redirection in UNIX (using >
symbol to redirect the standard output of a command to a file. However, the pipe is
different because it is used to pass the output of a command to another command, not
a file.
Example:
$ cat apple.txt | wc
3 4 21
In this example, the contents of the file apple.txt are sent through pipe to wc (word
count) command. The wc command then does its job and counts the lines, words, and
characters in the file.
You can combine many commands with pipes on a single command line. Here's an
example where the characters, words, and lines of the file apple.txt is sent to wc and
then the output of wc mailed to nobody@december.com with the subject line "The
count."
awk is used for grouping of data based on either a column or field, or on a set of
columns.
It derives its name from the first letter of the last name of its three authors namely
Alfred V. Aho, Peter J.Weinberger and Brian W. Kernighan.
awk can be used for reporting data in a useful manner. It searches one or more files to
see if they contain lines that match specified patterns and then perform associated
actions. awk is an advanced filter.
Simple awk Filtering
Syntax of awk:
Name,Marks,Max_Marks
Peter,200,1000
Sam,500,1000
Greg,1000
Abharam,800,1000
Henry,600,1000
Peter,400,1000
BEGIN and END blocks are helpfull in displaying information before and after
executing actual awk script.
BEGIN block is evaluated before awk starts processing the actual awk script; it’s an
excellent place to initialize the FS (field separator) variable, print a heading, or
initialize other global variables.
• Declaring variables.
• Initialization variables for doing increment/decrements operations in main
AWK code.
• Printing Headings/info before actual AWK code output.
END block is evaluated after all the lines in the awk script have been processed.
Typically, the END block is used to perform final calculations or print summaries that
should appear at the end of the output stream.
awk tool is mainly used for reporting some data in useful manner. Without these
BEGIN and END
blocks the output will be meaningless.
Consider db.txt which contains below data:
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122234 38 37
This is a block of code which is executed before executing actual awk script.
BEGIN block Syntax
awk ‘BEGIN{awk initializing code}{actual AWK code}’ filename.txt
Example: Print a meaning full info before actual AWK output.
~$ awk ‘BEGIN{print “########################\nThis is the output of
filtered
data\n########################”}{print $0}’ db.txt
Output:
##########################
This is the output of filtered data
##########################
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122234 38 37
Edwin 253734 87 97 95
Dayan 24155 30 47
This is the block which is executed after executing all the awk code.
Example:
Print some meaning full info after processing awk code.
~$ awk ‘{print $0} END {print “#########################\n Completed
printing
filtered data\n########################”}’ db.txt
Output:
Jones 21 78 84 77
Gondrol 23 56 58 45
RinRao 25 21 38 37
Edwin 25 87 97 95
Dayan 24 55 30 47
#########################
Completed printing filtered data
#########################
Example:
Output:
#########################
This is the output of filtered data
#########################
Jones 21 78 84 77
Gondrol 23 56 58 45
RinRao 25 21 38 37
Edwin 25 87 97 95
Dayan 24 55 30 47
########################
Completed printing filtered data
In order to make it simple we can divide above inbuilt variables in to groups on basis
of their operations.
Group1: FS(input field separator), OFS(Output Field Separator)
Group2: RS(Row separator) and ORS(Output Record Separator)
Group3: NR, NF and FNR
Group4: FILENAME variable
This variable is useful in storing the input field separator. By default AWK can
understand only spaces, tabs as input and output separators. But if your file contains
some other character as separator other than these mention one’s, awk cannot
understand them.
For example UNIX password file which contain ‘:’ as a separator. So in order to
mention the input filed separator we use this inbuilt variable. We will see what issue
we face if we don’t mention the field separator for our db.txt.
Example: without using FS
Output:
John,29,MS,IBM,M,Married
Barbi,45,MD,JHH,F,Single
Mitch,33,BS,BofA,M,Single
Tim,39,Phd,DELL,M,Married
Lisa,22,BS,SmartDrive,F,Married
Example:
Display only 1st and 4th column and with $ as field separator for the output .
~$ awk ‘BEGIN{FS=”,”;OFS=” $ “}{print $1,$4}’ db.txt
Output:
John $ IBM
Barbi $ JHH
Mitch $ BofA
Tim $ DELL
Lisa $ SmartDrive
Note: Space is give before and after $ in OFS variable to show better output.
RS (Row separator)
Row Separator is helpful in defining separator between rows in a file. By default awk
takes row separator as new line. We can change this by using RS built-in variable.
Example:
Convert a sentence to a word per line. We can use RS variable for doing it.
~$ echo “This is how it works” | awk ‘BEGIN{RS=” ”}{print $0}’
Output:
This
is
how
it
Works
Example:
Print all the company names in single line which are in 4th column.
~$ awk -F’,’ ‘BEGIN{ORS=” “}{print $4}’ db.txt
Output:
IBM JHH BofA DELL SmartDrive
NF
This variable keeps information about total fields in a given row. The final
value of a row can be represented with $NF.
Output:
5
5
4
5
4
NR
This variable keeps the value of present line number. This will come handy when you
want to print line numbers in a file.
Example:
Print line number for each line in a given file.
~$ awk ‘{print NR, $0}’ abc.txt
Output:
1 Jones 2143 78 84 77
2 Gondrol 2321 56 58 45
3 RinRao 2122234 38 37
4 Edwin 253734 87 97 95
5 Dayan 24155 30 47
This can be treated as cat command -n option for displaying line number for a file
FNR
This variable keeps count of number of lines present in a given file/data. This will
come handy when
you want to print no of line present in a given file. This command is equivalent to wc
-l command.
Example:
Print total number of lines in a given file.
~$ awk ‘END{print FNR}’ abc.txt
Output:
5
FILENAME
This variable contain file awk command is processing.
Example:
Print filename for each line in a given file.
~$ awk ‘{print FILENAME, NR, $0}’ abc.txt
Output:
abc.txt 1 Jones 2143 78 84 77
abc.txt 2 Gondrol 2321 56 58 45
abc.txt 3 RinRao 2122234 38 37
abc.txt 4 Edwin 253734 87 97 95
abc.txt 5 Dayan 24155 30 47
Arithmetic Functions
Nine of the built-in functions can be classified as arithmetic functions. Most of them
take a numeric argument and return a numeric value. Below table summarizes these
arithmetic functions with some Examples.
Examples:
~$ awk 'BEGIN{
print sqrt(16);
print sqrt(0);
print sqrt(-12);
}'
Output:
4
0
nan
Here nan stands for not a valid number.
String Functions
The built-in string functions are much more significant and interesting than the
numeric functions. Because awk is essentially designed as a string-processing
language, a lot of its power derives from these functions. Below table lists the string
functions found in awk. awk's Built-In String Functions
grep's exit status is 0 if matches were found, 1 if no matches were found, and 2 if
errors occurred.
grep search the target file(s) for occurrences of pattern, where pattern may be literal
text or a Regular Expression.
Syntax:
grep pattern [file...]
For example, let us copy the demo_file to demo_file1and use the grep on both the
files to search the pattern this.
The output will include the file name in front of the line that matched the specific
pattern as shown below.
When the Linux shell sees the meta character, it does the expansion and gives all the
files as input to grep.
$ grep -r "ramesh" *
Invert match
If you want to display the lines which does not matches the given string/pattern, use
the option -v as shown below. This example will display all the lines that did not
match the word “Two”.
Displaying the lines which does not matches the entire given pattern.
Syntax:
grep -v -e pattern -e pattern
Banana
Cauliflower
Grapes
Orange
Displaying only the file names which matches the given pattern
The -l option is used to display only the file names which matched the given pattern.
When you give multiple files to the grep as input, it displays the names of file which
contains the text that matches the pattern, will be very handy when you try to find
some notes in your whole directory structure.
Syntax:
grep -n pattern filename
Example:
grep -n "this" demo_file
2: this line is the 1st lower case line in this file.
6: Two lines above this line is empty.
sed
sed is a stream editor used to perform basic text transformations on an input stream (a
file, or input from a pipeline).
Working methodology
sed works by making only one pass over the input(s) s called as one execution cycle.
Cycle continues till end of file/input is reached.
Syntax:
sed -n 'ADDRESS'p filename
sed -n '/pattern/p' filename
Examples:
Let us assume the demo_file has the following content
The process is
• It reads the first line and places in its pattern buffer
• checks whether supplied command is true for this line , if true, deletes pattern space
buffer and starts next cycle and reads the next line.
• If supplied command is not true, it prints the content of the pattern space buffer.
To delete the 3rd line and print other lines from the file demo_file
Syntax:
$sed 'ADDRESSs/REGEXP/REPLACEMENT/FLAGS' filename
$sed 'PATTERNs/REGEXP/REPLACEMENT/FLAGS' filename
5. s is substitute command
6. / is a delimiter
7. REGEXP is regular expression to match
8. REPLACEMENT is a value to replace
Syntax:
$sed -e 'command' e 'command' filename
To Delete the first,last and all the blank lines from input
A few utilities like awk and egrep use the extended expression. Most use the "regular"
regular expression. From now on, if I talk about a "regular expression," it describes a
feature in both types.
The Anchor Characters: ^ and $ Anchores are used when we want to search for a
pattern that is at one end or the other, of a line. The character "^" is the starting
anchor, and the character "$" is the end anchor.Following list provides a summary:
Pattern Matches
^A "A" at the beginning of a line
A$ "A" at the end of a line
A^ "A^" anywhere on a line
$A "$A" anywhere on a line
^^ "^" at the beginning of a line
$$ "$" at the end of a line
• A character set matches only a single character. In case of the above example,
gr[ae]y does not match graay, graey or any such thing.
• The order of the characters inside a character set does not matter. The results
are identical.
• Some characters have a special meaning in regular expressions. If we want to
search for such a character, we have to escape it with a backslash.
Examples:
Expression Matches
Go*gle Gogle,Google,Gooogle, and so on.
"[0-9]*" zero or more numbers.
For example , A user name is a string beginning with a letter followed by at least two,
but not more than seven letters or numbers followed by the end of the string. Then the
regular expression is
^[A-z][A-z0-9]{2,7}
▪ A repetition modifier must be combined with other patterns; the modifier has no
meaning by itself.
For example , modifiers like "*" and "\{1,5\}" only act as modifiers if they follow a
character set. If they were at the beginning of a pattern, they would not be a modifier.
Character classes
Searches for all lines matching '--test--' using -e option . Without -e, grep would
attempt to parse '--test--' as a list of options
grep -e '--test--' filename
Test Sequence
grep OR Operator
Suppose the file “employee” has the following data:
To find the records of those who are either from Tech or Sales dept.
We can use the following syntaxes :
• grep -E option.
Syntax : grep -E 'word1.*word2 ' filename
grep -E 'word1.*word2|'word2.*word1' filename
Environment Variables:
Values of some of these variables are set by the system, some can be set by the user,
some of them by the shell, or any program that loads another program.
When a user logs in to the system, the shell undergoes a twostep process
called initialization to set up the environment. This process involves the shell reading
two files −/etc/profile and .profile in the home directory of the user.
The file /etc/profile is maintained by the system administrator of the Unix machine
and contains shell initialization information required by all users on a system.
The file .profile is under the control of the user. User can add as much shell
customization information as he/she wants to this file. The minimum set of
information that a user need to configure includes −
A global shell environment pointer called environ is maintained by UNIX kernel and
it can be used by a process to access its own table
The shell does not directly use this table, but it creates a child process and calls exec()
system call to execute a command program that uses the table inherited from the shell
parent. Children inherit the entire execution environment from the parent.
Some examples of the environment variables are the USER, LOGNAME, HOME,
PATH, PS1, PS2, TERM MAIL, etc.
The PS2 Variable: prompt string for continued command line (default ‘> ‘).
$IFS: String of characters which are used as word separators in command line ( space,
tab, newline chars).
Some of above variables like HOME and LOGNAME are set automatically each time
the user logs in. The others, however, have to be set. The .profile is used for this
purpose as it is executed as soon the user logs in. A sample .profile file would look
like:
PATH=.:/bin:/usr/bin
export HOME PATH PS1 MAIL
Hello!
You can save this file in your system to use it. This file can be used for practice.
Expected Output : 6
9.Count how many users are currently logged into the system
Expected Output :
You can save this file in your system to use it. This file can be used for practice.
Expected Output : 3
Expected Output :
Hello!
Happy Learning!
13.Replace the word this by that only for the first occurrence of it in a line.
Expected Output :
Hello!
You can save that file in your system to use it. This file can be used for practice.
Happy Learning!
Expected Output :
Hello!
Expected Output :
You can save this file in your system to use it. This file can be used for practice.
Happy Learning!
awk Scripting :
17. Create a file with name student.txt with the following contents in it
RollNo|Name|Marks1|Marks2|Marks3
123|Raghu|80|90|60
342|Maya|45|78|82
561|Gita|56|71|89
480|Mohan|71|90|89
a. Write an awk command to print the name and roll number of the students.
Expected Output:
Name RollNo
Raghu 123
Maya 342
Gita 561
Mohan 480
b . Write the awk command to calculate the total marks of each student and display
the total marks along with the name of the student.
Expected Output:
Raghu-230
Maya-205
Gita-216
Mohan-250
Shell Scripting
18. Write a shell script to print the sum of the 1st 10 natural numbers.
Expected Output: 55
19. Write a shell script to print the odd numbers from a given list of numbers. The list
of numbers will be given as command line argument.
Example :
Expected Output :
45
20. Create a file studentScores.txt and append the following content to it.
RollNo|Name|Marks
123|Raghu|80
342|Maya|45
561|Gita|56
480|Mohan|71
write a shell script named scores.sh to find the name of the student who scored highest
marks. Incorporate validations for the following :
a. Whether correct number of command line arguments are passed or not.
Expected Output :
1. https://www.tutorialspoint.com/unix_terminal_online.php
2. http://www.compileonline.com/execute_bash_online.php
3. https://bellard.org/jslinux/vm.html?url=https://bellard.org/jslinux/buildroot-
x86.cfg
A process require certain resources to accomplish its intended task, they are
>> CPU time
>> System memory
>> Disk Files
>> I/O devices.
Process state:- The state may be new, ready, running, waiting, halted, and so on.
Program counter:- The counter indicates the address of the next instruction to be
executed for this process.
CPU registers:- The registers vary in number and type, depending on the computer
architecture. They include accumulators, index registers, stack pointers, and general-
purpose registers, plus any condition-code information.
CPU-scheduling information:- This information includes a process priority, pointers
to scheduling queues, and any other scheduling parameters.
Memory-management information:- This information may include such
information as the value of the base and limit registers, the page tables, or the segment
tables, depending on the memory system used by the OS.
Accounting information:- This information includes the amount of CPU and real
time used, time limits, account numbers, job or process numbers, and so on.
I/O status information:- This information includes the list of I/O devices allocated to
the process, a list of open files, and so on.
Process state:
A process may present in one of the following state.
New:- The process is being created or just created.
Ready:- The process is waiting to be assigned/allocated to a processor time.
Running:- Instructions of the process are being executed by processor.
Waiting:- The process is waiting for some other event/process to execute.
Terminated:- The process has finished its execution.
ps command:
The ps (i.e., process status) command is used to provide information about the
currently running processes in the system. When ps is used without any option four
columns of information labeled PID,TTY, TIME and CMD for at least two processes,
the shell and ps will be visible.
PID :- The process are identified by a 5 digit number known as PID (Process
Identification Number).
TTY :- Is the name of the console or terminal that the user logged into (Stands for
terminal type now
but originally stood for teletype).
TIME :- Is the amount of CPU time in minutes and seconds that the process has been
running.
CMD :- is the name of the command that launched the process.
Types of process
Parent and Child Process:
A process can initiate a sub process, which is a called a child process, the initiating
process is referred to as its parent.The child processes, in turn create other child
processes forming a tree of processes
( which can be displayed using ps command with –forest option)
Orphan Process:
When a child process is killed, parent process gets the notification via a signal. Parent
then, can continue other task. However if the parent process is killed before, its child,
is called an orphan process.
Zombie Process:
When a process finished its execution and exit status not received by the parent ( or
parent did not read the exit status till now), the process state becomes zombie.
The process is dead (not to be scheduled for further execution) but cannot
becompletely removed from process table, until it has been determined that exit status
is no longer needed.
Daemon Process
Some programs are not designed to be run with continuous user input and disconnect
from the terminal when task completed. For example, a web server responds to web
requests, rather than user input. Mail servers are another example of this type of
application. These types of programs are known as daemons.
Starting a Process:
Process can be started in two ways:
In Foreground: By default every process starts in foreground, ie. Gets the input from
keyboard and sends the output in monitor. But in this case till the process completes
its execution no other process can be started in foreground.
In Background: To take the advantage multiprocessing environment, a process can
be started in background, so that other process can be started in the foreground
without waiting for the previous process to complete execution.
A process can be started in background by adding ampersand(&) after it.
Sometimes it may be required to execute certain Job or task on specific time. It may
be not possible for any user to start or execute the job physically on that particular
time due to other business requirements. Again if similar task need to be executed
periodically on a regular interval, then the task becomes very hectic. Unix internally
has solutions to handle this type of situations using utilities such as cron and at.
Crontab
The word 'crontab' in UNIX stand for chron(chronograph) table, or time table for
Unix system .
The crontab is a special table where it is possible to specify commands and time or
interval to execute the command once or repeatedly. Commands can be any
executable programs, for example, a script can be written to take regular backup of all
the required tables in database. A crontab instruction can be created to execute the
script on the specified time. The cron daemon reads the table and executes the
commands at the times specified.
Components of Crontab
The basic format of a crontab schedule consists of 6 fields, placed on a single line and
separated by spaces, formatted as follows:
The various fields and their utility are explained in the image below:
Example:
00 11 * * * ~/takeBackup.sh
To schedule the same job for every minute, the cron entry will be as
follows:
* * * * * ~/takeBackup.sh
Shell Programming
The UNIX shell program interprets user commands, which are either directly entered
by the user, or which can be read from a file called the shell script or shell program.
Shell scripts are interpreted, not compiled. The shell reads commands from the script
line by line and searches for those commands on the system.
Shell script
Shell script is a file-containing list of commands to be executed in a particular order.
A good shell script will have comments, (a statement preceded by a pound sign, #) ,
describing the purpose of the statement.
In a script we can use conditional tests, such as value A is greater than value B, loops
or iterative statements to execute some steps repetitively or to navigate through a list
of data or records and do processing. We can use files to read and store data. Can
use variables to read and store data. A script may include functions also.
When a script is executed, the shell reads the commands one by one and executes
them .
We can create the simple shell script file by using vi editor or cat command like,
$ vi test.sh
Below mentioned shebang statement should be the first statement in the shell script as
it tells the system that the commands mentioned in the shell script are to be executed
by the shell /bin/sh
#!/bin/sh
Consider the shell script with just two commands pwd & ls.
$cat test.sh
#!/bin/bash
pwd
ls
$ sh filename
Or
$ ./filename
In this case we have to modify the file access permissions of the shell script before
execution.
• Arithmetic Operators.
• Relational Operators.
• Boolean Operators.
• String Operators.
Example:
#!/bin/sh
val=`expr 2 + 2`
Output:
$ Total value : 4
• There must be spaces between operators and expressions for example 2+2 is
not correct, where as it should be written as 2 + 2.
Arithmetic Operators
It is very important to note here that all the conditional expressions would be put
inside square braces with one spaces around them, for example [ $a == $b ] is correct
where as [$a==$b] is incorrect.
Relational Operators
Below are relational operators which are specific to numeric values. These operators
would not work for string values unless their value is numeric.
For example, following operators would work to check a relation between 10 and 20
as well as in between "10" and "20" but not in between "ten" and "twenty".
It is very important to note here that all the conditional expressions would be put
inside square braces with one spaces around them, for example [ $a <= $b ] is correct
where as [$a <= $b] is incorrect.
Boolean Operators
String Operators
These are string operators. Assume variable a holds "abc" and variable b holds "efg"
then:
Assume a variable file holds an existing file name "test" whose size is 100 bytes and
has read, write and execute permission on:
Wild card
Meaning Examples
/Shorthand
Matches any $ ls * will show all files
string or group of $ ls a* will show all files whose first
characters. name is starting with letter 'a'
* $ ls *.c will show all files having
extension .c
$ ls Will show all files having
ut*.c extension .c but file name
must begin with 'ut'.
? Matches any $ ls ? will show all files whose
single character. names are 1 character long
$ ls will show all files whose
fo? names are 3 character long
and file name begin with fo
[...] Matches any one $ ls Will show all files beginning
of the enclosed [abc]* with letters a,b,c
characters
Example:
$ ls /bin/[a-c]*
The Metacharacters
Unix Shell provides various metacharacters which have special meaning while using
them in any Shell Script and causes termination of a word unless quoted.
Example:
? Matches with a single character while listing files in a directory and an * would
match more than one characters.
Here is a list of most of the shell special characters (also called metacharacters):
A character may be quoted (i.e., made to stand for itself) by preceding it with a \.
Example:
Hello ./test.sh: line 2: Word: command not found shell returned 127
Hello; Word
The $ sign is one of the metacharacters, so it must be quoted to avoid special handling
by the shell:
I have $1200
Quote Description
Single quote All special characters between these quotes lose their
special meaning.
Double Most special characters between these quotes lose their
quote special meaning with these exceptions:
\$
\'
\"
\\
Backslash Any character immediately following the backslash loses
its special meaning.
Back Quote Anything in between back quotes would be treated as a
command and would be executed.
Putting a backslash in front of each special character is tedious and makes the line
difficult to read:
y∥ny‖n
There is an easy way to quote a large group of characters. Put a single quote ( ') at the
beginning and at the end of the string:
echo '<-$1500.**>; (update?) [y|n]'
Any characters within single quotes are quoted just as if a backslash is in front of each
character. So now this echo command displays properly.
If a single quote appears within a string to be output, you should not put the whole
string within single quotes instead you would precede that using a backslash (\) as
follows:
VAR=ZARA
So this is not what you wanted to display. It is obvious that single quotes prevent
variable substitution. If you want to substitute variable values and to make invert
commas work as expected then you would need to put your commands in double
quotes as follows:
VAR=ZARA
Double quotes take away the special meaning of all characters except the following:
Syntax: var=`command`
Example:
Following would execute date command and produced result would be stored in
DATA variable.
While writing a shell script, there may be situations when you need to adopt one path
out of many available paths. In such cases you need to make use of conditional
statements that allow your program to make correct decisions and perform right
actions.
Unix Shell supports conditional statements, which are used to perform different
actions based on different conditions.
If..else statements
We can use “if..else” statement which can be used as decision making statement to
select an option from a given set of options.
Unix Shell supports following forms of if..else statement:
• if...fi statement
• if...else...fi statement
• if...elif...else...fi statement
Syntax:
• if...fi statement
if [condition]
then
command(s)
fi
• if...else...fi statement
if [ condition(s) ] then
command(s)
else
command(s)
fi
• if...elif...else...fi statement
if [ condition(s) ]
then
command(s)
command(s)
else
command(s)
fi
We can use test command as condition of if condition as used in the below script.
$cat if_test.sh
#!/bin/sh
read ans
then
exit
else
fi
case...esac Statement
We can use multiple if...elif statements to perform a multiway branch. However, this
is not always the best solution, especially when all of the branches depend on the
value of a single variable.
Unix Shell supports case...esac statement which handles exactly this situation, and it
does so more efficiently than repeated if...elif statements.
The interpreter checks each case against the value of the expression until it finds a
match. If nothing matches, goes with the default condition.
Syntax:
;;
pattern2)
;;
pattern3)
;;
esac
Here string word is compared against every pattern until a match is found and the
statement(s) following the match pattern executes. If shell cannot find any match, the
case statement exits without performing any action. When statement(s) part is
executed. The command ;; indicates program flow should jump to the end of the
entire case statement.
Example:
#!/bin/sh
COURSE=”DB”
case “$COURSE” in
;;
;;
;;
esac
Output:
Oracle is a DB
Iterative Statements/Loop :
Loops are a powerful programming tool that enables you to execute a set of
commands repeatedly.
• while loop
• for loop
• until loop
while loop
Here condition is evaluated by shell at the beginning of each iteration. If the resulting
value is true, given command(s) are executed. If condition is false then no command
would be executed and program would jump to the next line after done statement.
Syntax:
while condition
done
Example:
a=0
while [ $a -lt 3 ]
do
echo $a
a=`expr $a + 1`
done
Output:
until loop
Here condition is evaluated by shell. If the resulting value is false, given command(s)
are executed. If condition is true then no command would be executed and program
would jump to the next line after done statement.
Syntax:
do
command(s)
done
Example:
a=0
until [ ! $a -lt 3 ] do
echo $a a=`expr $a + 1`
done
Output:
for loop
For loop operate on lists of items. It repeats a set of commands for every item in a list.
Syntax:
do
done
Example:
for var in 0 1 2 3 4 5
do
echo $var
done
Output:
Example:
do
cat ${filename}
done
Command "break" causes the termination of a loop and “continue” resumes execution
at its top.
String Handling
• String handling with test command:
The expr is quite handy for finding the length of a string and extracting a sub-string:
$ str=”abcdefghijk” ;
$ echo $n
11
expr gave how many times any character (.*) occurs. This feature is very useful in
validating data entry.
Extracting a sub-string:
$ str=”abcdefghijk” ;
gh
Note that there are 6 dots preceding the sequence ..... This advanced regular
expression signifies that the first six characters of the string are to be ignored and
extraction should start from the 7th character. Two dots inside .... suggests that this
extraction is limited to two characters only (backslashes override the usual
interpretation of ‘()’).
Extracting string from 3rd character to end of the string:
$ str="abcdefghijk"
cdefghijk
$ str=”abcdefghijk” ;
Below will give the last occurrence of character 'a' from string str.
9.
The statement we write in the command prompt starting with the command followed
by list of arguments is the command line argument.
Example:
$0 – represents the command name (first word on command line). For above example
“ls”
$1 - the first argument of command (second word/symbol ).For the above example
dir1
$2 – the second argument to the command. For the above example, dir2
The command-line arguments $1, $2, $3...$9 are also called positional parameters,
with $0 pointing to the actual command/program/shell script and $1, $2, $3, ...$9 as
the arguments to the command.
$cat test.sh
#!/bin/sh
Let’s execute the above shell script “test.sh”. Remember to change File access
permission of script to execute
To get input from the keyboard, you use the read command. The read command takes
input from the keyboard and assigns it to a variable.
Example: read.sh
#!/bin/sh
read text
Note that “-n” given to the echo command causes it to keep the cursor on the same
line; i.e., it does not output a carriage return at the end of the prompt.
Next, we invoke the read command with “text” as its argument. What this does is wait
for the user ro type something followed by a carriage return (the Enter key) and then
assign whatever was typed to the variable text.
Execution:
$sh read.sh
Let us assume, we have one shell script which requires exactly 2 arguments to
execute. Shell script should throw proper error message in case user has not given
exactly two arguments.
Let us have a small shell script "test.sh" to show how to implement validations.
#!/bin/sh
if [ $# -ne 2 ]
then
else
$ ./test.sh
$ sh test.sh abc
Not sufficient arguments, please give exact 2 arguments
For scripts where input expected is a file , validations can be incorporated for file tests
like whether the file given as input exists or not, whether it is empty or not, whether it
is readable or not and so on as per requirements.
Basic UNIX commands :
The basic Unix commands are:
echo
Syntax:
echo [OPTION]... [STRING]...
Example:
Good Morning
passwd command
The passwd command is used to change passwords for user accounts. A normal user
may only change the password for his or her own account, while the superuser may
change the password for any account.
Options:
uname command
Options:
uname command:
Used to get the information about the current system (the name, version and other
details)
$uname
$ uname -s
$ uname -r
who command:
who command prints information about all users who are currently logged in.
Syntax:
who [OPTION]... [ FILE ] [ am i ]
find command:
find is one of the powerful utility of Unix (or Linux). It can search
the entire file-system to locate files and directories according to the specific
search criteria. Other than searching files in can also perform actions(executing
commands) on searched files.
Syntax:
Examples :
1 . File Type based Search:
>> The following command finds the file named Abc in current(.) directory and
all its sub-directories
Searches for the file Abc with any extension only in directories Dir1 and Dir2
4 . Size Based Search : Using find files can be searched based on its size .
>> To find the files with size more than 10Mb the command will be
./Abc
7 . Perform Action on the search result : Using exec option any action (command)
can be executed on the search result.
>> To remove all the files having extension .swp from the current directory the
command will be :
bc command:
In interactive mode it accepts input from the terminal and providing calculations on
requested arithmatic expression.
That means typing the command bc on a Unix command prompt and entering a
mathematical expression, such as (1 + 4) * 2 , whereupon 10 will be the output
While used with files , bc starts by processing code from all the files listed on the
command line in the order listed. After all files have been processed, bc reads from
the standard input. All code is executed as it is read.
Example:
$ echo "2+5" | bc
tty command:
Prints the file name of the terminal connected in standard input.
Syntax : tty [OPTION]...
One of the first end-user programs hosted on the system and standardized in UNIX-
based systems ever since is the vi editor. ed is a line editor for the UNIX operating
system.
vi Editor:
vi []
For cursor movement within the vi editor, we can not use the mouse . We have to use
key commands. To use a key command, user has to be in the command mode. To go
to command mode, press the Esc key.
The new or modified file is saved when user exits from vi. But it is also possible to
exit without saving the file.
To use a command to exit from vi , after pressing the Esc key ,colon : is to be pressed.
The cursor moves to bottom of screen whenever a colon (:) is typed. This type of
command is completed by hitting the (or ) key.
I
insert text at beginning of current
line, until hit
• touch
• cat
• cat > (single redirection)
• cat >>(double redirection)
• cp
• mv
• cmp
• comm
• diff
touch command
Touch command is used in Unix to create an empty file.
Touch command is used to change the timestamps (access time and modification time
of a file).
cat command
Use of cat command:
'cat' command is used to display a file content.
Syntax:
$ cat data
This is an unix file
This is line two
$
cat with double redirection (cat >>filename) appends the content given through
standard input to the file.
cp command
cp command is used to copy one file's content to another file.
Syntax: $ cp [source filename] [destination filename]
Switches:
1. cp -i [sourcefilename] [destination filename] This command copies the content of a
file interactively.
mv command
mv command is used for:
1. for renaming a file in same directory.
2. for moving a file to a different directory.
Syntax:
$ mv [sourcefilename] [destinationfilename]
$ mv [source_directory] [destination_directory]
Directory Commands:
Every item in the UNIX filesystem tree is either a file, or a directory. A directory is
like a file folder. A directory can contain files, and other directories. A directory
contained within another is called the child of the other. A directory in the filesystem
tree may have many children, but it can only have one parent. A file can hold
information, but cannot contain other files, or directories.
The place in the file system tree where an user is located is called the current
working directory (which can be listed using pwd command).
ls Command
Lists the contents of any directory (current directory if no directory specified).
Sorts entries without option
$ ls [option(s)] [filename]
Syntax :
Syntax:
Example: To show only lines common between file1 and file2, command is
The change commands are in the format [range][acd][range]. The range on the left
may be a line number or a comma-separated range of line numbers referring to file1,
and the range on the right similarly refers to file2. The character in the middle
indicates the action i.e. add, change or delete.
• ‘LaR’ – Add lines in range ‘R’ from file2 after line ‘L’ in file1.
• ‘FcT’ – Change lines in range ‘F’ of file1 to lines in range ‘T’ of file2.
• ‘RdL’ – Delete lines in range ‘R’ from file1 that would have appeared at line
‘L’ in file2
Introduction to WinSCP
Types of OS
OS are classified into different types depending on their capability of processing.
These are simple operating system designed to manage one task at a time ,for use by a
single user for a standalone single computer for performing a single task .
These OS allow execution of more than one task or process concurrently by dividing
the processor time amongst different tasks.
Multi-programming OS:
These OS allow more than one programs to run at the same time .
Embedded OS:
Embedded in a device in the ROM. They are specific to a device and are less resource
intensive.
Function Of OS
The OS performs basic tasks such as controlling and allocating memory, prioritizing
system requests, controlling input and output devices, facilitating networking, and
managing files
UNIX Introduction
Supports C, Fortran, Basic, Pascal, COBOL, Lisp, Prolog, Java, Ada compilers
Features of Unix Operating System
The UNIX operating system has a layered architecture having three main components
• Kernel
• Shell
• Utilities
• It allocates time and memory to programs and handles the file store and
communications
• Interacts directly with the hardware through device drivers
• Provides sets of services to programs
• Manages memory, controls access, maintains file system, handles interrupts,
allocates resources of the computer
System calls
The system calls are functions used in the kernel itself. UNIX system calls are used to
manage the file system, control processes, and to provide interpocess communication.
• File structure related calls -For example create, open,read, write,lseek,dup etc.
• Process Related calls -For example fork,exec,wait,exit etc
• Inter process related calls - For example pipe,msgget,msgsnd etc.
• ordinary
• directory
• special files (device, pipe, fifo, socket).
Root is the supremo and is represented by the ‘/’. Every subdirectory must have a
parent.
File names can be up to 14 characters long; can contain both upper and lower case
alphabets, digits, a dot, hyphen (-), underscore (_) anywhere; should not have a blank
or tab; are case-sensitive.
Path names are a sequence of directory names separated by ‘/’. They are used to
access files.
Absolute pathname - file location is determined with respect to the root.
Relative pathname - file location is determined with respect to the current directory.
Though the UFS looks hierarchical, it is actually a directed acyclic graph because files
can be shared.
/home – It holds user’s home directories. In other UNIX systems, this can be /usr
directory.
/bin – It holds many of the basic Linux programs; bin stands for binaries, files that are
executable.
/dev – It holds device files. All info sent to /dev/null is thrown into trash. Your
terminal is one of the /dev/tty files.
The UFS resides on a single logical disk. A logical disk is a disk partition comprising
of a set of consecutive cylinders.
UFS further subdivides a partition into one or more cylinder groups and attempts to
allocate inodes and related data blocks from the same cylinder group, thus minimizing
the disk head movements.
At the beginning of the logical disk lies the boot block of UNIX operating system
containing the bootstrap program.
Each cylinder group contains a duplicate copy of the super block. The super block
contains the size of file system, number of free blocks, index of next free block in free
block list, size of inode list, number of free inodes, index of next free inode in free
inode list.
The cylinder group block contains a number of inodes and corresponding data blocks
for that cylinder group. The block size is a power of 2 (>=4096).
For each file created in the system, an inode is also created. Inode is a disk file record
of 64 bytes that maintains the permanent attributes of a file.
An inode is permanent and it exists until the corresponding file is removed from the
system.
Sample details of an inode –
Whenever a file is opened, its inode is brought into main memory. The active inode is
kept there until the file is closed and is used to locate the beginning of an open file on
disk and to verify that every I/O request comes from a valid user as per specified
access permissions.
Users and Access Rights
Objective
This module is to understand about different user types in UNIX and their access
rights.
Course Content
• User types
• File access permissions
• Changing file access permissions using symbolic mode
• Changing file access permissions using octal mode
• Symbolic mode
• Absolute mode
chmod command options:
With symbolic representation, permission set can be added, deleted, or specified using
the operators listed in the following table:
Example:
Let us assume there is a file name "abc" , whose initial permissions are only read
permission to user and group and others. We will provide full permissions to all three
types of users.
Let us verify the existing file permissions using ls -l command as below and observe
the next steps, like changing the permissions and verifying the changes.
In the above example,
chmod u+wx,g+wx abc , adds write and execute permission to user and group to the
file abc
chmod g-x abc, removes execute permission from group from the file abc.
chmod o=r-- abc, provides read permission and removes write and execute
permission from others for file abc
The second way to modify permissions with the chmod command is to use a number
to specify each set of permissions for the file. Each permission is assigned a value, as
the following table shows, and the total of each set of permissions provides a number
for that set.
Examples: Observe the following sequence of command execution statements and
analyze the output
If we observe the output, initially owner have read and write permissions and group
and others have only read permission
2. Let us remove all the permission from all users for abc file, as below
chmod 0 abc
Let us verify the changed permissions
ls -l abc
---------- 1 735873 oinstall 0 Feb 7 12:37 abc
Yes all permissions are taken away from all users on the file abc
3. To provide all permissions to the user, read permission to group and write and
execute permissions for Others,
Hello!
You can save this file in your system to use it. This file can be used for practice.
Happy Learning!
9. Display how many number of lines are there in myFile.
Expected Output : 6
9.Count how many users are currently logged into the system
Expected Output :
You can save this file in your system to use it. This file can be used for practice.
Expected Output : 3
Expected Output :
Hello!
You can save that file in your system to use it. This file can be used for practice.
13.Replace the word this by that only for the first occurrence of it in a line.
Expected Output :
Hello!
You can save that file in your system to use it. This file can be used for practice.
Happy Learning!
Expected Output :
Hello!
Expected Output :
You can save this file in your system to use it. This file can be used for practice.
awk Scripting :
18. Create a file with name student.txt with the following contents in it
RollNo|Name|Marks1|Marks2|Marks3
123|Raghu|80|90|60
342|Maya|45|78|82
561|Gita|56|71|89
480|Mohan|71|90|89
a. Write an awk command to print the name and roll number of the students.
Expected Output:
Name RollNo
Raghu 123
Maya 342
Gita 561
Mohan 480
b . Write the awk command to calculate the total marks of each student and display
the total marks along with the name of the student.
Expected Output:
Raghu-230
Maya-205
Gita-216
Mohan-250
Shell Scripting
19. Write a shell script to print the sum of the 1st 10 natural numbers.
Expected Output: 55
19. Write a shell script to print the odd numbers from a given list of numbers. The list
of numbers will be given as command line argument.
Example :
Expected Output :
45
20. Create a file studentScores.txt and append the following content to it.
RollNo|Name|Marks
123|Raghu|80
342|Maya|45
561|Gita|56
480|Mohan|71
write a shell script named scores.sh to find the name of the student who scored highest
marks. Incorporate validations for the following :
Expected Output :
4. https://www.tutorialspoint.com/unix_terminal_online.php
5. http://www.compileonline.com/execute_bash_online.php
6. https://bellard.org/jslinux/vm.html?url=https://bellard.org/jslinux/buildroot-
x86.cfg
A process require certain resources to accomplish its intended task, they are
>> CPU time
>> System memory
>> Disk Files
>> I/O devices.
Process state:- The state may be new, ready, running, waiting, halted, and so on.
Program counter:- The counter indicates the address of the next instruction to be
executed for this process.
CPU registers:- The registers vary in number and type, depending on the computer
architecture. They include accumulators, index registers, stack pointers, and general-
purpose registers, plus any condition-code information.
CPU-scheduling information:- This information includes a process priority, pointers
to scheduling queues, and any other scheduling parameters.
Memory-management information:- This information may include such
information as the value of the base and limit registers, the page tables, or the segment
tables, depending on the memory system used by the OS.
Accounting information:- This information includes the amount of CPU and real
time used, time limits, account numbers, job or process numbers, and so on.
I/O status information:- This information includes the list of I/O devices allocated to
the process, a list of open files, and so on.
Process state:
A process may present in one of the following state.
New:- The process is being created or just created.
Ready:- The process is waiting to be assigned/allocated to a processor time.
Running:- Instructions of the process are being executed by processor.
Waiting:- The process is waiting for some other event/process to execute.
Terminated:- The process has finished its execution.
ps command:
The ps (i.e., process status) command is used to provide information about the
currently running processes in the system. When ps is used without any option four
columns of information labeled PID,TTY, TIME and CMD for at least two processes,
the shell and ps will be visible.
PID :- The process are identified by a 5 digit number known as PID (Process
Identification Number).
TTY :- Is the name of the console or terminal that the user logged into (Stands for
terminal type now
but originally stood for teletype).
TIME :- Is the amount of CPU time in minutes and seconds that the process has been
running.
CMD :- is the name of the command that launched the process.
Types of process
Parent and Child Process:
A process can initiate a sub process, which is a called a child process, the initiating
process is referred to as its parent.The child processes, in turn create other child
processes forming a tree of processes
( which can be displayed using ps command with –forest option)
Orphan Process:
When a child process is killed, parent process gets the notification via a signal. Parent
then, can continue other task. However if the parent process is killed before, its child,
is called an orphan process.
Zombie Process:
When a process finished its execution and exit status not received by the parent ( or
parent did not read the exit status till now), the process state becomes zombie.
The process is dead (not to be scheduled for further execution) but cannot
becompletely removed from process table, until it has been determined that exit status
is no longer needed.
Daemon Process
Some programs are not designed to be run with continuous user input and disconnect
from the terminal when task completed. For example, a web server responds to web
requests, rather than user input. Mail servers are another example of this type of
application. These types of programs are known as daemons.
Starting a Process:
Process can be started in two ways:
In Foreground: By default every process starts in foreground, ie. Gets the input from
keyboard and sends the output in monitor. But in this case till the process completes
its execution no other process can be started in foreground.
In Background: To take the advantage multiprocessing environment, a process can
be started in background, so that other process can be started in the foreground
without waiting for the previous process to complete execution.
A process can be started in background by adding ampersand(&) after it.
Sometimes it may be required to execute certain Job or task on specific time. It may
be not possible for any user to start or execute the job physically on that particular
time due to other business requirements. Again if similar task need to be executed
periodically on a regular interval, then the task becomes very hectic. Unix internally
has solutions to handle this type of situations using utilities such as cron and at.
Crontab
The word 'crontab' in UNIX stand for chron(chronograph) table, or time table for
Unix system .
The crontab is a special table where it is possible to specify commands and time or
interval to execute the command once or repeatedly. Commands can be any
executable programs, for example, a script can be written to take regular backup of all
the required tables in database. A crontab instruction can be created to execute the
script on the specified time. The cron daemon reads the table and executes the
commands at the times specified.
Components of Crontab
The basic format of a crontab schedule consists of 6 fields, placed on a single line and
separated by spaces, formatted as follows:
The various fields and their utility are explained in the image below:
Example:
00 11 * * * ~/takeBackup.sh
To schedule the same job for every minute, the cron entry will be as
follows:
* * * * * ~/takeBackup.sh
Shell Programming
The UNIX shell program interprets user commands, which are either directly entered
by the user, or which can be read from a file called the shell script or shell program.
Shell scripts are interpreted, not compiled. The shell reads commands from the script
line by line and searches for those commands on the system.
Shell script
Shell script is a file-containing list of commands to be executed in a particular order.
A good shell script will have comments, (a statement preceded by a pound sign, #) ,
describing the purpose of the statement.
In a script we can use conditional tests, such as value A is greater than value B, loops
or iterative statements to execute some steps repetitively or to navigate through a list
of data or records and do processing. We can use files to read and store data. Can
use variables to read and store data. A script may include functions also.
When a script is executed, the shell reads the commands one by one and executes
them .
We can create the simple shell script file by using vi editor or cat command like,
$ vi test.sh
Below mentioned shebang statement should be the first statement in the shell script as
it tells the system that the commands mentioned in the shell script are to be executed
by the shell /bin/sh
#!/bin/sh
Consider the shell script with just two commands pwd & ls.
$cat test.sh
#!/bin/bash
pwd
ls
$ sh filename
Or
$ ./filename
In this case we have to modify the file access permissions of the shell script before
execution.
• Arithmetic Operators.
• Relational Operators.
• Boolean Operators.
• String Operators.
Example:
#!/bin/sh
val=`expr 2 + 2`
Output:
$ Total value : 4
• There must be spaces between operators and expressions for example 2+2 is
not correct, where as it should be written as 2 + 2.
Arithmetic Operators
It is very important to note here that all the conditional expressions would be put
inside square braces with one spaces around them, for example [ $a == $b ] is correct
where as [$a==$b] is incorrect.
Relational Operators
Below are relational operators which are specific to numeric values. These operators
would not work for string values unless their value is numeric.
For example, following operators would work to check a relation between 10 and 20
as well as in between "10" and "20" but not in between "ten" and "twenty".
It is very important to note here that all the conditional expressions would be put
inside square braces with one spaces around them, for example [ $a <= $b ] is correct
where as [$a <= $b] is incorrect.
Boolean Operators
String Operators
These are string operators. Assume variable a holds "abc" and variable b holds "efg"
then:
Assume a variable file holds an existing file name "test" whose size is 100 bytes and
has read, write and execute permission on:
Wild card
Meaning Examples
/Shorthand
Matches any $ ls * will show all files
string or group of $ ls a* will show all files whose first
characters. name is starting with letter 'a'
* $ ls *.c will show all files having
extension .c
$ ls Will show all files having
ut*.c extension .c but file name
must begin with 'ut'.
? Matches any $ ls ? will show all files whose
single character. names are 1 character long
$ ls will show all files whose
fo? names are 3 character long
and file name begin with fo
[...] Matches any one $ ls Will show all files beginning
of the enclosed [abc]* with letters a,b,c
characters
Example:
$ ls /bin/[a-c]*
The Metacharacters
Unix Shell provides various metacharacters which have special meaning while using
them in any Shell Script and causes termination of a word unless quoted.
Example:
? Matches with a single character while listing files in a directory and an * would
match more than one characters.
Here is a list of most of the shell special characters (also called metacharacters):
A character may be quoted (i.e., made to stand for itself) by preceding it with a \.
Example:
Hello ./test.sh: line 2: Word: command not found shell returned 127
Hello; Word
The $ sign is one of the metacharacters, so it must be quoted to avoid special handling
by the shell:
I have $1200
Quote Description
Single quote All special characters between these quotes lose their
special meaning.
Double Most special characters between these quotes lose their
quote special meaning with these exceptions:
\$
\'
\"
\\
Backslash Any character immediately following the backslash loses
its special meaning.
Back Quote Anything in between back quotes would be treated as a
command and would be executed.
Putting a backslash in front of each special character is tedious and makes the line
difficult to read:
y∥ny‖n
There is an easy way to quote a large group of characters. Put a single quote ( ') at the
beginning and at the end of the string:
echo '<-$1500.**>; (update?) [y|n]'
Any characters within single quotes are quoted just as if a backslash is in front of each
character. So now this echo command displays properly.
If a single quote appears within a string to be output, you should not put the whole
string within single quotes instead you would precede that using a backslash (\) as
follows:
VAR=ZARA
So this is not what you wanted to display. It is obvious that single quotes prevent
variable substitution. If you want to substitute variable values and to make invert
commas work as expected then you would need to put your commands in double
quotes as follows:
VAR=ZARA
Double quotes take away the special meaning of all characters except the following:
Syntax: var=`command`
Example:
Following would execute date command and produced result would be stored in
DATA variable.
While writing a shell script, there may be situations when you need to adopt one path
out of many available paths. In such cases you need to make use of conditional
statements that allow your program to make correct decisions and perform right
actions.
Unix Shell supports conditional statements, which are used to perform different
actions based on different conditions.
If..else statements
We can use “if..else” statement which can be used as decision making statement to
select an option from a given set of options.
Unix Shell supports following forms of if..else statement:
• if...fi statement
• if...else...fi statement
• if...elif...else...fi statement
Syntax:
• if...fi statement
if [condition]
then
command(s)
fi
• if...else...fi statement
if [ condition(s) ] then
command(s)
else
command(s)
fi
• if...elif...else...fi statement
if [ condition(s) ]
then
command(s)
command(s)
else
command(s)
fi
We can use test command as condition of if condition as used in the below script.
$cat if_test.sh
#!/bin/sh
read ans
then
exit
else
fi
case...esac Statement
We can use multiple if...elif statements to perform a multiway branch. However, this
is not always the best solution, especially when all of the branches depend on the
value of a single variable.
Unix Shell supports case...esac statement which handles exactly this situation, and it
does so more efficiently than repeated if...elif statements.
The interpreter checks each case against the value of the expression until it finds a
match. If nothing matches, goes with the default condition.
Syntax:
;;
pattern2)
;;
pattern3)
;;
esac
Here string word is compared against every pattern until a match is found and the
statement(s) following the match pattern executes. If shell cannot find any match, the
case statement exits without performing any action. When statement(s) part is
executed. The command ;; indicates program flow should jump to the end of the
entire case statement.
Example:
#!/bin/sh
COURSE=”DB”
case “$COURSE” in
;;
;;
;;
esac
Output:
Oracle is a DB
Iterative Statements/Loop :
Loops are a powerful programming tool that enables you to execute a set of
commands repeatedly.
• while loop
• for loop
• until loop
while loop
Here condition is evaluated by shell at the beginning of each iteration. If the resulting
value is true, given command(s) are executed. If condition is false then no command
would be executed and program would jump to the next line after done statement.
Syntax:
while condition
done
Example:
a=0
while [ $a -lt 3 ]
do
echo $a
a=`expr $a + 1`
done
Output:
until loop
Here condition is evaluated by shell. If the resulting value is false, given command(s)
are executed. If condition is true then no command would be executed and program
would jump to the next line after done statement.
Syntax:
do
command(s)
done
Example:
a=0
until [ ! $a -lt 3 ] do
echo $a a=`expr $a + 1`
done
Output:
for loop
For loop operate on lists of items. It repeats a set of commands for every item in a list.
Syntax:
do
done
Example:
for var in 0 1 2 3 4 5
do
echo $var
done
Output:
Example:
do
cat ${filename}
done
Command "break" causes the termination of a loop and “continue” resumes execution
at its top.
String Handling
• String handling with test command:
The expr is quite handy for finding the length of a string and extracting a sub-string:
$ str=”abcdefghijk” ;
$ echo $n
11
expr gave how many times any character (.*) occurs. This feature is very useful in
validating data entry.
Extracting a sub-string:
$ str=”abcdefghijk” ;
gh
Note that there are 6 dots preceding the sequence ..... This advanced regular
expression signifies that the first six characters of the string are to be ignored and
extraction should start from the 7th character. Two dots inside .... suggests that this
extraction is limited to two characters only (backslashes override the usual
interpretation of ‘()’).
Extracting string from 3rd character to end of the string:
$ str="abcdefghijk"
cdefghijk
$ str=”abcdefghijk” ;
Below will give the last occurrence of character 'a' from string str.
10.
The statement we write in the command prompt starting with the command followed
by list of arguments is the command line argument.
Example:
$0 – represents the command name (first word on command line). For above example
“ls”
$1 - the first argument of command (second word/symbol ).For the above example
dir1
$2 – the second argument to the command. For the above example, dir2
The command-line arguments $1, $2, $3...$9 are also called positional parameters,
with $0 pointing to the actual command/program/shell script and $1, $2, $3, ...$9 as
the arguments to the command.
$cat test.sh
#!/bin/sh
Let’s execute the above shell script “test.sh”. Remember to change File access
permission of script to execute
To get input from the keyboard, you use the read command. The read command takes
input from the keyboard and assigns it to a variable.
Example: read.sh
#!/bin/sh
read text
Note that “-n” given to the echo command causes it to keep the cursor on the same
line; i.e., it does not output a carriage return at the end of the prompt.
Next, we invoke the read command with “text” as its argument. What this does is wait
for the user ro type something followed by a carriage return (the Enter key) and then
assign whatever was typed to the variable text.
Execution:
$sh read.sh
Let us assume, we have one shell script which requires exactly 2 arguments to
execute. Shell script should throw proper error message in case user has not given
exactly two arguments.
Let us have a small shell script "test.sh" to show how to implement validations.
#!/bin/sh
if [ $# -ne 2 ]
then
else
$ ./test.sh
$ sh test.sh abc
Not sufficient arguments, please give exact 2 arguments
For scripts where input expected is a file , validations can be incorporated for file tests
like whether the file given as input exists or not, whether it is empty or not, whether it
is readable or not and so on as per requirements.
Basic UNIX commands :
The basic Unix commands are:
echo
Syntax:
echo [OPTION]... [STRING]...
Example:
Good Morning
passwd command
The passwd command is used to change passwords for user accounts. A normal user
may only change the password for his or her own account, while the superuser may
change the password for any account.
Options:
uname command
Options:
uname command:
Used to get the information about the current system (the name, version and other
details)
$uname
$ uname -s
$ uname -r
who command:
who command prints information about all users who are currently logged in.
Syntax:
who [OPTION]... [ FILE ] [ am i ]
find command:
find is one of the powerful utility of Unix (or Linux). It can search
the entire file-system to locate files and directories according to the specific
search criteria. Other than searching files in can also perform actions(executing
commands) on searched files.
Syntax:
Examples :
1 . File Type based Search:
>> The following command finds the file named Abc in current(.) directory and
all its sub-directories
Searches for the file Abc with any extension only in directories Dir1 and Dir2
4 . Size Based Search : Using find files can be searched based on its size .
>> To find the files with size more than 10Mb the command will be
./Abc
7 . Perform Action on the search result : Using exec option any action (command)
can be executed on the search result.
>> To remove all the files having extension .swp from the current directory the
command will be :
bc command:
In interactive mode it accepts input from the terminal and providing calculations on
requested arithmatic expression.
That means typing the command bc on a Unix command prompt and entering a
mathematical expression, such as (1 + 4) * 2 , whereupon 10 will be the output
While used with files , bc starts by processing code from all the files listed on the
command line in the order listed. After all files have been processed, bc reads from
the standard input. All code is executed as it is read.
Example:
$ echo "2+5" | bc
tty command:
Prints the file name of the terminal connected in standard input.
Syntax : tty [OPTION]...
One of the first end-user programs hosted on the system and standardized in UNIX-
based systems ever since is the vi editor. ed is a line editor for the UNIX operating
system.
vi Editor:
vi []
For cursor movement within the vi editor, we can not use the mouse . We have to use
key commands. To use a key command, user has to be in the command mode. To go
to command mode, press the Esc key.
The new or modified file is saved when user exits from vi. But it is also possible to
exit without saving the file.
To use a command to exit from vi , after pressing the Esc key ,colon : is to be pressed.
The cursor moves to bottom of screen whenever a colon (:) is typed. This type of
command is completed by hitting the (or ) key.
I
insert text at beginning of current
line, until hit
• touch
• cat
• cat > (single redirection)
• cat >>(double redirection)
• cp
• mv
• cmp
• comm
• diff
touch command
Touch command is used in Unix to create an empty file.
Touch command is used to change the timestamps (access time and modification time
of a file).
cat command
Use of cat command:
'cat' command is used to display a file content.
Syntax:
$ cat data
This is an unix file
This is line two
$
cat with double redirection (cat >>filename) appends the content given through
standard input to the file.
cp command
cp command is used to copy one file's content to another file.
Syntax: $ cp [source filename] [destination filename]
Switches:
1. cp -i [sourcefilename] [destination filename] This command copies the content of a
file interactively.
mv command
mv command is used for:
1. for renaming a file in same directory.
2. for moving a file to a different directory.
Syntax:
$ mv [sourcefilename] [destinationfilename]
$ mv [source_directory] [destination_directory]
Directory Commands:
Every item in the UNIX filesystem tree is either a file, or a directory. A directory is
like a file folder. A directory can contain files, and other directories. A directory
contained within another is called the child of the other. A directory in the filesystem
tree may have many children, but it can only have one parent. A file can hold
information, but cannot contain other files, or directories.
The place in the file system tree where an user is located is called the current
working directory (which can be listed using pwd command).
ls Command
Lists the contents of any directory (current directory if no directory specified).
Sorts entries without option
$ ls [option(s)] [filename]
Syntax :
Syntax:
Example: To show only lines common between file1 and file2, command is
The change commands are in the format [range][acd][range]. The range on the left
may be a line number or a comma-separated range of line numbers referring to file1,
and the range on the right similarly refers to file2. The character in the middle
indicates the action i.e. add, change or delete.
• ‘LaR’ – Add lines in range ‘R’ from file2 after line ‘L’ in file1.
• ‘FcT’ – Change lines in range ‘F’ of file1 to lines in range ‘T’ of file2.
• ‘RdL’ – Delete lines in range ‘R’ from file1 that would have appeared at line
‘L’ in file2
Introduction to WinSCP