Results 1 to 6 of 6

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

  1. #1

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

    I have a tree of directories which contains MSWord files .doc. I want to convert them all to .odt. The sintax is
    Code:
    soffice --headless --convert-to odt -r --outdir *.doc
    I'm trying to use it with find to do it all at once, I'm trying things similar to
    Code:
    find . -name "*.doc" -exec soffice --headless --outdir dirname {} --convert-to odt {} \;
    Code:
    find . -name "*.doc" -exec soffice --headless --outdir $("dirname '{}'") --convert-to odt {} \;
    But all of them fail.How can I do it?regards


  2. #2
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    27,226
    Blog Entries
    15

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

    Hi
    I don't have any word documents.... but what about;
    Code:
    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...
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

  3. #3
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,385

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

    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
    Code:
    find . -name "*.doc" -exec echo --headless --outdir $("dirname '{}'") --convert-to odt {} \; 
    Henk van Velden

  4. #4
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    11,477
    Blog Entries
    2

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

    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.

    TSU
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

  5. #5

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

    Quote Originally Posted by hcvv View Post
    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
    Code:
    find . -name "*.doc" -exec echo --headless --outdir $("dirname '{}'") --convert-to odt {} \; 
    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:

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

    Code:
    find . -name "*.doc" -execdir soffice --convert-to pdf --headless {} \;
    converts all my files to PDF.


    regards

  6. #6
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,385

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

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •