ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I'm a scripting noob, having spent the past few weeks reading the books I'm trying to put it all into practice.
Part of the bash script that I am working on requires me to find and list all of the .htm files in a volume, then search each of those files for specific words, then export any of those .htm files that contain any of my search terms to an output folder. I have a bit of a blind spot with the final part...I suspect the solution is stunningly simple, can anyone help?
The relevant part of the script is as follows:
find search_path -iname '.htm' > htm.txt
cat htm.txt | while read i; do grep -if /path_to_search_terms "$i" ; done > hit_htm.txt
Obviously, from the above I get a large chunk of raw html containing my search hits. How do I copy each .htm file that contains one or more of my search terms to an output folder?
Thanks for the reply zaichik. I am getting the following error message:
bash: conditional binary operator expected
bash: syntax error near '-if'
The path to my regex file is correct. I have tried putting the $i variables inside " ", but still get the same error message. I have noticed that some of the file paths and/or file names of the .htm* files have spaces in them? Could that be the problem?
for i in $(find search_path -iname '*.htm*'); do
if grep -qif /path_to_regexes $i; then
cp $i /path/to/output/dir
might work - you need to quote the find pattern, probably don't want the grep output (thus the q flag) and need to check its exit status, not its output, anyway. I also modernized the command expansion ($(...) instead of `...`) and wrote out the full if..then..fi construct.
Last edited by slakmagik; 06-18-2007 at 02:12 PM.
Reason: trivial style - also changed the for..do..done to how I write them
Actually, I got to thinking about it again and it's probably best/simplest just to make minimal modifications to what you had to begin with. You may *need* the htm.txt in addition to actually copying/moving files. And spaces *can* be problematic.
find search_path -iname '*.html*' > htm.txt
cat htm.txt |
while read i; do
if grep -qif /path_to_search_terms "$i"; then
cp "$i" output_dir
You should probably throw in an 'if [ ! -d output_dir ]; then mkdir output_dir; fi' sort of thing before you try to copy to it (mkdir -p if you want it deeper in the hierarchy).
Part of the bash script that I am working on requires me to find and list all of the .htm files in a volume, then search each of those files for specific words, then export any of those .htm files that contain any of my search terms to an output folder.