When printing multiple files using lpr and the -p option, the name of the first file is printed in the header for all files. How can this behavior be modified such that the correct file name is printed in the header for each file?

As an example, one could print three files "file1.txt", "file2.txt", and "file3.txt" by either of the following commands:

lpr -p file1.txt file2.txt file3.txt
lpr -p file*.txt

In both cases, however, "file1.txt" is printed in the header line of each file. I would like "file2.txt" to be printed in the header for file2, and so forth (without having to print each file separately a la lpr -p file1.txt; lpr -p file2.txt; lpr -p file3.txt).


The lpr man page states:

-hheader       Specify  a title to be used on the banner page (if any).
               Default title is the name of the file.

And nothing else concerning this (meaning, as it states: "Default title is the name of the file", as you didn't pass the -h parameter). So it looks like a misbehaviour, at least at first sight. On a second look, I notice your -p parameter:

-p             Add pr(1) style headers to each page (text only)

Taking a look at the pr(1) man page:

use a centered HEADER instead of filename in page header, -h  ""
prints a blank line, don't use -h""

My guess would be that this "centered header" is only calculated once, instead for each file separately. So you could try your both approaches without the -p parameter to check whether that introduces the problem -- or try a third approach:

for myfile in file*.txt; do lpr -p $myfile; done

which would invoke lpr for each file separately, and thus get you rid of the described problem.

  • It does seem odd that lpr would only calculate the header once for a batch of files, but I suspect you have identified the culprit. I will have to settle for a loop construct, as you have suggested. Thank you. – user001 Jul 27 '12 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.