I want to use pdfunite to merge several pdf-files into one output file. Since the directory contains lots of different stuff, I thought i could use ls/grep to get the apropriate file-names and then use pdfunite to get it put together. The output of ls/grep look like this:

ls | grep "handout...pdf" 

Then I want to invoke pdfunit, which has the following syntax:

 pdfunite [options] PDF-sourcefile1..PDF-sourcefilen PDF-destfile

My Problem is, that it does not seem like I can pipe the output of ls/grep into pdfunite, because it is not supposed to work like that. On the otherhand, if I try:

pdfunite ls | grep "handout...pdf" grep.pdf

then grep obviously complains about my chosen output-file:

grep: grep.pdf: No such file or directory

I know I could do two seperate steps and then it would work, but I'd like to figure out how to do this with one line.


If your file patterns are that simple, you don't really need a regex, and you can use a simple wildcard:

pdfunite handout??.pdf

Otherwise, you can use a regex with the find command and -regex, but I could not find a way to execute it that doesn't have the possibility of splitting up into multiple groups of files. Some of the ways you might do it also depend on none of the filenames having any spaces in them.


the "tick" character can be used to do that (the thing above the tilda), I believe. Try something like this:

pdfunite [options] `ls | grep "handout...pdf"`

From what I understand, that's how you nest a command. Anything inside the ticks is executed as its own command, and the output of it replaces the ticks. So, the command above would essentially equate to

pdfunite [options] handout01.pdf* handout02.pdf* ...
  • True. But using ls for this is really evil (though common). You have no guarantee that the output of ls will be consistent. It often is aliased. Find otoh is consistent. – Hennes May 20 '15 at 18:41
  • True that. I wanted to provide an answer using his original attempts, however, so I didn't use find – Russell Uhl May 20 '15 at 18:42
  • 1
    Oh, it is a good answer for the OP. I just wanted to state that for all the other people who will eventually read this. – Hennes May 20 '15 at 19:01

You don't necessarily need a grep. You can pass on the pattern to ls.

pdfunite $(ls handout*.pdf*) merged.pdf

  • There's no guarantee that this executes pdfunite only once – Random832 May 20 '15 at 18:33
  • @Random832 Sorry i misread the question. Yes this will execute the pdfunite for each of the pdf files. – rahul May 20 '15 at 18:36
  • @Random832 . I've edited the answer to suit the question now. Thanks for pointing it out. – rahul May 20 '15 at 18:43

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.