Print from command line

145,388

Solution 1

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

Solution 2

You can use lp

For example:

man firefox | lp -d printername

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

Solution 3

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.

Solution 4

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

Solution 5

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 
Share:
145,388

Related videos on Youtube

kamil
Author by

kamil

Updated on September 18, 2022

Comments

  • kamil
    kamil almost 2 years

    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

    • alexis
      alexis over 10 years
      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.
    • nux
      nux over 10 years
      i dont know why they downvote my answer , just try it .
    • kamil
      kamil over 10 years
      @nux +1 All answers are great thanks you all
    • kamil
      kamil over 10 years
      @alexis I am clear: I want to print console output. For example ls output directly to the printer
    • alexis
      alexis over 10 years
      Oops, indeed you did say and I missed it, sorry!
  • kamil
    kamil over 10 years
    this is a nice idea
  • Elijah Lynn
    Elijah Lynn almost 7 years
    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.
  • SDsolar
    SDsolar about 6 years
    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
    SDsolar about 6 years
    Sure beats coding postscript directly.
  • Geppettvs D'Constanzo
    Geppettvs D'Constanzo almost 3 years
    Thank you. Working pretty fine today on 20.04, really useful.