I have a directory search which can contain (directory or file). I
have to implement a search for these two broad conditions -
One : Search based on the properties of the directory/file, like
title, creator, name, date etc.
Two : Search for specific strings within the file.
Or in short, implement something similar to 'find'. As a start, I have
downloaded the source code for find. My problem is - going through all
the files. I wanted to know the name of the files where the 'searching
for files' part is implemented and the name of the files where the
'pattern matching' part is implemented.
Thanks and regards.
Pradip.
The easiest way to do this would be to USE find. Find can do all of the
things you mention except for strings within the file. For that you
could use a combination of find | grep | awk.
"Pradip" <cau...@iiitb.ac.in> wrote in message
news:7209c168.0211...@posting.google.com...
Welcome, Pradip.
If I can assume that you want to find a particular string in a
file with certain characteristics, then here's a
script that will do the trick, but you will have to
fill in the find options with whatever is appropriate.
And the test options ( [ .....] is a synonym for test)
$help test .
Bash is also assumed here.
Man find is your ticket, and this is a critical utitlity,
as is grep and the test builtin.
# for file in $( find . -type f ) ; do \
if [ -x $file -a -w $file ] ; then \
grep -in '.*string.*' $file /dev/zero ; \
fi ; \
done
Here's what 's going on
Those \ mean it is all actually one line. Nothing should be typed after
them except Enter/Return. It neutralises the newline that is inserted when
you hit Enter/Return (you will then get a new prompt
that looks like > )
Firstly, it runs find on every file in every subdirectory from the top
one. If it is a regular
file ( -type f ) it will send it to the builtin test utility,
and there it will be tested to see if the executable (-x) permission is
set and (-a) if it is writable (-w).
If it passes all these tests, the grep will look for
a string (the .* is the regular expression equivalent of the shell wild card
* and means any characters
at all, and any number of them, including spaces.
Find uses shell wildcards but grep uses regular
expressions. ) and return the line with that string.
i means the search is case-insensitive and n
will return the line number. /dev/zero is used as a dummy
file that will make grep return the name of the file.
You can make this a script to use again by putting
it in a file with #!/bin/bash at the top, and leaving
out the \ .
HTH
Bruce
--
Bellingham Washington USA
bburhan1 [ AT] earthlink [ DOT] net
I think that everything you require can be accomplished without reading or
modifying _any_ source code, eg. find. If you want to read the source,
go ahead. However, modifying find to cover a specific application is probably
unwise - what are you going to do when GNU fixes bugs and issues new releases?
Maintain your modification as a patch set?
You need to study the 'find' man page and search for examples or tutorials.
Maybe also study how to use the shell to combine commands: pipes, command
substitution and a program called 'xargs'.
"Chris Coyle" <cco...@nohost.sprint.ca> wrote in message
news:uMOE9.1308$oz4....@newscontent-01.sprint.ca...
[...]
> You need to study the 'find' man page and >search for examples or
tutorials.
Good luck on that one. I searched for a tutorial on
find for hours. Of course, the very name of the utility makes such a
search almost impossible.
Grep has many tutorials, as does sed. But the
last member of this holy trinity of Linux/Unix
utilities doesn't seem to have a one.
Bruce
--
Bellingham Washington USA
bburhan1 [ AT] earthlink [ DOT] net
> Maybe also study how to use the shell to combine commands: pipes, command
OK, here's one:
http://www.kingcomputerservices.com/unix_101/using_find_to_locate_files.htm
But even better, go to google groups (groups.google.com) and do a
group-specific search for "find" in comp.unix.shell
(Tons of stuff).
> Good luck on that one. I searched for a tutorial on
> find for hours. Of course, the very name of the utility makes such a
> search almost impossible.
We are fortunate that the GNU project calls the package "findutils".
The info pages contain some examples.
--
Paul Kimoto
This message was originally posted on Usenet in plain text. Any images,
hyperlinks, or the like shown here have been added without my consent,
and may be a violation of international copyright law.
"Paul Kimoto" <kim...@lightlink.com> wrote in message
news:slrnau7n9g...@adore.lightlink.com...
> In article <yhPE9.2074$yy.3...@newsread1.prod.itd.earthlink.net>,
> Bruce Burhans wrote:
> >> You need to study the 'find' man page and
> >> search for examples or
> >> tutorials.
>
> > Good luck on that one. I searched for a tutorial on
> > find for hours. Of course, the very name of the utility makes such a
> > search almost impossible.
>
> We are fortunate that the GNU project calls the package "findutils".
> The info pages contain some examples.
>
A few. But not enough, as is the usual case
with info and man pages.
Bruce
--
Bellingham Washington USA
bburhan1 [ AT] earthlink [ DOT] net
"Chris Coyle" <cco...@nohost.sprint.ca> wrote in message
news:GYQE9.1330$oz4....@newscontent-01.sprint.ca...
Thanks Chris,
That tutorial is now on my disk and you are certainly right about searching
comp.unix.shell.
for anything related to this topic.
Thank you all for your replies. But I would like to re-phrase my
earlier question. (I think I communicated the wrong meaning...) I need
to implement a search for my project here. The project I am working on
has a directory kinda structure. This directory kinda structure is
different from the file system structure in Unix/Linux. So I was
thinking of 're-using' the code of the 'find' utility for my project.
(Or in other words, I can't use/call utilities like 'find' because of
portability.) And I would be glad if anyone could tell me the name of
the files where the searching of the file is implemented in 'find'. I
would also like to add that the code I need to write should be in C.
Or, scripts are not allowed. Any other suggestions are welcome too.
Regards.
Pradip.
May I suggest that <comp.lang.c> is where you
want to be?
And that you have some serious study ahead of
you?
No one here, or even on <comp.lang.c>, is going
to re-write find for you. And even if they gave you
some good advice, you wouldn't be able to understand it.
Good grief!