- Print from command line
- 8 Answers 8
- Printing from the Linux Command Line
- Printers in Linux
- Setting Up a Demo Printer
- 1. Installing CUPS
- 2. Creating a Dummy Printer
- Printing from the Command Line
- 1. Basic Usage
- 2. Printing Multiple Copies
- 3. Print Queue
- 4. Canceling a Print Job
- 5. Double-Side Printing
- 6. Landscape Printing
- Other CUPS Options
- 1. Listing the Connected Printers
- 2. Setting a Default Printer
- Conclusion
- About the author
- Sidratul Muntaha
- Print text from terminal
- 3 Answers 3
Print from command line
How can I print the command line output directly to the printer. I am using ubuntu server 12.04 and I have to copy files into a shared directory and then download them from a desktop ubuntu distribution to print them. Any help is appreciated
You didn’t say if you’ll be printing plain text or PDF. Some systems only support plain text out of the box, so this needs to be covered.
8 Answers 8
Mainly there are two default commands:
lpr submits files for printing. Files named on the command line are sent to the named printer (or the default destination if no destination is specified). If no files are listed on the command-line, lpr reads the print file from the standard input.
lp submits files for printing or alters a pending job. Use a filename of «-» to force printing from the standard input.
so easily use the command:
man firefox | lp -d printername
This will print the man page from firefox to the specified printer
Thanks, I also needed to use o fit-to-page to keep the text on the page a bit better. man bash | lp -o media=letter -o portrait -o fit-to-page — , the — at the end ensures standard input is read.
If your printer is default you don’t need the -d parameter. On Ubuntu with Unity, the default printer is set by clicking on the Search button then type «print» and it will take you to the right place where the default can be set. Also, lp in the example says you have been around Unix for some time. lpr works the same on Ubuntu.
If you have them installed, another pair of options worth knowing about are
These are useful for providing numbered pages with headings and optional line-numbers. You can also use then to print booklet style (e.g. two pages on each side of a sheet)
I use these with Postscript-capable printers but I believe that Ubuntu’s print system can rasterize PS for any supported printer.
You may want to find out how the printer is accessed first — lpstat will give you that information. If you compare its output across both systems, you can probably tell whether the printer in question has been configured on both of them. lpstat -p -d lists all printers with their status and tells which one has been set as default printer.
You can simply pipe your output to the lp or lpr command then. You may want to insert a filter for pretty-printing or pagination though. There’s a good summary of tools at the debian manual «Highlighting and formatting plain text data», but I’m usually just using sed to highlight prompts and other stuff before sending everything through a2ps
To print a .txt file in a use :
command | lpr -P printername -p ( periority from 1 to 100 )
ls -l | lpr -P printername -p 1
You can use the lp command.
To print the output of a command to the default printer (use lpstat -d to see what the default printer is):
To print to a specific printer (use lpstat -p | awk » to list available printer names):
echo "test" | lp -d printername
To print a file, rather than a command output:
The question is about how to print from the command line on a server, and it sounds like you don’t yet have any printers defined on that system. I don’t have a system to check on so the following are approximate, but they should give you the general idea:
- Make sure the CUPS system is installed and running. It provides the daemons that will handle your print jobs.
- Once this is up, you need to set up one or more network printers to print to. The usual way to install printers is with the Printer Admin utility, but you evidently aren’t set up to run any GUI programs from your server— just commandline access via ssh. Since you already have an Ubuntu desktop system on the same network, I’d try copying the contents of the directory /etc/cups from the desktop box to the server. It should contain all the printer definitions and drivers you need. Check the files for any necessary adjustments (in case your set-up mentions user IDs or passwords that differ between the two systems), restart cupsd , and if you’re lucky you’ll be able to use lpr to print from the commandline.
PS. In a pinch, you could install enough X utilities to run the Print Admin GUI utility via a remote X connection to your desktop Ubuntu box (log in with ssh -X ), and define the printers you need. But hopefully this won’t be necessary.
Printing from the Linux Command Line
To perform the steps that are demonstrated in this guide, you need the following components:
- A properly configured Linux system. Learn more about setting up Ubuntu and Arch Linux VMs in VirtualBox.
- Basic understanding of the Linux command-line interface.
Printers in Linux
Linux is a versatile operating system. It can perform simple everyday tasks and handle even massive-scale infrastructure. Printing is a common task that all Linux systems can handle.
For this guide, we will configure a dummy printer that prints the output to /dev/null. We will also showcase how to send a document to the printer and manage the print queue.
Setting Up a Demo Printer
To configure a dummy printer, we will use CUPS (Common UNIX Printing System). It’s a freely available printing layer that most Linux distros use as the standard to manage the printers. Check out how to install and configure the CUPS server on Ubuntu.
1. Installing CUPS
First, install CUPS using APT:
Next, enable and start the CUPS service:
2. Creating a Dummy Printer
Now, create a dummy printer that prints to /dev/null:
If the action is successful, it appears on the list of printers:
Optionally, we can mark our new printer as the default printer:
Printing from the Command Line
Now that we have our printer ready, it’s time to start printing.
1. Basic Usage
To print a document, use the “lp” command:
Since no printer is specified, lp sends the document to the default printer to print. If you configured the demo printer that prints to /dev/null, it won’t actually produce any output.
2. Printing Multiple Copies
To print several copies of the document, use the following command:
3. Print Queue
When working with actual printers, it usually takes some time to finish printing a copy. However, you may have multiple documents that you want to print. What to do in that situation?
Thanks to the print queue feature, you can send all the documents to print at once. The CUPS server stores the additional documents in a buffer. Once the current printing task finishes, it automatically sends a new document to the printer.
To view the list of prints that are currently queued, use the following command:
4. Canceling a Print Job
What if you accidentally sent the wrong document to print? As long as the document isn’t sent to the printer yet, we can remove it from the print queue:
However, you should be quick about it. Otherwise, the document will be already printed. In that case, it shows the following error:
5. Double-Side Printing
By default, most printers are configured to print a single side. However, we can tell the CUPS to perform a double-side print:
If you want to make the double-sided printing the default configuration, use the following command:
If you wish to revert to single-sided printing, use the following command:
6. Landscape Printing
We can also specify to print in landscape mode. To do so, use the following command:
Other CUPS Options
So far, we learned the various ways of printing a document. In this section, we will work on the CUPS configuration itself.
1. Listing the Connected Printers
To list all the printers that are currently connected to the system, run the following command:
2. Setting a Default Printer
Whenever printing from the command line without specifying the printer, the “lp” command sends the document to the default printer.
There are a couple of ways to specify a default printer. As demonstrated at the beginning of this guide, we can use the “lpoptions” command to establish a default printer:
Alternatively, we can use the PRINTER environment variable to specify the default printer:
If you want to make this change permanent, add the line to the “bashrc” file:
$ echo «export PRINTER=
Conclusion
We showcased the various ways of printing a document from the command line. We used the CUPS printing layer since it’s the standard printing solution that most Linux distros support. Although the guide uses a virtual printer, any physical printer works as long as you have the proper driver installed and it supports the CUPS communication.
Looking for a printer that’s compatible with Linux? Check out the shortlist of Linux-compatible printers for home/office usage. Linux can also work with printers over the network.
About the author
Sidratul Muntaha
Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.
Print text from terminal
I would like to use a Linux distro without a desktop environment, but I need to print out homework that I type up. I could always email it to myself and print from another computer, but it would be nice if I could just do something like print homework.txt from a bash prompt. Does anyone have a way of doing this?
3 Answers 3
CUPS understands many different types of files directly, including text, PostScript, PDF, and image files. This allows you to print from inside your applications or at the command-line, whichever is most convenient! Type either of the following commands to print a file to the default (or only) printer on the system:
Use the -d option with the lp command to print to a specific printer:
Or the -P option with the lpr command:
Printing the Output of a Program
Both the lp and lpr commands support printing from the standard input:
program | lp program | lp -d printer program | lpr program | lpr -P printer
If the program does not provide any output, then nothing will be queued for printing.
More advanced options can be added to the print job with the -o options. For exampling stapling:
lpr -P printer -o StapleLocation=UpperLeft
Last time I was using such a system, I used to prefer enscript . It’s been a while but I seem to recall it being a more sophisticated version of lpr which could do everything that lpr did and then some. It works like so:
It can either print directly to a printer or convert text to postscript. It should be available in the repositories of all major distributions.
I chose to install Manjaro «Full» which includes printer networking and printing support. From past experience, the «Minimal» install requires a non-trivial amount of effort to get anything printed.
I didn’t know the name of my printer. I tried lpstat -t but that showed the following:
scheduler is running no system default destination lpstat: No destinations added. lpstat: No destinations added. lpstat: No destinations added. lpstat: No destinations added.
At first, I was able to get lpstat working by connecting through Geany, but the printer would timeout after a few minutes.
Since I have a relatively new printer, it uses the «IPP» standard. I used ippfind -l thanks to this answer https://unix.stackexchange.com/a/490827/8234
For me the address was ipp://whatever.local:631/ipp/print
Then I found this answer https://unix.stackexchange.com/a/683993/8234 and that worked great lpadmin -p Cool -v ipp://whatever.local:631/ipp/print -m everywhere
Now lpstat -t works as expected:
Cool accepting requests since Sun 19 Mar 2023 02:23:37 AM CDT printer Cool is idle. enabled since Sun 19 Mar 2023 02:23:37 AM CDT
Then I can simply do echo «test» | lp -d Cool
PS: Perhaps there is an easier way. You can try ippfind -s and then change the spaces to underscores like echo «test» | lp -d Canon_Cool_series but I can’t get this to work without the underscores, and I can’t find any documentation explaining why this works. Basically, I cheated by activating the printer through Geany, and then I could see the printer with lpstat -t
For future reference, I would try the easier «underscore» method first, and if that times out or doesn’t work, use the longer «lpadmin» method.