How to print from the command line in Ubuntu?

This little guide will show you how to print documents and output them in the Ubuntu command line on your printer. By default, you have a printer set up for use and the command in use here will use your default printer to fulfill the commands.

There might be several reasons why you want to print from the command line in Ubuntu. Maybe you have content in a text file that you would like to print, or maybe you want to run a specific command and print the outcome. Or are you one of those people with an ink printer that runs dry and you want to create a script that ensures the printer is used at least once a week for the ink heads not to run dry? Here I will show you quickly how to print documents from the command line in Ubuntu.

Print using the Ubuntu command line.

A printing command is installed on your Ubuntu system by default, lpr. You can run the command man lpr to see all that it has to offer, but let us start with the examples you see as you run the man command.

Print two copies of a document to the default printer:
lpr -# 2 filename

Print a double-sided legal document to a printer called "foo":
lpr -P foo -o media=legal -o sides=two-sided-long-edge filename

Print a presentation document 2-up to a printer called "foo":
lpr -P foo -o number-up=2 filename

Sometimes the examples and instructions are complicated and hard to understand, so let me just give you some very straightforward and easy-to-understand examples.

Print a text file or other document from the Ubuntu command line.

lpr mydocument.txt

This command will print the document mydocument.txt in the active directory using your default printer. If the document is located elsewhere, add the folder information before the name of the file (for example: /home/myuser/documents/mydocument.txt)

Print the output of a command in the command line.

ls -l | lpr

This will take the output of your list request and send it to the printer for printing. This is just one example, but you can do the same with other commands that you run and then send the output to your printer.

Schedule a printing job with Crontab.

Would you like to print something regularly? You can use a command like this in Crontab.

15 30 * * * lpr /home/myuser/camel.txt

If you add this line to your crontab instructions, the document camel.txt will be printed on your default printer every day at 15:30.


That was easy… These are very basic instructions, but they might be exactly what you need to get your task done. If you have further comments, questions, or input, please use the comment field below.

Leave a Reply