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?

Asked By: Broseph


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:

lp filename 
lpr filename

Use the -d option with the lp command to print to a specific printer:

lp -d printer filename

Or the -P option with the lpr command:

lpr -P printer filename

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

Source and more Details.

Answered By: Nidal

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:

enscript foo.file

It can either print directly to a printer or convert text to postscript. It should be available in the repositories of all major distributions.

Answered By: terdon

Here’s what worked for me:

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

For me the address was ipp://whatever.local:631/ipp/print

Then I found this answer 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.

Answered By: PJ Brunet
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.