text serach in unknown file

Can any one tell me if there is an easy way to find a text in an unknown file name in a dierectory.

I'm trying to search for the text "where is john going" from a file
whose name I do not even know. There are about 1000 files in that directory and one of the files has this text in it.

Can any one tell me how I can achive this?

Thanks,
Ted

ls | xargs grep -l "where john is going"

And if they're in more than one directory, you could use this in the top directory:

find . -exec grep -l "where john is going" {} \;

cd /into/your/dir

grep "where john is going" *

Regs David

That is guaranteed to fail on most systems. You are asking the shell to replace * with a list of "about 1000 files". A command line must be less than LINE_MAX which is often 2048.

Hi perderabo,

You're right. In this case it would have been ok probably as it are only 1000 files, but in case of extreme large directories, this indeed fails.
Better to stick to the other solutions then, or just remember that it might fail once.

Regs David

Since the files reside in the same directory they must have unique names. There are only 254 values that can reside in a byte used as a filename. After you max out the 1 character filenames, you will need 746 two character filenames. 746 * 2 + 254 = 1746. And now you need 999 spaces to separate them... 1746 + 999 = 2745. So if you're willing to tolerate a lot of unprintable filenames, you can get 1000 names into a list that is 2745 bytes long, but that is the absolute minimum. And you still need 2 bytes to have a one character command.

This is not probably ok, it is guaranteed to fail on a system with LINE_MAX set to 2048.

Hi! Perderabo,

Excellant Analysis..

Perderabo,

How do you get 746 files for the 2 character names? 254 *2 is 508...

___________________________________
one other way that is a simple loop...

cd /dir/to/check/

for name in `ls *`
do
grep "where john is going" $name
done > file.out 2>err.out

This will capture the output in a file and any errors.

The thread that would not die... :smiley:

First, your "ls *" is going to have the same problem. There too many filenames to build the list all at once.

As for how I did the math. We want to build a list of 1000 filenames and we want the list to be as short as possible. We start by using as many 1 character filenames as possible. There are 256 possible values for a byte. But you can't use null for a filename. Nor can you use /. So that gives us 254 possible 1 character filenames. But we want 1000 filenames. 1000 - 254 = 746. We still need 746 more filenames. We will make those 2 characters long because we want the list as short as possible. At this point, we have (254 + (2 * 746)) = 1746 characters in our list of filenames. But we can't run the names together. We need a space between the names and that takes 999 spaces. So 1746 + 999 = 2748 is the shortest possible list of 1000 files.

Now here is a confession... my logic that jyotipg praised has a flaw. And the correct answer is actually 2751. But I won't tell why. I know that you'll enjoy finding my flaw on your own. :smiley:

ROFL

I just realized 1000 - 254 =756 hehe... sorry long day... basic math here...

Yes I realized that the ls * was the same mistake... after i posted it. That works better on directories where there are not normally 1000 files in the dir...

also, 1746 + 999 = 2745, not 2748 :smiley: You had that right in your previous post... +2 bytes for the command "l " gives you 2747... which was your first answer as well...

not sure I can see the flaw, Im kinda out of it today...

original post by kelas_magnum

also, 1746 + 999 = 2745, not 2748 You had that right in your previous post... +2 bytes for the command "l " gives you 2747... which was your first answer as well...

magnum,
you missed out adding 1 byte for the space between "ls" and "*". That makes it 2747 + 1 = 2748 as originally said by perderabo.

But I cant understand the mystery behind the magic number of "2751".. :smiley: