[SOLVED] string search in .odt document directories
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
File indexers which come with major desktop environments - baloo for KDE and tracker for Gnome can do it, but they are CPU and disk hogs. Not sure if XFCE has a similar tool. Actually odt file is just a buch of zipped xml files, so quick and dirty way would be to write a script unzipping them and grepping inside xml starting from /content.xml or maybe using a more advanced xml grep tool like xml_grep
I fear I might have sounded more capable than I am! But - how would I unzip all the .odt files in a particular directory (and sub-directories) and then pipe them to grep?
And yes, thanks for the warning - I will take a careful backup of everything before I try any operation on them.
Another tool is recoll. It's also an indexer. You can set it to run when you want it to and it's very customizable. It should be in your distro's repos.
As an alternative, using libreoffice to convert .odt files to ,txt files
Code:
#!/bin/bash
# Script to search all .odt files in top directory specified in $1 for string specified in $2
topdir=$1
shopt -s globstar
for f in "$topdir"/**/*.odt; do
libreoffice --convert-to "txt:Text (encoded):UTF8" --outdir "/tmp" "$f" 1>/dev/null
tmpfile=${f/%odt/txt}
tmpfile=/tmp/${tmpfile##*/}
if $(grep -iq "$2" "$tmpfile"); then
echo "Found $2 in $f"
fi
rm "$tmpfile"
done
shopt -u globstar
Actually, the package unzip also provides the zipgrep command.
There's also odt2txt which is much faster than converting with the libreoffice command line (I mean the standalone package of that name, not the symbolic link /usr/bin/odt2txt provided by unoconv which is more or less the same as the headless libreoffice).
Thank you, wonderful community! I have several options now: I can convert .odt files to .txt files, and then just run a search using grep. Or, I could experiment with using recoll. Or ack.
I reckon shruggys suggestion of zipgrep is a better option. I have some very bad experiences of indexers - to the extent I disable them at every system install.
I just looked up zipgrep, and it sounds like the answer to my prayers! One further question, though - is it possible to use zipgrep to search through a whole directory containing .odt files? Or would I first have to zip up the directory, so that I have a file called, say, documents.zip - and then run zipgrep on it?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.