lp Command in Linux



The lp command in Linux handles printing tasks from the command line, making it easy to print documents directly from the terminal. It supports a wide range of options that allow users to adjust print settings such as page orientation, margins, and the number of pages printed per sheet. This command is ideal for both basic and advanced printing needs, as it provides flexibility and control over print tasks.

In this tutorial, we'll explore the command's key functionalities, syntax, and options, followed by detailed examples of its practical uses.

Table of Contents

Here is a comprehensive guide to the options available with the lp command −

What is lp Command in Linux?

The "lp" command (stands for "line printer") is a command-line tool that allows users to print files efficiently in UNIX and Linux environments. It operates under the Common UNIX Printing System (CUPS), which standardizes printing tasks across different Linux distributions.

Syntax of lp Command

The basic syntax of the lp command is as follows −

lp [options] [file...]

Here, file... represents one or more files to print.

lp Command Options

The lp command accepts several options, as shown in the following table. You can use any option with the lp command to customize its printing tasks as per your needs −

Options Description
-- Ends options and is useful for printing files with names starting with a dash (-).
-E: Forces encryption for the server connection.
-U username Specifies the username for server login.
-c: For compatibility; copy the print file to the spool directory, but is not necessary in CUPS, as files are sent directly.
-d destination Sends files to a specified printer.
-h hostname[:port] Connects to an alternate server.
-i job-id Identifies a job to modify.
-m Send an email when the print job is done.
-n copies Sets the number of copies to print (1 to 100).
-o "name=value ..." Sets job options.
-q priority Defines job priority (1 for lowest to 100 for highest; default is 50).
-s Silent mode; it does not report job IDs.
-t "name" Assign a name to the job.
-H hh, hold, immediate, restart, resume

Specifies when the job should be printed.

Immediate prints right away, hold keeps it until released, and specific times (HH) hold it until that UTC time.

Use resume or restart with -i to manage held or completed jobs.

-P page-list Chooses specific pages to print, using numbers and ranges separated by commas (e.g., 1,3-5,16).

lp Command Manual Page

You can access the general manual page of the lp command to learn more about it −

man lp
lp Command in Linux1

Help Page of lp Command

Let's access the lp command's help page for a concise overview of the lp command and its usage −

lp --help
lp Command in Linux2

Examples of lp Command in Linux

You can use the lp command in Linux with or without any option. Let's go through the below-given examples that demonstrate its various functionalities.

Printing a Single File

We can use the lp command to print a single file. To do this, simply type lp followed by the document name to be printed −

lp tutorials.txt

This command sends the specified file to the default printer.

Printing Multiple Files

Similarly, to print more than one files, run the lp command with space separated file names −

lp tutorials1.txt tutorials2.txt tutorials3.txt

This time, the lp command will queue all specified files for printing.

Print Double-Sided Legal Document

Let's run the lp command with different options to print a double sided legal document. Make sure to replace filename with the actual name of the document you want to print −

lp -d myprinter -o media=legal -o sides=two-sided-long-edge filename

In this command we use the -d option to specify the printer, "-o media=legal" to set the paper size to legal, "-o sides=two-sided-long-edge" to enable double-sided printing.

Print Across Four Pages

In the following command, we use the "-o scaling=200" to scale the document so that it prints across four pages −

lp -d myprinter -o scaling=200 filename

Replace the myprinter with your actual printer and filename with your document's name.

Customize Text File Layout

lp command lets us customize the text file layout as well. For this purpose, simply run the lp command with the following syntax −

-d myprinter -o cpi=12 -o lp i=8 -o page-left=72 filename

This time, we use -d option to specify our printer, "-o cpi=12" to set 12 characters per inch, "-o lpi=8" to set 8 lines per inch, and "-o page-left=72" to set a 1-inch left margin (which is equal to 72 points).

Conclusion

The lp command in Linux is a command-line utility that simplifies printing tasks directly from the terminal. It offers a range of customizable options, which allows users to set up print jobs precisely as needed, such as printing single files, adjusting layouts, or managing complex document settings like double-sided printing or specific page scaling.

In this tutorial, we covered the basic syntax, command options, and practical examples to demonstrate how the lp command works in Linux.

Advertisements