From disk to paper
Tutorial – Printing in the Shell
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
Action |
lp |
lpr |
Output to default printer |
|
|
Output with printer definition |
|
|
Number of copies (max. 100) |
|
|
Print without filter |
|
|
Pages to print |
|
Table 2
Print Options
Action |
Input |
Note |
Paper size |
|
For example, |
Landscape |
|
|
Single-sided printing |
|
|
Duplex printing |
|
Flip on long edge |
Duplex printing |
|
Flip on short edge |
Fit to page |
|
|
Group multiple pages on one sheet |
|
Supported values: |
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.
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.
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.
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.
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 02 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 ) 11 12 echo "Selected print job: $dj" 13 lpq -a $dj 14 echo "---------------------------------------------------------------" 15 action=$(echo "Nothing cancel" | smenu -m "Select action" ) 16 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.
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
#!/bin/bash prin () { clear target=$(/usr/sbin/lpc status all | grep \: | tr -d \: | smenu -n3 -c -m "Select a printer:") # Example: Queue output lpq -P$target exit 0 } prin
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 10 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 \| ) 20 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.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.