1

ls *.txt | lp

Wouldn't this command list all files that end in .txt and then print them?

2

2 Answers 2

3

The simple answer is "No".

lp takes either a [list of] files, or STDIN. The command as offered does nothing except fire up an empty print job:

$ ls *.txt|lp
request id is queue-153 (0 file(s))

It's not clear what you wanted. If you want to print a list of files matched by the glob *.txt, then you need to tell lp to use STDIN:

$ ls *.txt|lp -

If you want to print the content of the files matched by the glob *.txt, then you would simply pass the glob itself to lp:

$ lp *.txt
6
  • 2
    Are you sure? Wouldn't ls *.txt | lp produce a single output consisting of a list of the file names? Commented Apr 26, 2021 at 18:59
  • No, it does not. I tried it. lp doesn't take STDIN unless you tell it with lp -
    – Rich
    Commented Apr 26, 2021 at 20:55
  • 1
    That shows how long it is since I used lp :-) The ancient Printing HOWTO also says, "To specify a queue you use the -d option and a filename to print a file, or nothing to print from stdin" Commented Apr 26, 2021 at 21:04
  • 1
    I know - like, "who uses printers these days?!" lol - I thought I'd pick it up since where I work, we actually do.
    – Rich
    Commented Apr 26, 2021 at 21:08
  • 1
    No, leave it! The link to the Printing HOWTO is illuminating history. This is the "Linux & Unix" SE, so the historical perspective is useful.
    – Rich
    Commented Apr 26, 2021 at 21:22
-2

The lp command requires the files to be given as command-line arguments, not via stdin.

If you are trying to print the files (and not to print the list of file names), ls is completely unneeded:

lp *.txt
9
  • 2
    Hang on, I haven't used lp in years, but that seems like it would print the contents of the files while the OP's command would cause lp to print the list of file names. Isn't that the case?
    – terdon
    Commented Apr 26, 2021 at 18:34
  • 1
    I'd argue that I'm not wrong: the question is ambiguous. Commented Apr 26, 2021 at 20:34
  • 1
    Totally agree, Glenn. I'm not looking to "win", but you did say "not via stdin" which ain't true :-) I had to make guesses with my answer.
    – Rich
    Commented Apr 26, 2021 at 20:55
  • 2
    @terdon SysV.3 allowed lp to read from stdin, but the SVR4 and CUPS reimplementations of lp on top of lpr do not. (No, I didn't know either.) Commented Apr 26, 2021 at 21:08
  • 2
    A POSIX-conforming lp takes data from STDIN. Nowadays lp is very likely to be from CUPS which apparently bizarrely doesn't work this way. Commented Apr 26, 2021 at 21:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .