Bulk converting and moving docx files to pdf

The issue I had was a set of student assignments downloaded from Moodle. Most were in Word format (.docx), but to grade them quickly I wanted them in pdf.

The Moodle download was a zip file that expanded to a series of directories of the format “studentName/randomwassignmenttitle.docx.” So I also wanted to collect all the files one directory higher up BUT add the student names to the files.

So to put in steps:

  1. I wanted to convert all .docx files to .pdf
  2. I wanted to rename all PDF files in individual directors from $DIR/*.pdf to $DIR/$DIR*.pdf
  3. I wanted to move all pdf files up one directory.

In the end I did it quickly using two scripts:

  1. to convert everything to PDF: find . -name "*.docx" -print0 | xargs -0 unoconv -f pdf *.docx (modified from here)
  2. to add the directory name to the file name and then move it up one: find "$PWD" -type f -name "*.pdf" -exec bash -c ' DIR=$( dirname "{}" ); mv "{}" ../"${DIR##*/}".pdf ' \; (modified from here)

I’m sure I could combine them into a single and more elegant script, but this worked for now.

tags:


Follow

Get every new post delivered to your Inbox

Join other followers: