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 Files
Section titled “Printing Files”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.
The lp and lpr Commands
Section titled “The lp and lpr Commands”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.txtrequest 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(forlp) or-P printername(forlpr): Specify a particular printer.-n num_copies(forlp) or-#num_copies(forlpr): Print multiple copies.
# Print 2 copies of status.log to the printer named 'laserjet4'$ lp -d laserjet4 -n 2 status.logChecking the Print Queue: lpstat and lpq
Section titled “Checking the Print Queue: lpstat and lpq”These commands show the status of print jobs in the queue.
lpstat: Provides detailed status information about printers and jobs.lpstat -ooften shows all jobs in the queue.lpq: Also shows the print queue, sometimes with slightly different formatting.
$ lpstat -olaserjet4-123 alice 10240 Jul 25 14:10 processinglaserjet4-124 bob 5120 Jul 25 14:11 pending
$ lpqlaserjet4 is ready and printingRank Owner Job File(s) Total Sizeactive alice 123 report.txt 10240 bytes1st bob 124 notes.txt 5120 bytesCanceling Print Jobs: cancel and lprm
Section titled “Canceling Print Jobs: cancel and lprm”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-124request "laserjet4-124" cancelled
# Cancel job 124 using the ID from lpq$ lprm 124You can typically only cancel your own print jobs unless you are the system administrator.
Sending Email with mail
Section titled “Sending Email with mail”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.
Sending a Simple Email
Section titled “Sending a Simple Email”Syntax for sending:
$ mail -s "Subject Line" recipient@example.comAfter 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.comHi Admin,
The system update process has completed successfully.
Regards,Automated Script.Cc: # Press Enter if no Cc neededYou 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.logReading Email (Basic)
Section titled “Reading Email (Basic)”To check for incoming mail delivered to your local Unix user account, simply type:
$ mailThis 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.
Limitations and Alternatives
Section titled “Limitations and Alternatives”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
muttoralpine.