Unix / Linux Basic Utilities - Printing and Email

Hello, aspiring programmers! Today, we're going to dive into some essential Unix/Linux utilities that you'll find incredibly useful in your journey. As your friendly neighborhood computer teacher, I'm excited to guide you through the world of printing files and sending emails using the command line. Don't worry if you've never touched a terminal before – we'll start from the very basics and work our way up. Let's get started!

Unix / Linux - Basic Utilities

Printing Files

Printing files might sound like a mundane task, but trust me, knowing how to do this from the command line can be a real time-saver. Let's explore the different ways to print files in Unix/Linux systems.

The 'lp' Command

The 'lp' command is your go-to utility for printing files. It stands for "line printer," a term that dates back to the early days of computing when printers literally printed line by line. Nowadays, it works with all types of printers.

Here's the basic syntax:

lp [options] filename

Let's look at some examples:

  1. Print a file named "report.txt":

    lp report.txt

    This command will send the file to the default printer.

  2. Print multiple files:

    lp file1.txt file2.txt file3.txt

    You can list as many files as you want, separated by spaces.

  3. Specify a printer:

    lp -d myprinter report.txt

    This sends the file to a printer named "myprinter" instead of the default.

  4. Print multiple copies:

    lp -n 3 report.txt

    This will print three copies of the file.

The 'lpstat' Command

The 'lpstat' command is your printing status checker. It provides information about printers and print jobs.

Here are some useful examples:

  1. List all available printers:

    lpstat -p
  2. Check the status of all print jobs:

    lpstat -o
  3. Get detailed information about a specific printer:

    lpstat -p myprinter -l

The 'cancel' Command

Made a mistake? No worries! The 'cancel' command lets you cancel print jobs.

Basic syntax:

cancel [options] [job-id]

Example:

cancel 123

This cancels the print job with ID 123.

Sending Email

Now, let's move on to sending emails from the command line. It might seem old-school, but it's incredibly powerful for automation and quick communications.

The 'mail' Command

The 'mail' command is your Swiss Army knife for sending emails from the terminal.

Basic syntax:

mail [options] recipient

Let's look at some examples:

  1. Send a simple email:

    echo "Hello, this is a test email" | mail -s "Test Subject" [email protected]

    This sends an email with the subject "Test Subject" and the body "Hello, this is a test email" to [email protected].

  2. Send an email with an attachment:

    mail -s "Report Attached" -a report.pdf [email protected] < email_body.txt

    This sends an email with the subject "Report Attached", attaches the file "report.pdf", and uses the content of "email_body.txt" as the email body.

  3. Send to multiple recipients:

    echo "Meeting at 3 PM" | mail -s "Reminder" [email protected] [email protected] [email protected]

    This sends the same email to multiple recipients.

The 'mailx' Command

'mailx' is an enhanced version of 'mail' with more features. Its usage is similar to 'mail', but it offers more flexibility.

Example:

echo "Content of the email" | mailx -s "Subject" -a attachment.pdf -c [email protected] [email protected]

This sends an email with a subject, attachment, and CC recipient.

Putting It All Together

Now that we've covered the basics of printing and emailing, let's look at a practical example that combines both:

#!/bin/bash

# Generate a report
echo "Daily Report" > report.txt
date >> report.txt
echo "Sales: $1000" >> report.txt

# Print the report
lp -d officeprinter report.txt

# Email the report
cat report.txt | mail -s "Daily Sales Report" [email protected]

echo "Report printed and emailed successfully!"

This script generates a simple report, prints it to the office printer, and emails it to the boss. Pretty neat, right?

Conclusion

Congratulations! You've just taken your first steps into the world of Unix/Linux utilities for printing and emailing. Remember, practice makes perfect, so don't be afraid to experiment with these commands. They might seem a bit intimidating at first, but soon they'll become second nature.

As we wrap up, here's a quick table summarizing the commands we've learned:

Command Purpose Example
lp Print files lp report.txt
lpstat Check printer status lpstat -p
cancel Cancel print jobs cancel 123
mail Send email echo "Hello" \| mail -s "Subject" [email protected]
mailx Enhanced email sending mailx -s "Subject" -a file.pdf [email protected]

Keep exploring, keep learning, and before you know it, you'll be a command-line wizard! Until next time, happy coding!

Credits: Image by storyset