How to Use the lp Command in Linux to Print Files From Terminal

Spread the love

Linux printing can be a bit of a challenge – especially to new users. In this tutorial, we introduce you to the lp command in Linux and show you how to use it to perform basic printing operations. We cover how to print in portrait and landscape mode, single and multiple copies, and more.

The lp command: a basic introduction

In Linux, the Common UNIX Printing System (CUPS) is the layer responsible for managing printer and printing options and services, including printers, printing jobs, and queues.

The CUPS layer has many options to help you set up and use a printer on your Linux system. The lp or “Line Printer” command is one of the commands within the CUPS layer.

The lp command submits a file for printing, while the lpq, or “Line Printer Queue,” command allows you to view the print jobs in the queue.

Like most other Linux terminal commands, the lp command supports various options. Let’s look at some of them.

How to install the lp command

In some instances, you may not have the lp command installed on your system. In such cases, you need to install lp before you can use it.

To check whether you have the lp command installed, run the which command as:

which lp

If you have lp installed, the command line should show you the file path. Example output:

/usr/bin/lp

If you don’t have the lp command installed, you can use the apt package manager to install it.

sudo apt install lprng

The general syntax for using the lp command is:

lp -d [printer-id] [filename]

Replace [filename] with the path to the target file.

Using the lp command to list available printers

We use the lpstat command with the -a option to show connected and available printers and the -d option to show the default printer. The syntax for that is:

lpstat -a | awk '{print $1}'

The above command should return the name of the available printers.

Using the lp command to print to a specific printer

In most cases, you will only have one printer connected to your system. Thus, you will only have to use the lp command followed by the filename to print to the default printer.

However, in some cases, you may have more than one printer connected. You will have to use the -d option to print to a specific printer if that situation arises.

For example, to print the “/home/debian/mte/hello.txt” file to the HP Ink-Tank 310 printer, use the command:

lp -d “Ink-Tank-310” /home/debian/mte/hello.txt

Using the lp command to show the print queue

To show the print queue from the command line, use the lpq command.

Note: if the command is not available in your system, install the cups-bsd package:

sudo apt install cups-bsd

Once you have the package installed, run the command:

lpq -P [printer-name]

The command should give output as:

lpq -P "Ink-Tank-310"
Ink-Tank-310 is ready
no entries

This shows the specified printer does not have any printing jobs and that you can go ahead and print your documents.

Using lp command to print multiple copies

To print a specific number of copies of a file, you can use the -n option of the lp command. The syntax for that is:

lp -n

For example, to print ten copies of a file named “hello.txt” to the printer “HP-ink-jet 315,” run the command as:

lp -d "Ink-Tank-310" -n 10 hello.txt

Using the lp command to print in portrait or landscape

You can also use the lp command to print in the two main printing orientations: portrait and landscape. To do that, you will use the -o option.

Note: the -o option is very versatile. Besides using it to specify the print job orientation, you can also use it to set the paper size.

To use this option to print the previous job example in the portrait orientation, the command for that would be:

lp -d "Ink-Tank-310" -n 10 -o portrait hello.txt

On the other hand, to print in landscape, we would specify “landscape” in the syntax as:

lp -d "Ink-Tank-310" -n 10 -o landscape hello.txt

As mentioned, the -o option has many other options. For example, you can use it to set the paper size.

To print the previous file in a letter size, we would specify size using the lp command and the “media” attribute. An example syntax for that would be:

lp -d "Ink-Tank-310" -n 10 -o portrait -o media=letter hello.txt

lp command for single or double-sided printing

Using the lp command with the “sides” attribute allows you to specify single- or double-sided printing jobs.

The “sides” attribute has two key values:

  1. two-sided-short-edge – landscape mode
  2. two-sided-long-edge – portrait mode.

For example, to print out a two-sided page for our earlier example, the syntax for that would be:

lp -d "Ink-Tank-310" -n 10 -o portrait -o media=letter -o sides=two-sided-long-edge hello.txt

Lp command options

Here are some other options provided by the lp command.

Option Operation
-E Forces encryption for server connections
(marks the end of options) Used to print files with names starting with a dash (-)
-d (destination) Used to print files to a destination printer
-U (username) Used to specify the username for connecting to a server
-h Denotes the server hostname
-m Sends a notification (email) after the completion of a job
-n (copies) Specifies the number of document print copies (1-100)
-q (priority) This option sets the priority of the printing jobs in the queue. 1 is the lowest, 50 is the default, and 100 is the highest.
-i (job-id) Specifies which current to modify
-t (name) Assigns a name to a job
-o (“name=value [name=value …]”) Used to set or assign job options (one or more)

To learn more about all lp supported options, consider the CUPS man pages.

Wrapping Up

This tutorial has shown you how to work with the lp command in Linux to print from the terminal. If you have difficulty setting up your printer, check out the tutorial here for setting up a printer in Linux.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


John Wachira

John is a technical writer at MTE, when is not busy writing tech tutorials, he is staring at the screen trying to debug code.

Comments (1)