You are on page 1of 25

UNIX – Day 2

Intermediate Level
Recap – Day 1

 Introduction to Unix
 Unix System Architecture
 Basic Unix Commands
 File and directory related commands
 Shell meta characters
 Standard files
 Redirection and Pipe

2
Session Plan – Day 2

 Text Processing Commands

 Process Related Commands

 Memory Related Commands

 Compression Utilities

3
Text Processing Commands
Text Processing Commands (1/7)

Command Description Syntax


 Combines the content of the files
paste line by line $ paste file1 [file2…]
 Prints the files content horizontally
Displays a set of lines of the given
head $ head [-n] file_name
data from start
Displays a set of lines of the given
tail $ tail [–n/+n] file_name
data from last
Displays total no of lines, words and
wc $ wc [–lwc] file_name
characters in any data

5
Text Processing Commands (2/7)

Command Description Syntax

 Translate characters
$ cmd_output | tr [option]
tr  Used on output of some other
command src_charset tar_charset

Variations Syntax/Sample Example


Squeeze the character $ ls –l | tr –s “ ”
Squeeze and translate the characters $ ls –l | tr –s “ ” “#”
Translate the characters $ date | tr “ ” “#”

6
Text Processing Commands (3/7)

Command Description Syntax


 Retrieves specific fields or
$ cmd_output | cut
characters from the data
cut  Used on output of some other
[option…]
command or file content $ cut [option…] file

Variations Syntax/Sample Example


$ cat file1 | cut –c5
Retrieve specific character set
$ cat file1 | cut –c5-10
$ date | cut –d “ ” –f3
$ date | cut –d “ ” –f3,5
Retrieves specific field set
-d: for delimiter
-f: for field number

7
Text Processing Commands (4/7)

Command Description Syntax

 Used to order the data


 Used on output of some other $ cmd_output | sort
sort command or file content [option…]
 Default sorting is as per the $ sort [option…] file
English dictionary

Variations Syntax/Sample Example


To sort in reverse order $ sort –r file1
To remove duplicate data $ sort –u file1
To apply numerical sort $ sort –n file1
To merge the content of two or more files $ sort –m file1 file2

8
Text Processing Commands (5/7)

Command Description Syntax


 Pattern search in the given data
grep  Global Regular Expression Parser
$ grep [option] pattern data

Variations Syntax
Ignores case while searching $ grep –i pattern file_name
Prints only the count of the number of lines,
$ grep –c pattern file_name
having given pattern
Prints the lines which are not containing
$ grep –v pattern file_name
given pattern

9
Anchor Characters for grep Command

Matching for lines Syntax


Prints lines starting with any pattern $ grep „^pattern‟ file_name
Prints lines ending with any pattern $ grep „pattern$‟ file_name

Matching for words Syntax


 Defining the boundary of the words
 This will match only alphabetic and numeric characters
Prints the lines which are containing word
$ grep „\<pattern‟ file_name
starting with any pattern
Prints the lines which are containing word
$ grep „pattern\>‟ file_name
ending with any pattern
Prints the lines which are containing given
$ grep „\<pattern\>‟ file_name
pattern as whole word

10
Text Processing Commands (6/7)

Command Description Syntax


Removes adjacent repeated lines in
uniq $ uniq [option] data
the given data

Variations Syntax
Removes adjacent repeated lines and prints
$ uniq file_name
the output
Prints only uniq lines $ uniq –u file_name
Prints only duplicate lines $ uniq –d file_name

11
Text Processing Commands (7/7)

Command Description Syntax


 Used to compare two files
cmp  Pinpoints only the first difference $ cmp file1 file2
between two files
 Used to compare two files
 Can be applied only on sorted
files
 Give the result in three columns:
First column: Data unique to first
comm $ comm file1 file2
file
Second column: Data unique to
second file
Third column: Data common in
both the files

12
Self Study: Text Processing
Commands Demo

13
Can you answer these questions?

 What should be the command:


 To count the number of directories and files
present in the current working directory
 To count the number of blank lines (lines without
text) in a file
 To display the first 8 lines from a file
 To display only the 4th line from a file

 Differentiate between
 cat file1 file2 file3 & paste file1 file2 file3

14
Process and Memory related
Commands
Process related Commands

 A process is a program in execution

 Each process is allocated a process identifier


or PID

 In general, each process is started by another,


known as parent process.

 So every process is also have one parent


process identifier or PPID

16
Process related Commands (1/2)

Command Description Syntax & important variations


$ ps
Displays the list of $ ps –f :Full Listing showing
ps processes submitted to PPID
kernel $ ps –e :Including user and
system processes
Used to see the status of
bg $ bg
background jobs
Used to bring any
fg background job in $ fg %JobID OR fg #PID
foreground

17
Process related Commands (2/2)

Command Description Syntax & important variations


Execute a command with
nice $ nice [option] [command]
updated scheduling priority
Executes the command in
nohup background even after log out $ nohup command
from the session
Used to send a signal to a $ kill [signal_info] PID
kill
particular process $ kill [signal_info] %JobID

18
Memory related Commands

Command Description Syntax & important variations


Prints file system disk space
df $ df [FILE]
usage

du Estimates file space use $ du [FILE]

df command du command
It will display the information about the
It will display the block used by all the
blocks of different file systems installed
directories and sub-directories available
on our system.
under the present working directory.
OR
OR
It will give the information about in
It will display the block used by the
which particular file system the
specified file or directory.
specified file or directory is available.

19
Compression Utilities
Compression Utilities

Command Description Syntax


 Utility for compression and
decompression of files $ gzip file1 [file2…]
gzip
 Compressed files have .gz $ gzip –d file1.gz
extension
 Used to view the content of
zcat $ zcat file1.gz
compressed file
 Used to view the content of
zmore $ zmore file1.gz
compressed file pagewise
 Utility for decompression of files
gunzip having extension .gz $ gunzip file1.gz
 Same as gzip –d
 Creates a tape archive file $ tar -cvf file1.tar file1
tar  Extracts from archive file $ tar -xvf file1.tar

21
Self Study: Compression Utilities
Demo

22
Can you answer these questions?

 Differentiate between bg and fg commands.


 Differentiate between df and du commands.
 Harry has a directory named “Dir1” containing 872
files. James wants to use Dir1. How will Harry send
this through mail to James?

23
Summary

 Text Processing Commands.


 Process related Commands
 Memory related Commands
 Compression Commands

24
Thank You

25

You might also like