LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep & find search hangups (https://www.linuxquestions.org/questions/linux-newbie-8/grep-and-find-search-hangups-4175579365/)

maybebaby 05-09-2016 05:11 PM

grep & find search hangups
 
EXERCISE:
There is a file in the /etc directory which contains the line: default=0

Search only the /etc directory; only use ONE command and do not perform a recursive search.

Command used: ____________________________________________
Filename: _____________________________________

I am logged in as root. So far I tried:
  • 1ST ATTEMPT - grep "default=0" ./* (nothing happened)
  • 2ND ATTEMPT - find /etc "default=0" ./* (listed all the files in /etc and stated find: 'default=0': No such file or directory)
  • 3RD ATTEMPT - cd /etc;grep "default=0" ./* (which returned as ./grub.conf:default=0 BUT I am confused because technically I combined two commands on one line).

Since my command was cd /etc;grep "default=0" ./* is my filename going to be ./grub.conf:default=0 since that's what it returned from my command? AND Is there a way I can do this without using cd first?

smallpond 05-09-2016 05:29 PM

This works for me:

Code:

grep "default=0" /etc/*

keefaz 05-09-2016 06:52 PM

Quote:

Originally Posted by maybebaby (Post 5542854)
Since my command was cd /etc;grep "default=0" ./* is my filename going to be ./grub.conf:default=0 since that's what it returned from my command?

It's the file path + : + the line where grep found the matching term.

maybebaby 05-10-2016 01:40 PM

Quote:

Originally Posted by keefaz (Post 5542889)
It's the file path + : + the line where grep found the matching term.

I don't understand what you mean

keefaz 05-10-2016 01:47 PM

Quote:

Originally Posted by maybebaby (Post 5543361)
I don't understand what you mean

Code:


./grub.conf:default=0 
1. File path
2. :
3. line that contains the grep match


grail 05-10-2016 02:19 PM

As with your other question, you really need to start reading man pages for the commands you are using.

Turbocapitalist 05-10-2016 02:54 PM

Quote:

Originally Posted by maybebaby (Post 5542854)
Is there a way I can do this without using cd first?

In addition to reading the manual pages for grep and find, you'll want to read a little about the shell and in particular use of paths. Your shell is probably bash, but most any shell tutorial will be relevant and have a section on paths.

To get the manual pages for find and grep,

Code:

man grep
man find

The semicolon separates two 'commands' as if they were on separate lines. So your example with cd is really two on one line, not one long one. So the above could be written like this:

Code:

man grep; man find
Either way, look at the options -l and -h for grep. Read the paragraphs on them in the manual page and then try them.


All times are GMT -5. The time now is 11:24 AM.