Operating System - Linux
1752567 Members
5116 Online
108788 Solutions
New Discussion юеВ

Re: Keep find within a single directory

 
SOLVED
Go to solution
jdavee
New Member

Keep find within a single directory

My grep commands were working find until the number of files in the search directory exceeded the parameter size.

All the solution I find say to use find then pipe or -xargs the find output to grep -l -E .

That does work, but it has two problems.

First, it returns ./ instead of just . That's fixable by messing with the output.

But second and worse, find wants to drill down to lower subdirectories. I only want to return files from the single directory where I'm searching.

I've tried the -prune and -xdev options for find, but none effectively stop the subdirectories from being included in the search.

Is there any way to keep find in a single directory, or an alternative to using find to fix the original grep "paramter list too long" problem?

Thanks,
Jim
9 REPLIES 9
Jeff_Traigle
Honored Contributor

Re: Keep find within a single directory

I think -prune is the option you want on find.

Another option would be to put your file list in a variable (could be read in from a file or from output of an ls command of some sort) and do a for loop that would grep each file individually.
--
Jeff Traigle
Kent Ostby
Honored Contributor

Re: Keep find within a single directory

how about using :

ll | grep "^\-"| awk '{print $NF}' | xargs

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Pete Randall
Outstanding Contributor

Re: Keep find within a single directory

I'll confirm: prune is what you want.


Pete

Pete
James R. Ferguson
Acclaimed Contributor
Solution

Re: Keep find within a single directory

Hi Jim:

The key is to leverage both the '-path' and the '-prune' options of 'find':

# find /home/jrf -path "/home/jrf/*" -prune -type f -exec grep "ARY" /dev/null {} \;

...would search for files in '/home/jrf' without descent into subdirectories, and 'grep' for the pattern "ARY". All occurances found would be printed with the filename in front.

Regards!

...JRF...
jdavee
New Member

Re: Keep find within a single directory

Thanks everyone for the prompt replies!

The -prune and -path options did the trick to restrict the find command to the current directory:

cd
find . -path "./*" | xargs grep -l -E

I'll also try the ls suggestion.

Thanks!
Jim
Daavid Turnbull
Frequent Advisor

Re: Keep find within a single directory

I have always said that anybody who knows and understands all the arguments to find and can use them in anger has achieved minor guru status in the Unix world. ;-)
Behold the turtle for he makes not progress unless he pokes his head out.
Muthukumar_5
Honored Contributor

Re: Keep find within a single directory

You can get the files in the current directoy as,

# ls -l | awk '/^-/ { print $9; }'

To grep into the file with find command then use syntax as,

# find -type f -grep 'pattern' {} \+

or

# find -type f | xargs grep 'pattern'


There is another problem in your requiremnet. Grep will create messy messages when grepping with binary file??

You can remove that with file option as,

for file `find `
do
file $file | grep -q 'executable'
if [[ $? -ne 0 ]]
then
grep 'pattern' ${file}
fi
done

-Muthu
Easy to suggest when don't know about the problem!
dirk dierickx
Honored Contributor

Re: Keep find within a single directory

i always use the 'depth' parameter for this instead of prune.
jdavee
New Member

Re: Keep find within a single directory

Followup:

The first ls suggestion didn't work well, but the find suggested by James Ferguson got implemented in my app easily.

The app itself is a Java UI running on a Windows server that connects to HP-UX data via CIFS. Searches over the CIFS connection for files with specific content were simply impossible using any Windows search tool (and we tried almost a dozen), so my Java app constructs a short Unix script, sends it to the HP-UX server, executes it, collects the results (they were piped to a file), and reformats into a simple Java grid table for file name, date, size, path, etc.

It now works with larger directory sizes thanks to using the find command.

Thanks again to everyone for the prompt replies plus options in case one didn't work!

Jim