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
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.