26

I know that I can convert pdf files to text files one by one like this :

$ pdftotext filename.pdf

But is there a single command that would make that conversion without specifying separate file names so as to convert them all?

I see here, on Wikipedia, that "Wildcards (*), for example $ pdftotext *pdf, for converting multiple files, cannot be used because pdftotext expects only one file name."

5 Answers 5

29

The following will convert all files in the current directory:

for file in *.pdf; do pdftotext "$file" "$file.txt"; done
7
  • its only one command, it can be typed in one line in the terminal (its the pdftotext inside a for loop in a one-line-syntax, which is what the op asked for)
    – Sam
    Nov 4, 2012 at 18:23
  • check out these links for more info of how the for loop works: cyberciti.biz/faq/bash-for-loop thegeekstuff.com/2011/07/bash-for-loop-examples
    – Sam
    Nov 4, 2012 at 18:28
  • would this not cause issues with non-pdf files?
    – cprofitt
    Nov 4, 2012 at 18:30
  • Wouldn't this produce files like "filename.pdf.txt"? Nov 4, 2012 at 23:27
  • 4
    You can just for file in *.pdf; do pdftotext "$file"; done and it will replace .pdf with .txt. No need for sed or awk.
    – User0
    Jun 11, 2015 at 19:10
11
ls *.pdf | xargs -n1 pdftotext

xargs is often a quick solution for running the same command multiple times with just a small change each time. The -n1 option makes sure that only one pdf file is passed to pdftotext at a time.

Edit: If you're worried about spaces in filenames and such, you can use this alternative:

find . -name '*.pdf' -print0 | xargs -0 -n1 pdftotext
2
  • please see my answer: can that command be adapted so as to avoid the problem mentioned there? this doesn't mean that your solution is not good, on the contrary, it does something very specific that the other alternatives here do not. but i was just curious
    – user47206
    Nov 18, 2012 at 23:22
  • 3
    Alternatively: ls *.pdf | xargs -L1 -I% pdftotext % %.txt
    – kenorb
    Aug 1, 2014 at 9:18
2

write a bash script

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

or type it in a one-line command as follows:

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

I hope this helps. I do not have a large group of .pdfs to test this on, but I use this strategy to convert my .flac files to .ogg files.

3
  • can it be done by opening the terminal in that folder and running a command instead of inserting path manually?
    – user47206
    Nov 4, 2012 at 18:39
  • couldn't you paste it here as such and insert it into your answer? that would be a good answer. i was not able to reach the good formula just by deleting a part of what you posted
    – user47206
    Nov 4, 2012 at 19:08
  • the find and xargs I initially suggested did not work when I got a chance to test them.
    – cprofitt
    Nov 4, 2012 at 20:01
1

I have to thank first to Sam and to Ryan Thompson as well to all other answerers - for my answer here is nothing but a variation relating to the possibility of adding their solutions to Thunar's custom actions:

so, as any terminal command, a command to convert to text all pdf files within a folder can be put in the list of custom actions in Thunar file manager

enter image description here

enter image description here

enter image description here

The command there is find . -name '*.pdf' -print0 | xargs -0 -n1 pdftotext, (comming from Ryan Thompson) it is the one I prefer to use, but it has a nasty turn... see below...

enter image description here

enter image description here

...it is a funny command, to be used with care: it is made to convert to text all pdf within the folder where it is fired, so, if it is fired by mistake in the home folder, it will have some unwanted effects: all your pdfs will be converted to text!

(I tested it like this: created a folder called "test" on the desktop and in it a pdf file and a series of folders within folders (/Desktop/test/a/b/c/e/f/g/h/i) each containing the same pdf. Running that command in /Desktop/test has converted all pdfs down to that in "i" folder.)

(I would welcome comments on how to adjust this command so as to avoid that risk.)

Replacing that with the other one (for file in *.pdf; do pdftotext "$file" "$file.txt"; done) coming from Sam, the problem is avoided.

But in certain cases one might wish exactly what Ryan's solution does!

1
  • 1
    You can avoid the find command searching in subdirectories by using -maxdepth 1. Also, when putting it into Thunar's custom actions feature, you should probably replace find . with find %F to allow Thunar to properly pass the paths of the selected directories. Nov 19, 2012 at 1:10
0
for file in *.pdf; do pdftotext "$file" "$file.txt"; done

This one outputs sample.pdf.txt.

I tried using this one, as user2357111317 suggest and I also include -layout to preserve the layout of the text

for file in *.pdf; do pdftotext -layout "$file"; done

You must log in to answer this question.