From disk to paper

Tutorial – Printing in the Shell

Article from Issue 234/2020

A few commands and some simple shell scripts make it easier to manage your printer so that you can access print functions quickly and automate recurring tasks.

If you work with LibreOffice or an image processing program like Gimp, you don't have to look too hard for the print function. The print icon is usually located in the upper left corner of the buttonbar; alternatively, you can press Ctrl+P. In many situations, however, it would be more practical to print without the help of an application – for example, if you want to print from a script.

Complex printing commands can also be transferred as shell commands. There are instructions to fit several pages on one sheet, for duplex printing, for cover sheets to make sorting easier, or for options to change the page orientation.

Linux basically comes with two commands for controlling printers at the command line, lp and lpr. Table 1 shows some important options, while Table 2 lists some helpful variants for everyday use. For additional options and settings, check out the extensive man pages for lp [1] and lpr [2].

Table 1

Print Commands




Output to default printer

lp <file>

lpr <file>

Output with printer definition

lp -d <printer> <file>

lpr -P <printer> <file>

Number of copies (max. 100)

lp -n <count> [...]

lpr -# <count> [...]

Print without filter

lp -o raw [...]

lpr -o raw [...]

Pages to print

lp -P <Pages> [...]

Table 2

Print Options




Paper size

-o media=<format>

For example, a3, a4, a5


-o landscape

Single-sided printing

-o sides=one-sided

Duplex printing

-o sides=two-sided-long-edge

Flip on long edge

Duplex printing

-o sides=two-sided-short-edge

Flip on short edge

Fit to page

-o fit-to-page

Group multiple pages on one sheet

-o number-up=<number>

Supported values: 2, 4, 6, 9, 16

Displaying the Queue

The status of the currently pending print tasks can be displayed using the lpq or lpstat commands. If executed without any further options, both commands display the queue for the default printer. Optionally, use the -P <printer> option to specify the desired printer.

All printers and their current status can be obtained by typing lpstat -a, lpstat -o, or lpq -a (Figure 1). The lpstat -t command provides a comprehensive overview of printers, queues, and jobs.

Figure 1: There are many approaches for displaying the queued jobs.

If print jobs are available, you will see output like that shown in Figure 2. Among other things, you will find the job number with which you can manage a print job in the queue, if needed. For various actions you have to extract the print job number from the output.

Figure 2: The number appended to the printer name with a hyphen corresponds to the number of the print job.

You can use the command from Listing 1 to filter the number of the current print job from the status report and then use the number to obtain additional information about the print request, delete the associated request, or move it to another printer.

Listing 1

Print Job Number

