Skip to content

Unix / Linux - Basic Utilities

Unix / Linux Basic Utilities - Printing and Email

Section titled “Unix / Linux Basic Utilities - Printing and Email”

This chapter covers some basic utility commands related to printing files and sending simple emails from the command line. While graphical interfaces are common, these command-line tools remain useful, especially in scripting and server environments.

Printing directly from the Unix/Linux command line is less common for end-users than it once was, but the underlying systems and commands are still relevant, particularly on servers or for automated tasks.

Modern Unix-like systems typically use CUPS (Common Unix Printing System) as the backend print server. The traditional commands lp and lpr often act as front-ends to CUPS.

The pr command, mentioned historically for basic text formatting before printing, is rarely used today. Modern tools or application-specific formatting are preferred.

These commands send a file to the print queue for printing. Your system administrator usually configures a default printer.

Syntax:

$ lp filename(s)
# or
$ lpr filename(s)

Example: Print the file report.txt to the default printer.

$ lp report.txt
request id is printer_name-123 (1 file(s))

The command usually returns a request ID, which can be used to manage the print job.

Common Options:

  • -d printername (for lp) or -P printername (for lpr): Specify a particular printer.
  • -n num_copies (for lp) or -#num_copies (for lpr): Print multiple copies.
# Print 2 copies of status.log to the printer named 'laserjet4'
$ lp -d laserjet4 -n 2 status.log

These commands show the status of print jobs in the queue.

  • lpstat: Provides detailed status information about printers and jobs. lpstat -o often shows all jobs in the queue.
  • lpq: Also shows the print queue, sometimes with slightly different formatting.
$ lpstat -o
laserjet4-123 alice 10240 Jul 25 14:10 processing
laserjet4-124 bob 5120 Jul 25 14:11 pending
$ lpq
laserjet4 is ready and printing
Rank Owner Job File(s) Total Size
active alice 123 report.txt 10240 bytes
1st bob 124 notes.txt 5120 bytes

To remove a job from the print queue, use cancel (often associated with lp) or lprm (often associated with lpr). You usually need the job ID.

# Cancel job laserjet4-124 using the ID from lpstat
$ cancel laserjet4-124
request "laserjet4-124" cancelled
# Cancel job 124 using the ID from lpq
$ lprm 124

You can typically only cancel your own print jobs unless you are the system administrator.

The mail utility provides a basic command-line interface for sending and reading emails. While functional for simple tasks and scripting, it lacks the features of modern graphical email clients (like Thunderbird, Outlook, Apple Mail) or advanced console clients (like mutt or alpine).

Important Note: For the mail command to successfully send email to external addresses (outside your own system), your Unix/Linux system needs a correctly configured Mail Transfer Agent (MTA), such as Postfix, Sendmail, or Exim. This configuration is often complex and beyond the scope of basic usage.

Syntax for sending:

$ mail -s "Subject Line" recipient@example.com

After running this command, mail will wait for you to type the body of the message. Press Enter after each line. To finish and send the email, type a single period (.) on a line by itself and press Enter, or press Ctrl + D.

$ mail -s "System Update Notification" admin@example.com
Hi Admin,
The system update process has completed successfully.
Regards,
Automated Script
.
Cc: # Press Enter if no Cc needed

You can also pipe content directly into the mail command or use input redirection:

# Pipe the output of a command as the email body
$ df -h | mail -s "Disk Usage Report" sysadmin@example.com
# Send the contents of a file as the email body
$ mail -s "Log File" errors@example.com < /var/log/app_error.log

To check for incoming mail delivered to your local Unix user account, simply type:

$ mail

This will present a list of messages and a simple prompt for reading, replying, deleting, etc. However, most users today manage their email through dedicated clients rather than the basic mail command’s reading interface.

The mail command is best suited for:

  • Sending simple notifications from scripts.
  • Quickly sending text-based messages without attachments or complex formatting.

For regular email usage, consider using:

  • Graphical email clients (Thunderbird, Evolution, Geary, etc.).
  • Webmail interfaces (Gmail, Outlook.com, etc.).
  • More capable console-based clients like mutt or alpine.