Search code examples
linuxbashlibreoffice

moving files to their original locations after conversion


I have created a Bash script to convert files from .odt to .pdf, but after conversion, the script moves files into the current working directory. As the script is recurring I want each output file to stay in the location where the original is. Here is the script.

#!/bin/bash
​
for file in $(find -type f -name '*.odt')
do
    echo $file
    libreoffice --headless --convert-to pdf $file
done

​I have tried setting --outdir to be path of directories but it moves all files to those directories. I want converted files to stay in same directory as original files. ​


Solution

  • Generate the --outdir value dynamically, and avoid parsing find's output.

    find . -type f -name '*.odt' -print -exec sh -c '
    for fpath do
      soffice --headless --convert-to pdf --outdir "${fpath%/*}" "$fpath"
    done' _ {} +