• Name and Location of Zip-File. Example: collectionOfPdfFiles2017.zip
    • Zip File is a collection of PDFs without folder structure
  • Name of PDF File in Zip-File. Example: someFileFrom2017.pdf


  • Console way of extracting the named PDF from the given zip file
  • The file should be unmodified.
    • Basically the extracted file should be in the same condition as I would get it by extracting the whole archive and copying the wanted file by hand.
  • Ideally into a destination folder. But that is luxury.

How do I do this? Currently I have a script that will search for strings in PDF files insides ZIPs and prints out the name of the zip and the pdf inside it. I'll post this for good messure:

    echo "Hi I'll find text in pdf files that are stored inside zip files."
    echo ""
    echo "Enter search string:"
    read searchString

    echo "Ok. I'll search all zip files for content with this text..."

    for z in *.zip
    zipinfo -1 "$z" |  # Get the list of filenames in the zip file
        while IFS= read -r f
        unzip -p "$z" "$f" | # Extract each PDF to standard output instead of a file
            pdftotext - - | # Then convert it to text, reading from stdin, writing to stdout
            grep -q $searchString && echo "$z -> $f" # And finally grep the text

This script was created thanks to this answer.

  • So what is wrong with this script? Dec 11, 2019 at 9:44
  • Your script and your question seems to be conflicting.. You want to find and extract a pdf that contains specific text? So its not related to filename of pdf? Please review your question
    – cmak.fr
    Dec 11, 2019 at 9:44
  • The script I posted above is one that I use to find which zip-files contain pdf files that I need. It is indeed not an solution to my problem. But it provides the skeleton for the solution.
    – KFleischer
    Dec 11, 2019 at 22:28

1 Answer 1


unzip specific file from zip archive

unzip -j "myarchive.zip" "in/archive/file.pdf" -d "/destination/path/"

In your script

# Set a destination path
# dump pdf to temp text file
# unzip the file to stdOut and convert it to text
unzip -p "$z" "$f" | pdftotext - $tempfile
if grep -q $searchString $tempfile; then
    unzip -j "$z" "$f" -d "$dest"
    # some text output
    echo "$z -> $f"
rm $tempfile
  • Hmm... I can not get it running. It says that grep expects a unary operator.
    – KFleischer
    Dec 11, 2019 at 22:26
  • Oops, answer edited
    – cmak.fr
    Dec 12, 2019 at 4:47
  • nearly correct. It must be "| pdftotext - $tempfile" The "-" was missing.
    – KFleischer
    Dec 12, 2019 at 6:58
  • Another problem was for me, that I replaced $dest directly with a fixed folder. In this case I needed to remove the quotes. (Not "~/output/dir" but ~/output/dir).
    – KFleischer
    Dec 12, 2019 at 7:20

You must log in to answer this question.

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