user3643012
user3643012

Reputation: 42

pdftotext all files in subdirectories if it doesn't already exist

I need to pdftotext all files in a subdirectory if the text file doesn't already exist. I've tried:

find . -name "*.pdf" | while read file; if [ ! -e $file.txt ] do pdftotext $file; done;

but receive: -bash: syntax error near unexpected token `done'

Upvotes: 0

Views: 394

Answers (2)

chepner
chepner

Reputation: 531708

Don't pipe the data to a shell; execute a shell loop from within find.

script='
  for f in "$@"; do
    if ! [ -e "$f" ]; then
      pdftotext "$f"
    fi
  done
'
find . -name '*.pdf' -exec sh -c "$script" _ {} +

This will work for any valid file name, even one containing a newline. find will pass as many files as possible to the script each time it is invoked, and invoke the script as many times as necessary to process all the files.

Upvotes: 0

Cyrus
Cyrus

Reputation: 88731

I suggest:

find . -name "*.pdf" | while IFS= read -r file; do if [ ! -e "$file.txt" ]; then pdftotext "$file"; fi; done

See: help while and help if

Upvotes: 3

Related Questions