Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

UNIX utility


May 23, 2021 UNIX Getting started


Table of contents


Utility

So far you've definitely got some general understanding of the UNIX system and some basic ways to use commands. This tutorial will cover some very basic but important UNIX utilities.

Print the file

In a UNIX system, before you print a file, you may want to reformatt it to adjust its margins, highlight some words, and so on. Most files can also be printed without reformatting, but unprocessed printing may not look so good.

Many versions of the UNIX system contain two powerful text formatting commands, nroff, and troff. They are not included in this tutorial, but you can find a lot of information online about how to use these two commands.

the pr command

The pr command can format files displayed on the terminal display or printer by a small amount. For example, if you have a long list of names in your file, you can format it into two columns or display them on the screen.

Here's an example of the syntax of the pr command:

pr option(s) filename(s)

The pr command simply formats the copy file that is displayed on the screen or printed, and it does not modify the source file. The following list shows some optional pr in the pr command:

Operation Describe
-k Produces the output of the k column
-d Separate the output with two spaces (not all pr versions apply).
-h "header" Use the next item as header information.
-t Remove the printed head and top/bottom margins.
-l PAGE_LENGTH Set the number of rows of data stored on a page to PAGE_LENGTH (66). The default number of lines of text is 56 lines.
-o MARGIN Set the interval between each row to MARGIN (0) spaces.
-w PAGE_WIDTH Set the number of characters on a page line to PAGE_WIDTG (72) characters. This parameter is only available for multi-text column output.

Before using the pr command, look at the contents of the food file as follows:

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

Let's then use the pr command to turn the output into two columns with Restaruants displayed at the header :

$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
........
$

lp and lpr commands

Command lp or lpr prints the file on paper instead of displaying it on the screen. Once you're ready to format text with the pr commands, you can use them to print your files on any printer connected to your computer.

Your system administrator may have established a site as the default printer. In order to print a file named food on the default printer, you can use the lp or lpr command, as follows:

$lp food
request id is laserp-525  (1 file)
$

The lp command shows the printer's ID, which you can use to cancel a print job or check its status.

  • If you are using the lp command, you can use the -nNum option parameter to set the number of copies of the printed copy. For the lpr command, you can also use the parameter -Num to do the same.
  • If more than one printer is connected to a shared network, you can use the -dprinter parameter for the lp command to select the printer you want to use, and for the lpr command you can use the -Pprinter parameter to achieve the same effect. Here printer is worth the name of the printer.

lpstat and lpg commands

The lpstat command appears in the printer queue for jobs: the requested ID, owner, file size, and when the print task is sent to the printer, the requested status is also sent to the printer.

If you want to see all the output requests, not just your own, pstat -o command. Requests are displayed in the order in which they will be printed:

$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
$

The information displayed by lpg is slightly different from that shown by lpstat -o:

$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
$

The status of the printer is displayed on the first line. If the printer is disabled or the paper is used up, you can see different information on the first line.

Cancel and lprm commands

The cancel command terminates the print request made by the lp command. lprm command terminates the printing request made by lpr. You can specify the printer's ID (request made by lp or lpq) or name to terminate the print task.

$cancel laserp-575
request "laserp-575" cancelled
$

To cancel a task that is currently printing, you can ignore its ID and simply enter the name of the cancel command and printer:

$cancel laserp
request "laserp-573" cancelled
$

The lprm command will cancel the activity if it belongs to you. Otherwise, you can use the work number as an argument to the command, or use dashes (-) to delete all your work:

$lprm 575
dfA575diamond dequeued
cfA575diamond dequeued
$

The lprm command will tell you the file name of the task that was removed from the printer queue.

Send a message

You can use unIX mail commands to send and receive messages. Here's the syntax for sending an e-mail message:

$mail [-s subject] [-c cc-addr] [-b bcc-addr] to-addr

Here are the important parameters in the mail command:

Parameters Describe
-s Specify the subject of the message on the command line.
-c Send a copy to the user in the list. The list of users is a list of user names separated by commas.
-b Send a copy of the document to the user in the list. The lists are separated by commas.

The following is an example of sending a test message [email protected].

$mail -s "Test Message" [email protected] 

Next, enter your message section, which is after the "control-D" at the top of the line. If you want to end, you only need to enter one point type (.), as follows:

Hi,
This is a test
.
Cc: 

You can send a complete file by taking advantage of the < as follows:

$mail -s "Report 05/06/07" [email protected] < demo.txt 

To check for messages received, you can simply enter the following commands in the UNIX system:

$mail
no email