从 Linux 终端打印文件
lpr 打印

> 
> 使用 lpr 命令在终端中打印文件。
> 
> 
> 


![](/data/attachment/album/202112/20/135809an6s6bxyg7sjnwyq.jpg "Typewriter with hands")


[在 Linux 上打印很容易](https://opensource.com/article/21/8/setup-your-printer-linux)，但有时感觉要做很多工作，比如启动应用程序、打开文件、在菜单中找到打印选项，单击确认按钮等等。当你是一个终端用户时，通常希望使用简单的触发器执行复杂的操作。打印很复杂，但没有什么比 `lpr` 命令更简单了。


### 使用 lpr 命令打印


使用 `lpr` 命令在终端打印文件：



```
$ lpr myfile.odt

```

如果失败的话，你需要设置默认打印机或手动指定打印机。


### 设置默认打印机


根据我在 1984 年印刷的 Berkeley 4.2 手册的旧版本中找到的资料，`lpr` 命令会分页并将文件发送到打印机池，后者将数据传输到称为 <ruby> 行式打印机 <rt>  line printer </rt></ruby> 的东西。


![显示 LPR 命令信息的页面](/data/attachment/album/202112/20/135831cwuu53g81v7dh7dz.jpg)


现在，最初的 `lpr` 命令已经无法满足，因为现代计算机可以访问多台打印机，而且是那些比点阵行式打印机复杂得多的打印机。现在有一个称为<ruby> 通用 Unix 打印系统 <rt>  Common Unix Printing System </rt></ruby>（CUPS）子系统，可以跟踪你的计算机可以访问的所有打印机，计算机应该使用哪个驱动程序与每台打印机通信，默认使用哪台打印机等等。 CUPS 捆绑提供的 `lpr.cups` 或 `lpr-cups` 命令通常以符号链接到 `lpr`，允许你首先借助 CUPS 配置从终端打印。


使用 `lpr` 打印文件，你应该首先设置默认打印机。你可以在系统的打印机设置中设置：


![设置默认打印机对话框](/data/attachment/album/202112/20/135833oqbksdd60szckkhz.jpg)


或者，你也可以使用 `lpadmin` 命令设置：



```
$ sudo lpadmin -d HP_LaserJet_P2015_Series
$ lpstat -v
device for HP_LaserJet_P2015_Series: ipp://10.0.1.222:631/printers/HP_LaserJet_P2015_Series

```

### 使用环境变量设置


你不能在没有管理员账户的系统上设置默认打印机，因为更改打印机默认设置是一项特权任务。在 `lpr` 借助 CUPS 找到目标打印机前，它会在系统中查询 `PRINTER` [环境变量](https://opensource.com/article/19/8/what-are-environment-variables)。


在本例中，`HP_LaserJet_P2015_Series` 是打印机的名称。将 `PRINTER` 设置为该值：



```
$ PRINTER=HP_LaserJet_P2015_Series
$ export PRINTER

```

一旦设置了 `PRINTER` 变量，你就可以打印了：



```
$ lpr myfile.pdf

```

### 获取连接的打印机列表


你可以使用 `lpstat` 命令查看所有连接到系统接受打印任务的打印机：



```
$ lpstat -a
HP_LaserJet_P2015_Series accepting requests since Sun 1 Aug 2021 10:11:02 PM NZST
r1060 accepting requests since Wed 18 Aug 2021 04:43:57 PM NZST

```

### 打印到任意一台打印机


将打印机添加到系统后，并且现在你知道如何识别它们了，你可以打印到其中任何一台，无论你是否设置了默认打印机：



```
$ lpr -P HP_LaserJet_P2015_Series myfile.txt

```

### 如何定义打印机


CUPS 有一个友好的前端页面，可通过 Web 浏览器如 Firefox 访问。虽然它使用 Web 浏览器作为用户界面，但它实际上是在本机（一个称为 `localhost` 的位置）的 631 端口上提供服务。CUPS 管理连接到计算机的打印机，并将其配置存储在 `/etc/cups/priters.conf` 中。


`printers.conf` 文件包含详细描述计算机可以访问的打印设备的定义。不要直接编辑它，但如果你想这样做，你必须先停止 `cupsd` 守护进程。


一个典型的文件定义如下所示：



```
<Printer r1060>
  Info Ricoh 1060
  Location Downstairs
  MakeModel Ricoh Aficio 1060 - CUPS+Gutenprint v5.2.6
  DeviceURI lpd://192.168.4.8
  State Idle
  StateTime 1316011347
  Type 12308
  Filter application/vnd.cups-raw 0 -
  Filter application/vnd.cups-raster 100 rastertogutenprint.5.2
  Accepting Yes
  Shared No
  JobSheets none none
  QuotaPeriod 0
  PageLimit 0
  KLimit 0
  OpPolicy default
  ErrorPolicy stop-printer
</Printer>

```

在本例中，打印机的名称是 `r1060`，即 “Ricoh Aficio 1060”。


`MakeModel` 属性是从 `lpinfo` 命令中提取的，该命令列出了系统上所有可用的打印机驱动程序。假设你知道要打印到 Ricoh Aficio 1060，那么你会发出以下命令：



```
$ lpinfo -m | grep 1060
gutenprint.5.2://brother-hl-1060/expert Brother HL-1060 - CUPS+Gutenprint v5.2.11
gutenprint.5.2://ricoh-afc_1060/expert Ricoh Aficio 1060 - CUPS+Gutenprint v5.2.11

```

它会列出已安装的相关驱动程序。


`MakeModel` 属性是结果的后半部分。在本例中为 `Ricoh Aficio 1060 - CUPS+Gutenprint v5.2.11`。


`DeviceURI` 属性标识打印机在网络上的位置（或物理位置，例如 USB 端口）。在本例中，它是 `lpd://192.168.4.8`，因为我使用 `lpd` 协议将数据发送到一台网络打印机。在我的另一个系统上，我有一个通过 USB 连接的 HP LaserJect 打印机，因此 `DeviceURI` 是 `hp:/usb/HP_LaserJet_P2015_Series?serial=00CNCJM26429`。


### 在终端中打印


将作业发送到打印机是一个简单的过程，只要你了解连接到系统的设备以及如何识别它们。在终端打印非常快速、高效，并且易于编写脚本或作为批处理作业完成。试试看！




---


via: <https://opensource.com/article/21/9/print-files-linux>


作者：[Seth Kenlon](https://opensource.com/users/seth) 选题：[lujun9972](https://github.com/lujun9972) 译者：[MjSeven](https://github.com/MjSeven) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
[Printing on Linux is easy](https://opensource.com/article/21/8/setup-your-printer-linux), but sometimes it feels like a lot of work to launch an application, open a file, find the **Print** selection in the menu, click a confirmation button, and so on. When you're a Linux terminal user, you often want to perform complex actions with simple triggers. Printing is complex, and there's little as simple as the `lpr`

command.

## Print using the lpr command

To print a file from your terminal, use the `lpr`

command:

`$ lpr myfile.odt`


Should that fail, you need to set a default printer or specify a printer manually.

## Setting a default printer

According to my well-worn copy of a Berkeley 4.2 manual printed in 1984, the `lpr`

command paginated and sent a file to a printer spool, which streamed data to something called a *line printer*.

![book page displaying lpr command information](https://opensource.com/sites/default/files/berkeley-1984-lpr.jpeg)

Figure 1: The lpr command.

These days, the actual `lpr`

command is insufficient because modern computers are likely to have access to several printers, and certainly to printers a lot more complex than a dot-matrix line printer. Now there's a subsystem, called the Common Unix Printing System (CUPS), to keep track of all the printers that you want your computer to access, which driver your computer should use to communicate with each printer, which printer to use by default, and so on. The `lpr.cups`

or `lpr-cups`

commands, bundled with CUPS and usually symlinked to `lpr`

, allow you to print from a terminal by referencing your Common Unix Printing System (CUPS) configuration first.

To print a file with `lpr`

, you should first set a default printer. You can set a default printer in your system's printer settings:

![dialog box to set default printer](https://opensource.com/sites/default/files/printer-default.jpeg)

Figure 2: Set a default printer.

Alternately, you can mark a printer as the default with the `lpadmin`

command:

```
$ sudo lpadmin -d HP_LaserJet_P2015_Series
$ lpstat -v
device for HP_LaserJet_P2015_Series: ipp://10.0.1.222:631/printers/HP_LaserJet_P2015_Series
```


## Setting a default destination with environment variables

You aren't permitted to set your own default printer on systems you don't have an admin account on because changing print destinations is considered a privileged task. Before `lpr`

references CUPS for a destination, it queries your system for the **PRINTER** [environment variable](https://opensource.com/article/19/8/what-are-environment-variables).

In this example, `HP_LaserJet_P2015_Series`

is the human-readable name given to the printer. Set **PRINTER** to that value:

```
$ PRINTER=HP_LaserJet_P2015_Series
$ export PRINTER
```


Once the **PRINTER** variable has been set, you can print:

`$ lpr myfile.pdf`


## Get a list of attached printers

You can see all the printers that are accepting print jobs and that are attached to your system with the `lpstat`

command:

```
$ lpstat -a
HP_LaserJet_P2015_Series accepting requests since Sun 1 Aug 2021 10:11:02 PM NZST
r1060 accepting requests since Wed 18 Aug 2021 04:43:57 PM NZST
```


## Printing to an arbitrary printer

Once you have added printers to your system, and now that you know how to identify them, you can print to any one of them, whether you have a default destination set or not:

`$ lpr -P HP_LaserJet_P2015_Series myfile.txt`


## How printers are defined

CUPS has a user-friendly front-end accessible through a web browser such as Firefox. Even though it uses a web browser as its user interface, it's actually a service running locally on your computer (a location called **localhost**) on port 631. CUPS manages printers attached to your computer, and it stores its configuration in `/etc/cups/printers.conf`

.

The `printers.conf`

file consists of definitions detailing the printing devices your computer can access. You're not meant to edit it directly, but if you do, then you must stop the `cupsd`

daemon first.

A typical entry looks something like this:

```
<Printer r1060>
Info Ricoh 1060
Location Downstairs
MakeModel Ricoh Aficio 1060 - CUPS+Gutenprint v5.2.6
DeviceURI lpd://192.168.4.8
State Idle
StateTime 1316011347
Type 12308
Filter application/vnd.cups-raw 0 -
Filter application/vnd.cups-raster 100 rastertogutenprint.5.2
Accepting Yes
Shared No
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
OpPolicy default
ErrorPolicy stop-printer
</Printer>
```


In this example, the printer's name is `r1060`

, a human-readable identifier for a Ricoh Aficio 1060.

The *MakeModel* attribute is pulled from the `lpinfo`

command, which lists all available printer drivers on your system. Assuming you know that you have a Ricoh Aficio 1060 that you want to print to, then you would issue this command:

```
$ lpinfo -m | grep 1060
gutenprint.5.2://brother-hl-1060/expert Brother HL-1060 - CUPS+Gutenprint v5.2.11
gutenprint.5.2://ricoh-afc_1060/expert Ricoh Aficio 1060 - CUPS+Gutenprint v5.2.11
```



This command lists the relevant drivers you have installed.

The *MakeModel* is the last half of the result. In this example, that's `Ricoh Aficio 1060 - CUPS+Gutenprint v5.2.11`

.

The *DeviceURI* attribute identifies where the printer is found on the network (or physical location, such as the USB port). In this example, the *DeviceURI* is `lpd://192.168.4.8`

because I'm using the `lpd`

(line printer daemon) protocol to send data to a networked printer. On a different system, I have an HP LaserJet attached by a USB cable, so the *DeviceURI* is DeviceURI `hp:/usb/HP_LaserJet_P2015_Series?serial=00CNCJM26429`

.

## Printing from the terminal

Sending a job to a printer is an easy process, as long as you understand the devices attached to your system and how to identify them. Printing from the terminal is fast, efficient, and easily scripted or done as a batch job. Try it out!

## Comments are closed.