How to batch print multiple PDFs from multiple (sub)folders in linux (command line, find, xargs and lpr)
Tags: bash, commandline, linux
Published: 2022-11-02 20:40:00

---
title: How to batch print multiple PDFs from multiple (sub)folders in linux (command line, find, xargs and lpr)
published: true
date: 2022-10-30 19:11:40 UTC
tags: bash,commandline,linux
canonical_url: https://diegocarrasco.com/how-to-batch-print-multiple-pdfs-from-multiple-subfolders-in-linux-command-line-find-xargs-and-lpr/
---

Use the command line command `lpr` on Linux to print several files (in this case PDFs) from several folders in the same tree.

<section id="context">
<h2>Context</h2>
<p>I needed to print several PDF files which were in several folders under a main folder. Ex. Folder1/pdf1.pdf, Folder2/Folder3/pdf2.pdf, and so on.</p>
<p>To open each folder and each file to print them was too much time and work, and I didn't find a way to use <a href="https://diegocarrasco.com/how-to-batch-print-multiple-pdfs-from-multiple-subfolders-in-linux-command-line-find-xargs-and-lpr/#nemo" id="citation-reference-1" role="doc-biblioref">[Nemo]</a> to print all the files.</p>
<p>I have used <code>find</code>, <code>xargs</code> before, and I found the command <code>lpr</code> allowed me to print from the terminal. (I wrote an article which also uses <code>xargs</code> some time ago here: <a href="https://diegocarrasco.com/how-to-find-files-using-regex-regular-expressions-in-gnulinux-and-macos-command-line-and-do-something/">How to find files using Regex (Regular Expressions) in GNU/Linux and MacOs command line and do something.</a>)</p>
</section><section id="solution">
<h2>Solution</h2>
<p>After searching for some information (see <a href="https://diegocarrasco.com/how-to-batch-print-multiple-pdfs-from-multiple-subfolders-in-linux-command-line-find-xargs-and-lpr/#references">References</a>), I used the following line to search for all the files in all the subfolders and send their paths to <code>lpr</code> using <code>xargs</code>.</p>
<p>This should work if the printer you want to use is set as default. If you want to use a specific printer check the last references, which uses the argument <code><span>-P</span> "$printer"</code> with <code>lpr</code> to set a printer.</p>
<div><pre><a id="rest_code_757c7c097a144467a459e83a64d015ad-1" name="rest_code_757c7c097a144467a459e83a64d015ad-1" href="https://diegocarrasco.com/how-to-batch-print-multiple-pdfs-from-multiple-subfolders-in-linux-command-line-find-xargs-and-lpr/#rest_code_757c7c097a144467a459e83a64d015ad-1"></a>find -iname <span>'*.pdf'</span> <span>|</span> xargs -i lpr <span>{}</span>
</pre></div>
<p>This will print all the files found with <cite>find</cite> in the folder you are in.</p>
</section><section id="references">
<h2>References</h2>
<ul>
<li><p><a href="https://stackoverflow.com/a/47743845">Make xargs handle filenames that contain spaces</a></p></li>
<li><p><a href="https://forums.linuxmint.com/viewtopic.php?t=168552">Print Multiple PDF's or Doc's without opening Each One???</a></p></li>
<li><p><a href="https://www.frenssen.be/2010-05-09-printing-a-file-from-the-right-click-context-menu-in-nautilus/">Printing a file from the right-click context menu in Nautilus</a></p></li>
<li><p><a href="https://man7.org/linux/man-pages/man1/lpr.1.html">lpr man-pages</a></p></li>
<li><p><a href="https://man7.org/linux/man-pages/man1/xargs.1.html">xargs man-pages</a></p></li>
<li><p><a href="https://man7.org/linux/man-pages/man1/find.1.html">find man-pages</a></p></li>
</ul>
<div role="list">
<div id="nemo" role="doc-biblioentry">
<span><span>[</span><a role="doc-backlink" href="https://diegocarrasco.com/how-to-batch-print-multiple-pdfs-from-multiple-subfolders-in-linux-command-line-find-xargs-and-lpr/#citation-reference-1">Nemo</a><span>]</span></span>
<p>file browser in Linux Mint</p>
</div>
</div>