39

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

5
  • 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.
    – alexis
    Mar 11, 2014 at 20:32
  • i dont know why they downvote my answer , just try it .
    – nux
    Mar 12, 2014 at 0:16
  • @nux +1 All answers are great thanks you all
    – kamil
    Mar 12, 2014 at 9:00
  • @alexis I am clear: I want to print console output. For example ls output directly to the printer
    – kamil
    Mar 12, 2014 at 9:00
  • Oops, indeed you did say and I missed it, sorry!
    – alexis
    Mar 12, 2014 at 18:31

8 Answers 8

41

Mainly there are two default commands:

lpr and lp

man lpr gives the output:

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.

man lp gives the output:

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:

lp /path-to-file-to-print

Or

lpr /path-to-file-to-print
1
  • Thank you. Working pretty fine today on 20.04, really useful. Jul 14, 2021 at 21:11
18

You can use lp

For example:

man firefox | lp -d printername

This will print the man page from firefox to the specified printer

2
  • 1
    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. Jul 11, 2017 at 3:52
  • 1
    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.
    – SDsolar
    Jun 4, 2018 at 17:30
4

If you have them installed, another pair of options worth knowing about are

and

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.

2
  • this is a nice idea
    – kamil
    Mar 12, 2014 at 9:02
  • Sure beats coding postscript directly.
    – SDsolar
    Jun 4, 2018 at 17:34
3

You may want to find out how the printer is accessed first - lpstatwill 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

2

To print a .txt file in a use :

command | lpr -P printername -p ( periority from 1 to 100 )

Example :

ls -l | lpr -P printername -p 1 
2

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):

echo "test" | lp

To print to a specific printer (use lpstat -p | awk '{print $2}' to list available printer names):

echo "test" | lp -d printername

To print a file, rather than a command output:

lp /path/to/file
1

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:

  1. Make sure the CUPS system is installed and running. It provides the daemons that will handle your print jobs.
  2. 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.

1

If you want to control various layout options, fonts, font size, etc. you can use enscript as suggested by RedGrittyBrick.

sudo apt install enscript

A few details that may be useful:

  • The fonts you can use are the ones which have an .afm file in /usr/share/enscript/afm/
  • The font name to use with the -f option must be the short name listed as FontName in it's .afm file. For example: "ZapfDingbats", not "ITC Zapf Dingbats".
    These names are also listed in the /usr/share/enscript/afm/font.map file.
  • If you want to see which monospaced fonts you have available, you could use something like this:
    for f in /usr/share/enscript/afm/*.afm; do grep -q '^IsFixedPitch true' "$f" && grep FontName "$f"; done
  • To set the font size, append "@n" to the font name.
  • You can get the list of printer names to use for the -d or -P option with
    lpstat -p

Example print command :

font=Courier@9
printer=HP-4350

your_command | enscript -P $printer -f $font --tabsize=4

# or for files:
enscript -P $printer -f $font --tabsize=4 $Your_file_to_print

There are of course many more options listed with man enscript.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .