using find with exec and basename to execute a command in the directory of the found file

[LEFT]I have a tree of directories which contains MSWord files .doc. I want to convert them all to .odt. The sintax is[/LEFT]

[LEFT]soffice --headless --convert-to odt -r --outdir *.doc

[LEFT]I’m trying to use it with find to do it all at once, I’m trying things similar to

[LEFT]find . -name "*.doc" -exec soffice --headless --outdir dirname {} --convert-to odt {} \;

[LEFT]find . -name "*.doc" -exec soffice --headless --outdir $("dirname '{}'") --convert-to odt {} \;

[LEFT]But all of them fail.How can I do it?regards


I don’t have any word documents… but what about;

find . -name '*.doc' -exec soffice --headless --convert-to odt --outdir *.doc {} \;

Also if you could show the actual error, just copy one doc file to a temporary directory and run it there…

A help in debug these sort of things is often to replace the main command with echo. Then you will get a list of what is created.

something like

find . -name "*.doc" -exec echo --headless --outdir $("dirname '{}'") --convert-to odt {} \; 

If what you’re doing works, pls report.
I’ve personally never seen what you’re trying do work using “find”
Using something like find probably will return only a single result or a list of results, but to do what you seem to want to do you should instead typically loop through each file in the directory, test for whether it’s a candidate for conversion (ie test whether it’s a .doc) and if so then execute the conversion… then on to the next file.

Even if you’ve never done something like this before, anyone should be able to Google working examples.

Also, out of curiosity I just Googled for doc to odt conversion utilities and I see a bunch of freeware and services that are all set up to do batch processing… So, unless you’re just trying to do something for educational purposes it looks like there are plenty of off the shelf simple working options.


very useful. I think I discovered what’s happening. The directory names have spaces, so when $(“dirname ‘{}’”) expand the name of the directory, the result command have spaces and fails to interpret them.

I have found a workaround wich works in these case:

find . -name "*.doc" -execdir soffice --convert-to odt --headless {} \;

because opposite to -exec which executes the command from the actual directory, -execdir executes the command from the directory in wich the file is placed

By the way, I have found soffice from console a very useful command, for instance

find . -name "*.doc" -execdir soffice --convert-to pdf --headless {} \;

converts all my files to PDF.


It seems that soffice is a real power tool for doing batch work. Thank you for drawing attention to it.