LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2007, 10:00 AM   #1
alenD
Member
 
Registered: Jul 2005
Posts: 69

Rep: Reputation: 15
strange behavior of find or grep


Hi all
I am trying to find a string in the files using:
file . -name '*' | xargs grep 'mystring'
this works fine except when I try to enter /etc (i.e. find /etc -name '*' | xargs grep 'mystring' )
it would enter the endless loop and in many cases would not show the string even if I know it is there. What is the problem here?
Thanx
 
Old 09-20-2007, 10:13 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Maybe some of your files have spaces or other white space in the file name, which means xargs treats them as two separate file names, not one. This is unusual for files in /etc however. If this is the case, you can ask find to de-limit output with the NUL character (using the -print0 option), and tell xargs to split on NUL (the -0 option).

Also, grep does not like to grep directores - you will want to tell find to only print files, not directories, using "-type f", and you may also want find to follow symbolic links (it does not by default, so if the files you are interested in are in a linked directory, they will be skipped - use -follow).

Putting it all together we have:
Code:
find /etc -type -f -follow -print0 |xargs -0 grep 'mystring'
Another, much simpler way to achieve much the same thing, is to tell grep to recurse into sub-directories:
Code:
grep -r mystring /etc
 
Old 09-20-2007, 10:49 AM   #3
alenD
Member
 
Registered: Jul 2005
Posts: 69

Original Poster
Rep: Reputation: 15
Thanx alot. It worked.
 
Old 09-21-2007, 05:31 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
-name '*'

is superfluous!

(almost, apart from hidden files)
 
Old 09-21-2007, 06:35 AM   #5
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
/etc has all kinds of stuff in it that problematic for grep. Whenever I'm searching for something in files in /etc, I use:

# find /etc -type f | xargs grep 'blah'

don't need anything else on the find.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange file behavior nc3b Linux - General 2 08-23-2006 01:27 PM
gaim: strange behavior Oliv' Linux - Software 2 03-28-2005 12:14 PM
Very Strange Behavior raysr Mandriva 4 08-31-2004 02:06 PM
Strange Behavior andrewb758 Linux - Hardware 5 08-31-2003 02:42 PM
strange behavior abhijit Linux - General 3 07-09-2003 11:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration