How to print from Linux using only the command line


Utamaru Kido/Getty Images

Back in the early 2000s, I remember connecting to my desktop computer via SSH (from another location) while my girlfriend at the time was at my desk. I sent the following command to the printer:

echo “I love you” | lp

The printer spat out a page with that single line. Within moments, she called me to ask if my printer was possessed or if I was just being cute. Guilty as charged.

Also: How to schedule Linux commands – and when you should

On several occasions over my decades of using Linux, I’ve had to print a file but didn’t have a GUI desktop available, either because I was logged into a server without a desktop environment or had logged into a desktop remotely (via SSH) and only had the command line.

Either way, Linux makes printing from the command line very easy. Better still, it’s all built into your distribution — as long as you have a printer connected and configured, you can do it. 

How to print from the Linux command line

What you’ll need: A running instance of Linux (it doesn’t matter what distribution you use) and a connected and configured printer.

This is only necessary if you have multiple printers connected to your machine. I’ve run into such situations when Linux is being used as a print server with several connected printers. If that’s your case, you’ll need to know which printer to use.

Also: Why Spacedrive is the cross-platform file manager of your dreams

You shouldn’t have to open a terminal window, as you’re probably already looking at it now (seeing as how you’re looking to print without a GUI). With the command prompt at the ready, you’ll need to locate the printer name to use with the command:

You should see a listing of all attached printers. For example, on my Pop!_OS desktop, I see:

printer HL-L2300D-series is idle.  enabled since Mon 21 Oct 2024 10:24:00 AM EDT

The name of my printer is printer HL-L2300D-series.

You might also want to view the printers and print queues. For that, you could issue the command:

lpstat -p | awk '{print $2}' | xargs -n1 lpq -P

 The awk command is used for pattern scanning and xargs is used to build and execute command lines from standard input. 

Also: 5 Linux commands I use to keep my device running smoothly

The output for the above command might look like:

HL-L2300D-series is ready
no entries

Now it’s time to print from the command line. First, I’ll show you how to print when you only have a single printer attached to your machine.

Printing with one attached printer

Let’s say you want to print the file zdnet.txt with your default printer. The command for that would be:

You should see something like this in the output:

request id is HL-L2300D-series-59 (1 file(s))

The file should print, and you’re done.

Printing with multiple attached printers

If you have several printers attached, you’ll first need to find the name of the printer you want to use (see step one) and then use that name in the command like so:

lp zdnet.txt -d HL-L2300D-series

The -d option stands for destination, so you’ll want to add the name of the printer after that.

Also: The first 5 Linux commands every new user should learn

Your file should print, and you’re ready to move on to the next file.





Source link