0% found this document useful (0 votes)
33 views26 pages

Laboratory Lecture 2

This document discusses Linux commands like cat, head, tail, sort, wc, grep, and pr. It provides examples of using each command on sample text files and observing the output. The document is intended to teach basic file manipulation and text processing commands in Linux.

Uploaded by

Charles Mashava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views26 pages

Laboratory Lecture 2

This document discusses Linux commands like cat, head, tail, sort, wc, grep, and pr. It provides examples of using each command on sample text files and observing the output. The document is intended to teach basic file manipulation and text processing commands in Linux.

Uploaded by

Charles Mashava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Operating System

(CPET 410)

Professor: Michael C. Olivo


1.Reading Files in Linux [Cat, >, <, >>]
Cat Command
This command is used to concatenate or join
files.
Syntax: cat [option] [filename].

Type gedit in console terminal and press enter


Example:
[mcolivo@localhost Desktop]$ gedit
Reading Files in Linux [Cat, >, <, >>]

In GUI interface type the text below:

This is just a sample text file.


Created on January 16, 2018.
Time of creation is 10:30 am
Reading Files in Linux [Cat, >, <, >>]

Save the file as catfile.txt at Desktop


directory
Open the terminal and type cat

filename.txt and press enter


Example:
[mcolivo@localhost Desktop]$ cat catfile.txt
Reading Files in Linux [Cat, >, <, >>]

Try executing cat with an option n


and verifying the output.
Example:
[mcolivo@localhost Desktop]$ cat catfile.txt -n
Reading Files in Linux [Cat, >, <, >>]
2. > Operator
The > operator is used to redirect output to the new
standard output.
Syntax: cat > filename

Example: Type gedit newfile.txt and press enter and


save, open terminal and type the command below
[mcolivo@localhost Desktop]$ cat catfile.txt >
newfile.txt
Reading Files in Linux [Cat, >, <, >>]

3. >> Operator
The operator >> appends to the file exists or
else creates it as a new file and writes the
data.
Syntax: cat sourcefile >> targetfile
Example:
Type gedit newfile1.txt and press enter. Click
save and open the terminal. Type the
command below
Reading Files in Linux [Cat, >, <, >>]

Example 1:
[mcolivo@localhost Desktop]$ cat newfile.txt >>
newfile1.txt
 Verify the output of newfile1.txt

Example 2:
[mcolivo@localhost Desktop]$ cat newfile1.txt >>
newfile.txt
 Verify the output of newfile.txt
Linux Commands [Head, Tail, Sort]

5. The head command


The head command is useful to see a few lines of
a file, typically the first few lines. The default is
10 lines if no option -i or -ci (where i is any
positive integer and c for character prefix).
Syntax:
head [filename] or head –i[filename]or head –
ci[filename].
Linux Commands [Head, Tail, Sort]

Using gedit create two text files (names.txt and lines.txt).

Content of names.txt:
 
noddy
tom
jerry
donald
mickey
asterisk
tintin
obelix
Content of lines.txt:

A lie never lives to be old. -- Sophocles


An honest man is always a child. -- Martial
When in doubt tell the truth. -- Mark Twain
False words are not only devil in themselves, but they infect the soul with
evil. -- Socrates
All truth is not to be told at all times. –- Thomas Fuller, M.D.
Linux Commands [Head, Tail, Sort]
Example 1:
Show the first 3 lines of text in name.txt

[mcolivo@localhost Desktop]$ head -3


name.txt
noddy
tom
Jerry
Linux Commands [Head, Tail, Sort]

Example 2: Show the first 5 lines of


text in lines.txt

[mcolivo@localhost Desktop]$ head –n5


lines.txt

What did you observe?


Linux Commands [Head, Tail, Sort]

Show the first 10 characters of the name.text

[mcolivo@localhost Desktop]$ head -c10


name.txt
noddy
Tom
Linux Commands [Head, Tail, Sort]
6. The Tail Command
The tail command is used to show the last few lines of a file.
Syntax:
tail[filename] or tail –i[filename] or tail –ci [filename]
Note:
Combining head and tail intelligently, views lines in the middle of the file.

Example 1:
[mcolivo@localhost Desktop]$ tail -3 name.txt
asterisk
tintin
obelix
Linux Commands [Head, Tail, Sort]
Example 2:
[mcolivo@localhost Desktop]$ tail -n4
name.txt
mickey
asterisk
tintin
obelix
Linux Commands [Head, Tail, Sort]

Show the first 10 last characters of the


name.text

[mcolivo@localhost Desktop]$ tail -c10


name.txt
in
obelix
Linux Commands [Head, Tail, Sort]
7. Sort Command
The sort command is used to sort the contents
of the file and display the sorted list on the
standard output (screen).
Syntax:
sort [filename] or sort [option] [filename]
Where option are –a (ascending order) and –r
(descending order).
Linux Commands [Head, Tail, Sort]

Example 1: Sort the file in ascending


[mcolivo@localhost Desktop]$ sort name.txt
asterisk
donald
jerry
mickey
noddy
obelix
tintin
tom
Linux Commands [Head, Tail, Sort]
Example 2: Sort the file in Descending

[mcolivo@localhost Desktop]$ sort -r name.txt


tom
tintin
obelix
noddy
mickey
jerry
donald
asterisk
Linux Commands [WC, Grep]
8. The WC Command
The command wc stands word count is used
to find the number of characters, the
number of words, and number of lines in a
file.
Syntax: wc [option] [filename].
Where options are -l(number of lines), -w
(number words), -c (number of characters),
or combination of the options.
Linux Commands [WC]

Example:
[mcolivo@localhost Desktop]$ wc -w file1.txt
4 file1.txt
[mcolivo@localhost Desktop]$ wc -c file1.txt
21 file1.txt
[mcolivo@localhost Desktop]$ wc -l file1.txt
5 file1.txt
Linux Command
9. Grep Command
The grep command is one of the most powerful commands in
Linux. It matches the given pattern with each line of the file
and displays all lines that match the pattern.
Syntax: grep [character patterns] [filename].

Example 1:
Show the lines that has character pattern character lie
from the lines.txt and verify the output.
 
[mcolivo@localhost Desktop]$ grep lie lines.txt
Reading Files in Linux
Example 2:
Show the lines that has character
pattern hope from the lines.txt and
verify the output.

[mcolivo@localhost Desktop]$ grep hope


lines.txt
Reading Files in Linux
10. Pr Command
The command pr is used to convert a text file for printing.
Syntax: pr [option] [filename]
Where option –n (n is positive integer for number columns).
 

Example:
Show the print preview of lines.txt and
names.txt in 3 columns respectively.
 
[mcolivo@localhost Desktop]$ pr -3 lines.txt
Practice Exercises
1. Show the content of lines.txt and names.txt
including the line number, respectively. Verify
the outputs. What is the command?
2. Using Tail command, show the first 10 last
characters of the name.txt and using Head
command, show the first 5 lines of lines.txt
respectively.

You might also like