Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 100

UNIX and Shell Scripting

Module 4: UNIX Commands

Copyright © 2009 Accenture All Rights Reserved. Accenture, its logo, and Accenture High Performance Delivered are trademarks of Accenture.
Module 4 Objectives
• Upon completing this module, you will be able to:
– Describe how to execute the following UNIX commands
• General Purpose
• File Manipulation
• Text Processing
• Printing
• Network and Communication
• Compression/Decompression
• Process Management

Copyright © 2009 Accenture All Rights Reserved. 2


Module 4 Agenda
Topic Name Duration
General Purpose Commands 45 min

File Manipulation Commands 65 min

Text Processing Commands 75 min

Printing Commands 10 min


Network and Communication Commands 10 min

Compression/Decompression Commands 15 min

Process Management Commands 40 min

Copyright © 2009 Accenture All Rights Reserved. 3


Demonstration Instructions
• For slides 5-19

• This topic comprises General Purpose Command sections.


These sections will be conducted in the following manner:
– Instructions:
• Explain the command.
• Ask participants to do the same.
• Discuss the related slide.

– Note: The commands would be used by the faculty and participants


simultaneously.

Copyright © 2009 Accenture All Rights Reserved. 4


Using UNIX Help (1 of 2)
• The man command
– Formats and displays online manual pages
– Accepts the name which is normally the name of manual page,
which is typically the name of the command, function, or file
Command Function
$ man cat Displays the manual page for cat command

• To search the specified string in all manual pages:


Command Function
$ man -K ln Searches the word “ln” in all manual pages

Copyright © 2009 Accenture All Rights Reserved. 5


Using UNIX Help (2 of 2)
• To force display all the manual pages:
Command Function

$ man –a cat Displays all pages containing string cat

Copyright © 2009 Accenture All Rights Reserved. 6


Print Working Directory
• The pwd command
– Stands for print working directory of the user
Command Function
$ pwd If user10 is working in directory TestD1, it will
display /home/user10/TestD1.

Copyright © 2009 Accenture All Rights Reserved. 7


Display List of Files and Directories
• The ls command
– Gives the list of files and directories
Command Function
$ ls Lists all files and directories in present working directory

• To list all files and directories including hidden files and


directories:
Command Function
$ ls -a TestD1 TestF1 TestF2 TestF3 .TestHidden1 .TestHidden2

Files or directories starting with .(dot) are hidden files or


directories in UNIX.

Copyright © 2009 Accenture All Rights Reserved. 8


Display Long Listing in ASCII
• To display long listing in ASCII collating sequence showing
seven attributes of a file:

Number of Size of the file in


– $ ls –l
links to the file terms of Bytes
Total 72
drw_r_xrwx 1 user1 user1 123 May 10 13:45 TestD1 Name of the
_rw_r_xrwx 1 user1 user1 123 May 10 13:45 TestF1 file or Directory
_rw_r_xrwx 1 user1 user1 123 May 10 13:45 TestF2
_rw_r_xrwx 1 user1 user1 123 May 10 13:45 TestF3

Permissions of the Group which user Last modification


file or directory to belongs to date and time of
user, group, and file and directory
others
User who created
the file or directory

Copyright © 2009 Accenture All Rights Reserved. 9


Display Sorted File Names by Last
Modification Time
• $ ls -t
List of files and directories without –t option

• Shows most recently modified file first


• TestF1 is most recently modified.

Copyright © 2009 Accenture All Rights Reserved. 10


Create User Directories
• The mkdir command
– Allows to create directories
Command Function

$ mkdir TestD1 Creates the directory in present working


directory with the name TestD1
$ mkdir TestD2 TestD3 Creates the directory in present working
directory with the name TestD2,TestD3

Copyright © 2009 Accenture All Rights Reserved. 11


Remove Directory
• The rmdir command
– Allows to delete one or more directories
– Removes directory (the directory has to be empty)
Command Function
$ rmdir TestD1 Removes TestD1 directory present in current
directory
$ rmdir TestD2 TestD3 Removes TestD2 and TestD3 directories
present in current directory

Copyright © 2009 Accenture All Rights Reserved. 12


See List of Users Currently Logged in (1
of 2)
• The who command
– Shows the name of the users who are currently logged in
– Shows the terminal type and the time users logged in
– $ who
Name of the users currently logged in

user1 tty3a Jun 10 09:15


user2 tty3c Jun 10 09:25 Login date and time users logged in

Name of the terminal user is using

Copyright © 2009 Accenture All Rights Reserved. 13


See List of Users Currently Logged in (2
of 2)
• To display the names and number of users currently logged in.

Command Function
$ who -q • Displays only the names and number of users
currently logged in
– When this option works all other options do not work
with who.

Showing two users currently logged


in, root and abhishek.x.sharma

Copyright © 2009 Accenture All Rights Reserved. 14


Display Information about Current
Terminal
Command Function
$ who -m • Displays information about the current terminal

Showing abhishek has logged in from terminal


pts/2 on date and time Dec 11 10:22

Copyright © 2009 Accenture All Rights Reserved. 15


Locate Specific Programs on Prompt
• The which command
– Locates a command
– $ which ls

Showing ls command file is located in the


directory path /bin/ls

Copyright © 2009 Accenture All Rights Reserved. 16


Change Password
• The passwd command
– Allows to change the password

– $ passwd
(after pressing enter key you will receive following three prompts.)
• Current passwd :
• New passwd :
• Confirm new passwd :

Copyright © 2009 Accenture All Rights Reserved. 17


Move Around in File System (1 of 2)
• The cd command
– Stands for change directory
– Acts as primary command for moving around the file system
Command Function

$ cd TestD1 Moves to child directory TestD1. from current directory

$ cd .. Moves to the parent directory from TestD1 if you are in


TestD1

Copyright © 2009 Accenture All Rights Reserved. 18


Move Around in File System (2 of 2)

Command Function
$ cd ~ Moves to the HOME directory

Present Working Directory

Command cd ~ will take you to home directory of the user.

Copyright © 2009 Accenture All Rights Reserved. 19


Module 4 Agenda
Topic Name Duration
General Purpose Commands 45 min

File Manipulation Commands 65 min

Text Processing Commands 75 min

Printing Commands 10 min


Network and Communication Commands 10 min

Compression/Decompression Commands 15 min

Process Management Commands 40 min

Copyright © 2009 Accenture All Rights Reserved. 20


Demonstration Instructions
• For slides 22-36

• This topic comprises command sections. These sections will be


conducted in the following manner:
– Instructions:
• Explain the command.
• Ask participants to do the same.
• Discuss the related slide.

– Note: The commands would be used by the faculty and participants


simultaneously.

Copyright © 2009 Accenture All Rights Reserved. 21


Display Contents of File on Screen
(1 of 2)
• The cat command
– Is most frequently used command
– Has three related functions with regard to the text files
Command Function
$ cat TestF1 Displays the contents of file TestF1 on the screen

• To concatenate contents of a files on the screen.


Command Function
$ cat TestF1 TestF2 TestF3 Note: Another role of cat is concatenation.
• Displays the concatenated contents of
three files TestF1 TestF2 TestF3

Copyright © 2009 Accenture All Rights Reserved. 22


Display Contents of File on Screen
(2 of 2)
• To display numbers to the lines while displaying

Command Function
$ cat -n TestF4 Lists line numbers while displaying contents of
TestF4

Copyright © 2009 Accenture All Rights Reserved. 23


Create New File on Screen
Command Function
$ cat > TestF4 Creates a new file with the name TestF4. Press
ctl+d after enter key

Copyright © 2009 Accenture All Rights Reserved. 24


Copy Files or Directories
• The cp command
Command Function
$ cp TestF1 TestF5 Copies contents of the file TestF1 to TestF5.
If file TestF5 doesn’t exist it will get created
$ cp -i TestF1 TestF2 As an interactive option, prompts the user
before overwriting the destination file
$ cp Copies contents of TestF2 to the destination
/home/user10/TestF2 directory TestD1 with the same file name
/home/user10/TestD1
$ cp -r Copies contents of TestD1 and its
/home/user10/TestD1 subsequent directories to the destination
/home/user10/TestD2 directory TestD2 with the same names

Copyright © 2009 Accenture All Rights Reserved. 25


Rename File or Directory
• The mv command
– Renames file or directory
– Moves a group of files to different directory
Command Function
$ mv TestF1 TestFNew Renames the file TestF1 with TestFNew
$ mv TestFNew TestF2 Moves the files TestFNew TestF2 TestF3 to
TestF3 TestD1 directory TestD1

• To confirm before renaming file or directory


Command Function
$ mv -i TestT2 TestFNew Prompts the user before overwriting the file
TestFNew

Copyright © 2009 Accenture All Rights Reserved. 26


Delete File
• The rm command
– Allows us to delete one or more files
Command Function
$ rm TestFNew Removes the file TestFNew

• To confirm from user to delete one or more file(s)


Command Function
$ rm -i TestF2 TestF3 Prompts the user for confirmation before
removing each file
$ rm -r TestF4 Performs a recursive search for all
subdirectories and files within these
subdirectories and removes all of them

Copyright © 2009 Accenture All Rights Reserved. 27


Locate File Based on Search Criteria
• The find command
– Locates files which meet the search criteria
Command Function
$ find . -name TestF1 Searches file TestF1 from current directory
onwards and prints its path

Copyright © 2009 Accenture All Rights Reserved. 28


Compare Two Files
• The cmp command
– Compares two files byte by byte
– Displays the location of the first mismatch
Command Function
$ cmp TestF1 TestF1 • Compares TestF2 and TestF3
– Will get result like TestF1 TestF2 differ : char
27, line 1
$ cmp TestFNew TestF2 • Compares TestFNew and TestF2
– Will not display any message, simply return
prompt if two files are identical

Copyright © 2009 Accenture All Rights Reserved. 29


Associate One File with Another
• The ln –s command
– Creates symbolic link between files
– Soft link is a synonym to symbolic link

Command Function
$ ln –s TestF2 TestFln Creates a soft link TestFln to file TestF2

Copyright © 2009 Accenture All Rights Reserved. 30


Page Through File
• The less command
– Does not read the entire file
Command Function
$ less TestLess Displays contents of file TestLess in pagewise manner

• To scroll forward in the file


– ^D option of less command
• Scrolls forward (Default one half of the screen)

• To scroll backward in the file


– ^B option of less command
• Scrolls backward (Default one half of the screen)

Copyright © 2009 Accenture All Rights Reserved. 31


Display First Few Lines of File
• The head command
– Displays first few lines of the file
Command Function
$ head TestF1 Displays the first 10 lines of the file TestF1, if
TestF1 has more then 10 lines; otherwise all lines
$ head -2 TestF1 Displays the first two lines of the file TestF1, where -
2 represents any positive number n

Copyright © 2009 Accenture All Rights Reserved. 32


Display Last Few Lines of File
• The tail command
– Reads last few lines of any text given and writes them on the screen
Command Function
$ tail TestF1 Displays the last 10 lines of the file TestF1, if TestF1
has more then 10 lines otherwise all lines
$ tail -2 TestF1 Displays the last two lines of the file TestF1, where -
2 represents any number n and can be negative or
positive

Copyright © 2009 Accenture All Rights Reserved. 33


Change Mode (Permission) of File or
Directory (1 of 3)
• The chmod command
– Allows to change the mode (permission) of files and directories

• There are three permissions to a file


– r – read (octal equivalent 4)
– w – write (octal equivalent 2)
– x – execute (octal equivalent 1)

• There are three entities to which any combination of these


permissions are assigned:
– User (owner)
– Group
– Others

Copyright © 2009 Accenture All Rights Reserved. 34


Change Mode (Permissions) of File or
Directory (2 of 3)
• The chmod command
– Assuming _rw_r_ _r_ _ are the permissions for the file TestF1 from
the long listing, they signify that:
• Owner can read and write the file but can’t execute the file TestF1
• Members of the group can read the file, but cannot write into the file or
execute the file TestF1
• All others can only read the file TestF1

Copyright © 2009 Accenture All Rights Reserved. 35


Change Mode (Permissions) of File or
Directory (3 of 3)
Command Function
$ chmod 700 TestF1 Changes the existing permission of file
TestF1 to rwx for user and removes all
permissions for group and others
$ chmod go+r TestF1 Assigns permission r to group and others
$ chmod go+w,go-r TestF1 Assigns permission w to group and others
and removes r permission for group and
others for file TestF1
$ chmod -R go+rwx TestD1 Assigns permission r, w, and x to group
and others for directory TestD1 as well as
all files and subdirectories within it

Copyright © 2009 Accenture All Rights Reserved. 36


Knowledge Check (1 of 3)
• If f1 and f2 are directories, explain how the command mv f1 f2
behaves when:

1. f2 exists
2. f2 does not exist

Copyright © 2009 Accenture All Rights Reserved. 37


Knowledge Check (2 of 3)
• Assuming that you are positioned in the directory /home/user1,
what are the following commands presumed to do?
• Additionally, explain whether they will work at all.

1. cd ../..
2. mkdir ../bin
3. rmdir ..
4. ls ..

Copyright © 2009 Accenture All Rights Reserved. 38


Knowledge Check (3 of 3)
• Find all files in the present directory onwards, starting with the
name T.

Copyright © 2009 Accenture All Rights Reserved. 39


Activity 1: General Purpose and File
Manipulation Commands
• Activity time: 10 minutes
– Use General Purpose and File Manipulation Commands

• Instructions:
– Create a file named TestFileCommand with at least ten lines and
perform the following tasks on the file:
• Rename the file TestFileCommand to cmdTest.
• Change the permissions of the file for following:
– User – read, write, and execute
– Group – read
– Others – read and execute
• List the first three lines of the file.
• List all the lines after line four.
• Create a soft link to the file cmdTest with name cmdTestLn.

Copyright © 2009 Accenture All Rights Reserved. 40


Module 4 Agenda
Topic Name Duration
General Purpose Commands 45 min

File Manipulation Commands 65 min

Text Processing Commands 75 min

Printing Commands 10 min


Network and Communication Commands 10 min

Compression/Decompression Commands 15 min

Process Management Commands 40 min

Copyright © 2009 Accenture All Rights Reserved. 41


Demonstration Instructions
• For slides 43-65

• This topic comprises command sections. These sections will be


conducted in the following manner:
– Instructions:
• Explain the command.
• Ask participants to do the same.
• Discuss the related slide.

– Note: The commands would be used by the faculty and participants


simultaneously.

Copyright © 2009 Accenture All Rights Reserved. 42


Using Pipe (1 of 2)
• Accomplishes complex tasks with ease by combining UNIX
commands

• Combines UNIX commands through pipes and filters

• Using pipe
– The symbol | is the UNIX pipe symbol.
• Is used on the command line
• Means that the standard output of the command to the left of the pipe
gets sent as standard input of the command to the right of the pipe

Copyright © 2009 Accenture All Rights Reserved. 43


Using Pipe (2 of 2)
• Using pipe
– Example
• $ who | sort | lpr
who | sort | lpr
 who command process writes to pipe1
 sort command process reads from pipe1, writes to pipe2
 lpr command process reads from pipe2

who Process sort Process lpr Process


readfd readfd
writefd writefd

pipe1 pipe2
Data flow

Kernel
Copyright © 2009 Accenture All Rights Reserved. 44
Using Filter
• A filter:
– Is a UNIX command that does some manipulation of the text of a file

pump pipe filter pipe filter pipe sink

– Transforms or filters the data it receives via the pipes with which it is
connected

– Can have any number of input and output pipes

Copyright © 2009 Accenture All Rights Reserved. 45


Search for Specific Pattern in Files
(1 of 2)
• The grep command
– Stands for global search for regular expression and print
– Functions as standard file searching and selection utility
Command Function
$ grep Accenture TestF1 • Searches string “Accenture” in file
TestF1 and displays those lines
$ grep Accenture TestF1 TestF2 • Searches string “Accenture” in file
TestF1 and TestF2 and displays
those lines
• Displays the name of the file as well
$ grep ‘Accenture is a’ TestF1 • Searches pattern comprising more
than a single word
–Note: Double quotes can be used to
enclose the same.

Copyright © 2009 Accenture All Rights Reserved. 46


Search for Specific Pattern in Files
(2 of 2)
• To search for the specific pattern in one or more files without
heeding the case
Command Function
$ grep Accenture -i TestF1 Searches for the pattern without
heeding the case.
Note: –i option makes it case
insensitive.

Copyright © 2009 Accenture All Rights Reserved. 47


Usages of grep Filter
Command Function
$ cat TestF1 | grep Accenture Sends the contents of file TestF1 to
standard input using pipe. grep takes
input from standard input in place of
file, searches “Accenture”, and
displays the lines which contain it.
$ ls -l /tmp | grep 'root' Shows all processes with the word
"root" somewhere in the line. That
doesn't mean that all the process
would be owned by root. However,
by using grep filter we can cut down
the number of processes we need to
look at.

Copyright © 2009 Accenture All Rights Reserved. 48


Get the Difference Between Two Files
(1 of 2)
• The diff command
– Displays file differences
• $ diff TestDiff1 TestDiff2

Copyright © 2009 Accenture All Rights Reserved. 49


Get the Difference Between Two Files
(2 of 2)
• $ diff TestDiff1 TestDiff2
Represents appended line> 101 Haleem ASE

Represents changed lines 8-11 of TestDiff1 with 9,10 of file TestDiff2

Represents deleted line< 101 Abhishek SSE


Copyright © 2009 Accenture All Rights Reserved. 50
Ignore Case
• To ignore case while getting difference between two files
– $ diff -i TestDiff1 TestDiff2

Due to the use of –i option, case of Represents the difference due to


the letters Rachit is ignored. the case of the letters rachit

Copyright © 2009 Accenture All Rights Reserved. 51


Ignore Trailing Blanks
• To ignore the trailing blanks while getting difference between
two files
– $ diff -b TestDiff3 TestDiff4

Without –b option diff identifies blank Causes blanks (spaces and tabs) to
spaces or tabs as difference. compare equally even if an unequal
number of blanks exist.
Copyright © 2009 Accenture All Rights Reserved. 52
Sort Contents of File
• The sort command
– Sorts ASCII (Text) files
– Sorts according to the ASCII collating sequence, i.e. sorts the
spaces and the tabs first, then the punctuation marks followed by
numbers, uppercase letters, and lower case letters
Command Function
$ sort TestF1 Sorts the contents of TestF1 and
displays the sorted output on screen
$ sort TestF1 TestF2 Sorts the contents of TestF1 and
TestF2 respectively and displays the
result on screen

Copyright © 2009 Accenture All Rights Reserved. 53


Sort File Numerically
Command Function
$ sort -n TestNSort Sorts file TestNSort on numbers in place of ASCII
values

Sort without –n Option Sort with –n Option

Copyright © 2009 Accenture All Rights Reserved. 54


Usages of Sort Filter
Command Function
$ ls | sort Gets the input from the input stream and displays
list of files in sorted order
$ cat TestF1 | sort Pipes the contents of TestF1 to sort

Copyright © 2009 Accenture All Rights Reserved. 55


Remove Duplicate Contents from File
• The uniq command
– Simply fetches one copy of each line and writes it to the standard
output
– Requires a sorted file as input
Command Function
$ uniq result Fetches one copy of each line and writes it on
screen

• To select non-repeated lines from a file


Command Function
$ uniq -u result Fetches unique line from result

Copyright © 2009 Accenture All Rights Reserved. 56


Usages of uniq Filter
Command Function
$ sort TestF1 | uniq Sort pipes the output to uniq which fetches one
copy of each line and writes to standard output
$ cat result | uniq -u Cat pipes the sorted output to uniq which selects
the lines which are non repeated

Copyright © 2009 Accenture All Rights Reserved. 57


Find Number of Words in File
• The wc command
– Stands for word count
– Displays count of words, lines, and characters
Command Function
$ wc TestF1 Displays number of lines, words, and
5    13    57  TestF1 characters in the file TestF1. This is the
standard representation
$ wc -w TestF1 Displays number of words in the file
13 TestF1 TestF1

Copyright © 2009 Accenture All Rights Reserved. 58


Find only the Number of Lines in File
Command Function
$ wc -l TestF1 Displays Number of lines in the file TestF1
5 TestF1

Copyright © 2009 Accenture All Rights Reserved. 59


Usages of wc Filter
Command Function
$ who | wc –l Counts the number of users logged into your
computer system
– The output of the who command is piped into wc
command, which counts the number of lines in the
output of who
$ ls -l | wc -l Counts the number of files and directories
present in your current working directory

Copyright © 2009 Accenture All Rights Reserved. 60


Remove Sections from File on Prompt
• The cut command
– From each line of file displays selected
• Columns
• Fields
Command Function
$ cut -c 3-10,15-20 TestF1 Cuts characters 3 to 10 and 15 to 20, and
displays that on standard output

Copyright © 2009 Accenture All Rights Reserved. 61


Remove Selected Fields from File on
Prompt
• Default field delimiter (Tabs and Spaces)
• Different delimiter (can also be used)
– Two options need to be used
• -d for the field delimiter
• -f for the field lists

Command Function

$ cut -d “ “ -f 2,4 TestF1 Extracts 2nd and 4th the fields from the file
TestF1 and displays them on standard
output

Copyright © 2009 Accenture All Rights Reserved. 62


Usages of cut Filter
Command Function

$ who | cut -d “ “ -f 1 • Extracts the users’ list logged into your


computer system
• Output of who command is piped to cut
command, which extracts the first field i.e.
user name.
$ ls | cut -d “ “ -f 1,3,5,7 • Extracts the file permissions, user name,
file size, and name
• Output of ls command is piped to cut
command, which extracts fields 1,3,5,7
from the output of ls command.

Copyright © 2009 Accenture All Rights Reserved. 63


Paste Sections from Two Files on
Prompt
• The paste command
– Pastes vertically rather than horizontally
– Displays two or more files adjacently by pasting them
Command Function

$ cut -d “ “ -f 2,5 TestF1 > cutList1 Extracts fields 2 and 5 from file
TestF1 and sends to cutList1

$ cut -d “ “ -f 3,6 TestF2 > cutList2 Extracts fields 3 and 6 from file
TestF2 and sends to cutList2
$ paste cutLsit1 cutList2 Pastes two files cutList1, cutList2
vertically
–In the output original lines are
separated by TABs by default.
$ paste -d “|” cutList1 cutList2 Paste uses the tab as the default
delimiter, we can specify one or
more delimiter with –d option.
Copyright © 2009 Accenture All Rights Reserved. 64
Merge Two Files on Prompt
• The join command
– A join of the two relations specified by the lines of file1 and file2
– Files are joined on a common key field (column) that should exist in
both files.
– Both files must be sorted on the key field in the same order.
file1 file2
01123 HP 9000/825 01123 Hewlett Packard
02213 Sun 3/110 02213 Sun Microsystems
03320 Sun 4/110 03321 Sun Microsystems
08412 HP 9000/835 08412 Hewlett Packard

– $ join -t"\t" file1 file2


• 01123 HP 9000/825 Hewlett Packard
• 02213 Sun3/110 Sun Microsystems
• 08412 HP 9000/835 Hewlett Packard

Copyright © 2009 Accenture All Rights Reserved. 65


Knowledge Check (1 of 2)
• What is the function of the following commands?

1. grep a b c
2. grep <Test> TestFile

Copyright © 2009 Accenture All Rights Reserved. 66


Knowledge Check (2 of 2)
• Show the octal representation of the following permissions:

1. rwxr-xrw-
2. rw-r-----
3. --x-w-r--

Copyright © 2009 Accenture All Rights Reserved. 67


Activity 2: Filter Command
• Activity time: 10 minutes
– Use appropriate filter command to accomplish the given task

• Instructions:
– Create a file named TestGrep1 and TestGrep2, add ten to fifteen
lines in each file with at least ten words separated by | (pipe sign) in
each line
– Perform the following operations on the file:
• Search for the string accenture or Accenture.
• Cut the column 2-3 and 5-7 respectively from file TestGrep1.
• Paste the deleted columns on the screen.
• Find out the difference between both files and make changes to make
them identical.

Copyright © 2009 Accenture All Rights Reserved. 68


Module 4 Agenda
Topic Name Duration
General Purpose Commands 45 min

File Manipulation Commands 65 min

Text Processing Commands 75 min

Printing Commands 10 min


Network and Communication Commands 10 min

Compression/Decompression Commands 15 min

Process Management Commands 40 min

Copyright © 2009 Accenture All Rights Reserved. 69


Demonstration Instructions
• For slides 71-73

• This topic comprises command sections. These sections will be


conducted in the following manner:
– Instructions:
• Explain the command.
• Ask participants to do the same.
• Discuss the related slide.

– Note: The commands would be used by the faculty and participants


simultaneously.

Copyright © 2009 Accenture All Rights Reserved. 70


Print Contents of File
• The lpr command
– Prints files from the command prompts
Command Function

$ lpr TestF1 Sends the file TestF1 for printing

Copyright © 2009 Accenture All Rights Reserved. 71


Cancel Jobs
• To cancel the jobs that have been queued for printing

– The lprm command

• $ lprm
• $ lprm 295

Removes the job with id 295

Copyright © 2009 Accenture All Rights Reserved. 72


Check Print Queue on Prompt
• The lpq command
– Shows the current print queue status on the named printer
• $ lpq

Copyright © 2009 Accenture All Rights Reserved. 73


Module 4 Agenda
Topic Name Duration
General Purpose Commands 45 min

File Manipulation Commands 65 min

Text Processing Commands 75 min

Printing Commands 10 min


Network and Communication Commands 10 min

Compression/Decompression Commands 15 min

Process Management Commands 40 min

Copyright © 2009 Accenture All Rights Reserved. 74


Demonstration Instructions
• For slides 76-78

• This topic comprises command sections. These sections will be


conducted in the following manner:
– Instructions:
• Explain the command.
• Ask participants to do the same.
• Discuss the related slide.

– Note: The commands would be used by the faculty and participants


simultaneously.

Copyright © 2009 Accenture All Rights Reserved. 75


Send Packets
• To send packets to a machine known to be in the network
– The ping command
• On not receiving any reply packets, ping will exit with code 1
• On error it exits with code 2
• Otherwise it exits with code 0
• Intended for use in network testing, measurement, and management
– $ ping 10.116.69.12

Packet Received 0
indicates that there is no
connectivity between the
local and remote hosts.

Copyright © 2009 Accenture All Rights Reserved. 76


Connect to Machine
• To connect to a machine, if we have account on a host in the
local network
– The telnet command
• Used to communicate with another host using the telnet protocol
– $ telnet 10.116.69.12

After supplying command trying to connect

If connects successfully, asks for Login:

If Host is not getting connected

Copyright © 2009 Accenture All Rights Reserved. 77


Print Network Connections on Prompt
• The netstat command
– $ netstat

Copyright © 2009 Accenture All Rights Reserved. 78


Module 4 Agenda
Topic Name Duration
General Purpose Commands 45 min

File Manipulation Commands 65 min

Text Processing Commands 75 min

Printing Commands 10 min


Network and Communication Commands 10 min

Compression/Decompression Commands 15 min

Process Management Commands 40 min

Copyright © 2009 Accenture All Rights Reserved. 79


Demonstration Instructions
• For slides 81-84

• This topic comprises command sections. These sections will be


conducted in the following manner:
– Instructions:
• Explain the command.
• Ask participants to do the same.
• Discuss the related slide.

– Note: The commands would be used by the faculty and participants


simultaneously.

Copyright © 2009 Accenture All Rights Reserved. 80


Create New Archive
• The tar command
– Stands for tape archive
– Writes archives directly to tape devices
– Creates new archives
– Lists files in existing archives
– Extracts files from archives

Command Function
$ tar -cvf archive.tar TestF1 TestF2 • Creates archive file archive. tar for files TestF1
and TestF2
• Additionally, use of –v (verbose) option displays
the progress while tar works.
• To create archive, we need to specify the name of
the archive (with –f), the copy, or write operation (-
c), and the filenames.

Copyright © 2009 Accenture All Rights Reserved. 81


Extract Archive File
Command Function

$ tar -xvf archive.tar tar uses –x option to extract files from an archive

Copyright © 2009 Accenture All Rights Reserved. 82


Compress and Expand Compressed File
• The gzip and gunzip command
– gzip – compresses the file
– gunzip – expands compressed file
• $ gzip TestGipTry
Create file TestGipTry.gz

Copyright © 2009 Accenture All Rights Reserved. 83


Compression/Decompression
Commands
• $ gunzip TestGipTry.gz

Copyright © 2009 Accenture All Rights Reserved. 84


Module 4 Agenda
Topic Name Duration
General Purpose Commands 45 min

File Manipulation Commands 65 min

Text Processing Commands 75 min

Printing Commands 10 min


Network and Communication Commands 10 min

Compression/Decompression Commands 15 min

Process Management Commands 40 min

Copyright © 2009 Accenture All Rights Reserved. 85


Demonstration Instructions
• For slides 87-93

• This topic comprises command sections. These sections will be


conducted in the following manner:
– Instructions:
• Explain the command.
• Ask participants to do the same.
• Discuss the related slide.

– Note: The commands would be used by the faculty and participants


simultaneously.

Copyright © 2009 Accenture All Rights Reserved. 86


Print Processes (1 of 3)
• To print currently active processes on prompt
– The ps command
• Stands for process statistics
• Produces a report summarizing execution statistics for current processes

Command Function

$ ps By default displays all active processes

Copyright © 2009 Accenture All Rights Reserved. 87


Print Processes (2 of 3)
• $ ps

The terminal with which the The cumulative process


process is associated (the time (TIME) that has been
controlling terminal). consumed since the
process has started.

Each process CMDCMD is the


PID   TTY   TIME  
assigned a unique process
6874  pts/9   0:00     kshname.
ID known as PID.
6877  pts/9   0:01     csh
418    pts/9   0:00     csh

Copyright © 2009 Accenture All Rights Reserved. 88


Print Processes (3 of 3)
• To print detail information of processes on the prompt

• $ ps –f
Parent Process ID

Owner ID of Process Time the process started

UID PID PPID C STIME TTY TIME CMD


root 0 0 0 09:36:35 ? 0:00 sched
root 1 0 0 09:36:35 ? 0:02 /etc/init

Copyright © 2009 Accenture All Rights Reserved. 89


Kill Process on Prompt
• The kill command
– The only way to abort a command or process while it is executing, is
to send it a certain kind of `signal‘, which can be sent in two ways:
• A single keystroke
• `kill' command

• $ kill 2880

Copyright © 2009 Accenture All Rights Reserved. 90


Run Processes in Background
• The & option with commands
– Multitasking system lets a user do more than one job a time
– Only one job can run in the foreground
– Rest all have to run in the background
– & is the shell operator used to run a process in the background
• $ sort -oresult TestDiff1 &

The PID of the invoked command

Status of the
background process
after completion

Copyright © 2009 Accenture All Rights Reserved. 91


Execute Job (1 of 2)
• To execute a job at a particular time on the prompt
– The at command
• Schedules a command to be run at a particular time
• Takes time as an input argument upon which command to be executed
• Sends all jobs go to the queue
• Executes jobs only once
Job Submitted

Job Executed

Copyright © 2009 Accenture All Rights Reserved. 92


Execute Job (2 of 2)
• To execute a job repeatedly at particular time on the prompt
– The crontab command
• Runs job automatically at regular interval
Job Submitted

Job Executed

Copyright © 2009 Accenture All Rights Reserved. 93


Knowledge Check (1 of 3)
• Of the following options, specify the name of command for
running process at specified time:

1. ps
2. ls
3. at
4. Batch

Copyright © 2009 Accenture All Rights Reserved. 94


Knowledge Check (2 of 3)
• Which of the following options will you choose to send any
process to background?

1. ps
2. at
3. &
4. kill

Copyright © 2009 Accenture All Rights Reserved. 95


Knowledge Check (3 of 3)
• List all the processes currently running.

– Identify the PPID of the background process created for sort


command.

Copyright © 2009 Accenture All Rights Reserved. 96


Activity 3: Compression/Decompression
Commands
• Activity time: 5 minutes
– Identify and apply the compression/decompression commands to
perform given operation

• Instructions:
– Create a file TestCmdG1 and TestCmdG2 with at least ten lines in
both the files
– Perform the following operations:
• Compress both the files in current working directory.
• Create archive file with the name TestCmdArc.tar containing both the
files.
• Extract the zip files.
• Extract the tar file TestCmdArc.tar.

Copyright © 2009 Accenture All Rights Reserved. 97


Activity 4: Background Process
• Activity time: 5 minutes
– Execute the command required for sending sort operation to
background

• Instructions:
– Perform sort operation on file TestCmdG1 and send it to the
background.

Copyright © 2009 Accenture All Rights Reserved. 98


Module Summary
• Upon completing this module, you should now be able to:
– Describe how to execute the following UNIX commands –
• General Purpose
• File Manipulation
• Text Processing
• Printing
• Network and Communication
• Compression/Decompression
• Process Management

Copyright © 2009 Accenture All Rights Reserved. 99


Questions and Comments
• What questions or comments
do you have?

Copyright © 2009 Accenture All Rights Reserved. 100

You might also like