Moving Files After Conversion to pdf

I have created a linux script to convert files from .odt to .pdf But the script after converting moves files into same dir where script is executed. As script is recurring I want files to stay in the same where originals are. Here is the script.

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

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 1:

You can try to change the directory you are working before converting the file, so the converted file will be output to the current directory.

I've not tested it but something like this might work

#!/bin/bash
for file in $(find -type f -name '*.odt')
do
    cd $(dirname ${file});
    libreoffice --headless --convert-to pdf $(basename ${file});
done