4

I have a directory with a very large number of PDFs that I am converting to *.txt documents with Poppler pdftotext. I have the following command that does the conversion of all the files.

for f in *.pdf; do pdftotext $f; done

The PDFs are on an external storage device without any additional space. How can I redirect the output to a different directory? That is, when each PDF is converted, it is written to another location rather than the same directory as the PDF.

Brian P
  • 223
  • 2
  • 10

1 Answers1

4

This was the suggested solution by don_crissti, and I am providing an explanation of it here for users just getting started with Unix and Linux. Feel free to edit, as this summary was written by a novice.

for f in *.pdf; do pdftotext "$f" "/path/to/some/dir/${f%.*}.txt"; done

Now, let's examine each command. Here is a standard for loop that is used to iterate overall all the files in the current working directory. for f in *.pdf. In this command, f is used as the index. The next command converts the PDF to text. This assumes the user has already installed poppler:

pdftotext "$f" "/path/to/some/dir/${f%.*}.txt"

"$f"is used to hold the name of the input PDF file and "/path/to/some/dir/${f%.*}.txt" specifies the path for the output TXT file, i.e. the target directory /path/to/some/dir/ and the output file name ${f%.*}.txt which is basically the input file name ("$f") with the .pdf extension removed from the file name via parameter substitution and a .txt suffix appended.

Brian P
  • 223
  • 2
  • 10