$ lpq -al | grep job | cut -d\[ -f2 | cut -d ' ' -f2

Managing Print Jobs

To cancel a print job, type lprm <job number> or cancel <job number>. You can extract the job number from the queue display by typing lpq -a. To cancel a print job, you either have to be the owner of the print job or have appropriate administrative rights. Regardless of which printer, cancel -a deletes all the current print jobs. This is why it makes more sense to specify the printer with the option. In Figure 3, lprm is used to delete one of the existing print jobs.

Figure 3: If you send your 100-page master's thesis to the wrong printer, you can cancel the job at the command line.

If a printer fails during operation, but you do not want to interrupt the print job, you can move it to another device using lpmove – provided you have the appropriate rights. You can use this command in the form lpmove <Job number> <New printer> or for all print jobs of a printer with lpmove <Old printer> <New printer>. Figure 4 shows how to move print jobs from one printer to another.

Figure 4: If a printer unexpectedly fails, you can reroute the print jobs to a working printer.

Scripted Jobs

Together with some other shell functions, the commands presented here can be assembled to create a script that prints details about a print job and deletes it at the push of a button if necessary (Listing 2). If you want to use the routine as a function in your own shell scripts, cut the djobs () { [...] } function and paste it at the start of your own program.

Listing 2

Print Job Details

01 #!/bin/bash
03 djobs () {
04   clear
05   echo "Current print jobs:"
06   echo "---------------------------------------------------------------"
07   lpq -a
08   echo "---------------------------------------------------------------"
09   echo "Select print job: "
10   dj=$(lpq -al | grep job | cut -d\[ -f2 | cut -d ' ' -f2 | smenu -n10 -t1 )
12   echo "Selected print job: $dj"
13   lpq -a $dj
14   echo "---------------------------------------------------------------"
15   action=$(echo "Nothing cancel" | smenu -m "Select action" )
17   if [ "$action" = "Cancel" ]; then
18     lprm $dj
19     if [ $? -eq 0 ]; then
20       echo "Print job $dj deleted"
21     fi
22     sleep 2
23   fi
24   exit 0
25 }
26 djobs

With a little help from the smenu [3] and YAD tools, it is quite easy to implement selection dialogs. The smenu command-line tool is recommended for command-line wizards and users who need to work on a remote computer via SSH. YAD on the other hand is an option for users who prefer the comfort of the desktop environment. The program displays windows on the screen with simple instructions.

Smenu and YAD are missing from the default software selection in most distributions. But, thanks to the smenu and yad packages, the two programs can be installed quickly from the package sources.

In smenu, the -m <Title> option shows the user what they are selecting and, if necessary, why. Without further options, you would select the desired entry word by word within a line. -n <number> limits the selection lines; t1 tells smenu to display the selection line by line in a single column. Figure 5 shows the flow of the script.

Figure 5: The djobs() function from Listing 2 wraps what are often cryptic printing commands in simple dialogs.

The script from Listing 3 is recommended for a terminal session. It selects the printer for the print output, the queue display, or a queue you want to move. The example in Figure 6 lists the print jobs for a specific printer.

Listing 3

Select Printer

prin () {
  target=$(/usr/sbin/lpc status all | grep \: | tr -d \: | smenu -n3 -c -m "Select a printer:")
  # Example: Queue output
  lpq -P$target
  exit 0
Figure 6: The prin() function from Listing 3 lists all the available printers and outputs the print queue for the selected device.

YAD is a good choice if you are looking for a graphic alternative. Without too much programming work, you can use this tool to create simple applications based on a shell script. The example in Listing 4 shows how to select a printer and the file to be printed.

Listing 4

Select Printer and File

01 #! /bin/sh
02 prin {
03   clear
04   # Read printers
05   z=0
06   pline=""
07   for i in $(/usr/sbin/lpc status all | grep : | tr -d \: ); do
08     pline=$(echo $pline$i!)
09   done
11   # Menu item with Yad
12   printer=$(yad --title="PRINT PROGRAM" --text="Select printer" --form \
13   --field="Printer":CB $pline \
14   --button="Cancel":1 \
15   --button="Next":2)
16   if [ $? -eq 1 ]; then
17     exit
18   fi
19   printer=$(echo $printer | tr -d \| )
21   # Print sample file
22   file=$(yad --title="PRINT PROGRAM" --file)
23   yad --title="PRINT PROGRAM" --text="Print selected file $file?" --yesno
24   if [ $? -eq 0 ]; then
25     lpr -P$printer $file
26   fi
27   exit 0
28 }
29 prin

The yad calls in lines 12, 22, and 23 first ask for the desired printer (Figure 7), then open a dialog for file selection (Figure 8), and finally show a confirmation.

Figure 7: A Bash script can also be used to implement programs with graphical dialogs as in Listing 4.
Figure 8: The YAD command-line tool supports selection lists, file dialogs, and simple buttons.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • smenu

    The smenu tool reduces the effort of creating shell menus to one line, with numerous options for a wide range of design alternatives.

  • Command Line – CUPS

    Using the Common Unix Printing System, you can configure and manage your printer from the command line.

  • Perl – Print Test

    Cautious administrators will want to make sure a Linux installation is still working as intended after an update. A simple test suite offers that assurance.

  • Make Your Printer Smarter

    Niche hardware from the olden days does not always embrace the network. Attaching a Raspberry Pi or other single board computer can add lots of new functionality.

  • Bluetooth Printing

    Even if your printer vendor doesn’t advertise Linux Bluetooth support, there are a few tools that may help you set up your Linux system for Bluetooth printing.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More