LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-24-2009, 03:27 PM   #1
foampile
Member
 
Registered: Feb 2009
Posts: 50

Rep: Reputation: 15
Question find grep


is there any way to exclude unmounted devices when doing find/grep ? i am unable to search for a string in a file because it tries to go to /dev/tty*.

my find/grep command is:

find / -exec grep 'mystring' '{}' \; -print

and i get choked getting stuff like:

...
grep: /dev/ttyab: Input/output error
grep: /dev/ttya5: Input/output error
grep: /dev/ttyaa: Input/output error
grep: /dev/ttya9: Input/output error
grep: /dev/ttya8: Input/output error
grep: /dev/ttya7: Input/output error
grep: /dev/ttya6: Input/output error
grep: /dev/ttya4: Input/output error
grep: /dev/ttya3: Input/output error
grep: /dev/ttya2: Input/output error
grep: /dev/ttya1: Input/output error
....
 
Old 04-24-2009, 03:39 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by foampile View Post
is there any way to exclude unmounted devices when doing find/grep ? i am unable to search for a string in a file because it tries to go to /dev/tty*.

my find/grep command is:

find / -exec grep 'mystring' '{}' \; -print

and i get choked getting stuff like:

...
grep: /dev/ttyab: Input/output error
grep: /dev/ttya5: Input/output error
grep: /dev/ttyaa: Input/output error
grep: /dev/ttya9: Input/output error
grep: /dev/ttya8: Input/output error
grep: /dev/ttya7: Input/output error
grep: /dev/ttya6: Input/output error
grep: /dev/ttya4: Input/output error
grep: /dev/ttya3: Input/output error
grep: /dev/ttya2: Input/output error
grep: /dev/ttya1: Input/output error
....
Specify a more exact path, specify a partial name or bit of information, output to a file and search it, grep the output, find in and of itself does not support exclusions... and you don't need to use find at all for the way you're using it.

Maybe something like one of these would be better:

find /home -iname *.conf -exec grep "string" {} \;
cd /home && grep -iR "string" *.conf

If you really have zero idea of the name of the file, zero idea where its located, and need to use find on the whole drive you could grep -v the output to exclude the trash.

find / -exec grep "string" {} \; | grep -v "/dev" | grep -v "/proc" | grep -v "/mnt"

Or pipe it to a pager like less/more/vim.

find / -exec grep "string" {} \; | less
find / -exec grep "string" {} \; | vim -

Output it to a file

find / -exec grep "string" {} \; > ~/returned.information

or a combination of a variety of those items...

Last edited by rweaver; 04-24-2009 at 03:41 PM.
 
Old 04-24-2009, 03:39 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
My first reaction is that you should not try to search for files in /dev and other directories unless you actually give find a search criteria. Your current command says "run the exec string for every file and directory on the system."

Also, what happens if you do "grep keyword directory_name"?
 
Old 04-24-2009, 03:42 PM   #4
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 88
You could add -type f argument to the find command, to match only regular files, perhaps.
 
Old 04-24-2009, 05:16 PM   #5
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by Robhogg View Post
You could add -type f argument to the find command, to match only regular files, perhaps.
Good advice, but if nothing else he needs to provide some additional criterial rather than "everything". Be it "all files", "file name has x in it", "this /x/y location", etc... far too general.
 
Old 04-24-2009, 07:14 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,834

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
Use -prune in find to exclude directories.
It's in the manpage, but you'll have to play around to get the syntax correct.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Grep V/s Find justmovedtolinux Programming 3 10-02-2008 04:29 AM
using find /grep gamor Linux - Newbie 14 04-13-2005 03:36 PM
find with grep and replace it dominant Linux - Newbie 4 03-03-2004 02:11 PM
help me find grep-2.5.1 shanenin Linux - Software 1 01-23-2004 10:01 PM
trying to use find and grep, but don't know well enough. duhduhdude Linux - Newbie 9 03-22-2003 09:44 PM


All times are GMT -5. The time now is 04:57 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration