Monday 19 December 2011

Unix - Basic Utilities

So far you must have got some idea about Unix OS and nature of its basic commands. This tutorial would cover few very basic but important Unix utilities which you would use in your day to day life.

Printing Files:

Before you print a file on a UNIX system, you may want to reformat it to adjust the margins, highlight some words, and so on. Most files can also be printed without reformatting, but the raw printout may not look quite as nice.
Many versions of UNIX include two powerful text formatters, nroff and troff. They are not covered in this tutorial but you would quit a lot material on the net for these utilities.

The pr Command:

The pr command does minor formatting of files on the terminal screen or for a printer. For example, if you have a long list of names in a file, you can format it onscreen into two or more columns.
Here is the syntax of pr command:
pr option(s) filename(s)
The pr changes the format of the file only on the screen or on the printed copy; it doesn't modify the original file. Following table lists some pr options:
OptionDescription
-kProduces k columns of output
-d Double-spaces the output (not on all pr versions).
-h "header"Takes the next item as a report header.
-tEliminates printing of header and top/bottom margins.
-l PAGE_LENGTHSet the page length to PAGE_LENGTH (66) lines. Default number of lines of text 56.
-o MARGINOffset each line with MARGIN (zero) spaces.
-w PAGE_WIDTHSet page width to PAGE_WIDTH (72) characters for multiple text-column output only.
Before using pr, here are the contents of a sample file named food
[amrood]$cat food
Sweet Tooth
Bangkok Wok
Mandalay
Afghani Cuisine
Isle of Java
Big Apple Deli
Sushi and Sashimi
Tio Pepe's Peppers
........
[amrood]$
Let's use pr command to make a two-column report with the header Restaurants:
[amrood]$pr -2 -h "Restaurants" food
Nov  7  9:58 1997  Restaurants   Page 1

Sweet Tooth              Isle of Java
Bangkok Wok              Big Apple Deli
Mandalay                 Sushi and Sashimi
Afghani Cuisine          Tio Pepe's Peppers
........
[amrood]$

The lp and lpr Commands:

The command lp or lpr prints a file onto paper as opposed to the screen display. Once you are ready with formatting using pr command, you can use any of these commands to print your file on printer connected with your computer.
Your system administrator has probably set up a default printer at your site. To print a file named food on the default printer, use the lp or lpr command, as in this example:
[amrood]$lp  food
request id is laserp-525  (1 file)
[amrood]$
The lp command shows an ID that you can use to cancel the print job or check its status.
  • If you are using lp command, you can use -nNum option to print Num number of copies. Along with the command lpr, you can use -Num for the same.
  • If there are multiple printers connected with the shared network, then you can choose a printer using -dprinter option along with lp command and for the same purpose you can use -Pprinter option along with lpr command. Here printer is the printer name.

The lpstat and lpq Commands:

The lpstat command shows what's in the printer queue: request IDs, owners, file sizes, when the jobs were sent for printing, and the status of the requests.
Use lpstat -o if you want to see all output requests rather than just your own. Requests are shown in the order they'll be printed:
[amrood]$lpstat -o
laserp-573  john  128865  Nov 7  11:27  on laserp
laserp-574  grace  82744  Nov 7  11:28
laserp-575  john   23347  Nov 7  11:35
[amrood]$
The lpq gives slightly different information than lpstat -o:
[amrood]$lpq
laserp is ready and printing
Rank   Owner      Job  Files                  Total Size
active john       573  report.ps              128865 bytes
1st    grace      574  ch03.ps ch04.ps        82744 bytes
2nd    john       575  standard input         23347 bytes
[amrood]$
Here the first line displays the printer status. If the printer is disabled or out of paper, you may see different messages on this first line.

The cancel and lprm Commands:

The cancel terminates a printing request from the lp command. The lprm terminates lpr requests. You can specify either the ID of the request (displayed by lp or lpq) or the name of the printer.
[amrood]$cancel laserp-575
request "laserp-575" cancelled
[amrood]$
To cancel whatever request is currently printing, regardless of its ID, simply enter cancel and the printer name:
[amrood]$cancel laserp
request "laserp-573" cancelled
[amrood]$
The lprm command will cancel the active job if it belongs to you. Otherwise, you can give job numbers as arguments, or use a dash (-) to remove all of your jobs:
[amrood]$lprm 575
dfA575diamond dequeued
cfA575diamond dequeued
[amrood]$
The lprm command tells you the actual filenames removed from the printer queue.

Sending Email:

You use the Unix mail command to send and receive mail. Here is the syntax to send an email:
[amrood]$mail [-s subject] [-c cc-addr] [-b bcc-addr] to-addr
Here are important options related to mail command:
OptionDescription
-sSpecify subject on command line.
-cSend carbon copies to list of users. List should be a comma-separated list of names.
-bSend blind carbon copies to list. List should be a comma-separated list of names.
Following is the example to send a test message to amrood@gmail.com.
[amrood]$mail -s "Test Message" admin@yahoo.com 
You are then expected to type in your message, followed by an "control-D" at the beginning of a line. To stop simply type dot (.) as follows:
Hi,

This is a test
.
Cc: 
You can send a complete file using a redirect < operator as follows:
[amrood]$mail -s "Report 05/06/07" admin@yahoo.com < demo.txt 
To check incoming email at your Unix system you simply type email as follows:
[amrood]$mail
no email

No comments: