Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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."

share|improve this question
add comment

4 Answers

up vote 10 down vote accepted

the following will convert all files in the current directory:

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

Sam

share|improve this answer
    
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 '12 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 '12 at 18:28
    
would this not cause issues with non-pdf files? –  cprofitt Nov 4 '12 at 18:30
    
thanks Mik, i eddited the post with your suggestions –  Sam Nov 4 '12 at 19:20
    
Wouldn't this produce files like "filename.pdf.txt"? –  Ryan Thompson Nov 4 '12 at 23:27
show 3 more comments
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
share|improve this answer
    
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 –  cipricus Nov 18 '12 at 23:22
add comment

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.

share|improve this answer
    
can it be done by opening the terminal in that folder and running a command instead of inserting path manually? –  cipricus Nov 4 '12 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 –  cipricus Nov 4 '12 at 19:08
    
the find and xargs I initially suggested did not work when I got a chance to test them. –  cprofitt Nov 4 '12 at 20:01
add comment

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!

share|improve this answer
    
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. –  Ryan Thompson Nov 19 '12 at 1:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.