This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes Grep in a directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Grep in a directory" Watch "Grep in a directory" New topic
Author

Grep in a directory

Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
I have a directory in which there are lots of logs(*.gz). I have to search for particular text in those logs.
I want to know how to write that script in the terminal.
I tried something but it does not seem to be correct syntax.
i.e


Could someone help me with this?
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

This link has an explanation on how to look for files where the name matches a certain pattern,
and how to find those files in that pattern that contain a certain string.

By combining the -name and -exec options, you can achieve your requirement.


OCUP UML fundamental and ITIL foundation
youtube channel
Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
Actually my friend showed wrote a script in the termal and it had for loop with ls , zrep with the search test ...
Can you tell me how to do that in that way...
It looked simple as the one you pointed out

Thanks
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

Sony Agrawal wrote:Actually my friend showed wrote a script in the termal and it had for loop with ls , zrep with the search test ...
Can you tell me how to do that in that way...
It looked simple as the one you pointed out

Thanks
It would be great if you ask your friend to show the solution again, and to share that info with us.
Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

You can and should directly use the filepattern:

instead of the way over ls, which is possible, but errorprone.

This version will fail whith fancy characters like blanks or newlines in filenames.

In allmost all cases 'ls' hasn't anything to do in scripts.

An even more simple solution will be:



http://home.arcor.de/hirnstrom/bewerbung
Deepak Mahalingam
Greenhorn

Joined: Jul 09, 2010
Posts: 12
the answer is pretty simple.

just type : find ~ -name "*.gz" -exec zcat {} \; | grep -n "hello"

this will find all the files having the .gz extension from the home directory and the it will execute the zcat command on those files. on that result we can do a grep that will return the line numbers of the pattern "hello". This will surely work.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

[quote=Deepak Mahalingam]the answer is pretty simple.

just type : find ~ -name "*.gz" -exec zcat {} \; | grep -n "hello"

this will find all the files having the .gz extension from the home directory and the it will execute the zcat command on those files. on that result we can do a grep that will return the line numbers of the pattern "hello". This will surely work.[/quote]

I don't quiet understand why you send a solutions 2 months later, which is - pardon - inferior in 5 aspects:

- linenumbers wheren't a requierement
- we searched for Hello (uppercase) all the time
- zgrep was already introduced
- find will search in subdirectories
- ~ will search in the home, not in the current directory
Deepak Mahalingam
Greenhorn

Joined: Jul 09, 2010
Posts: 12
@Stefan: If posting a reply after 2 months is wrong, I have no idea why the discussion is still open and available:)
Anyway, my idea is not to give the exact solution, rather my idea was to give a hint, or idea so that based on the solution provided, the person can build up further.

your comments like 'line numbers were not needed, ~ searched from home, 'etc. were not very mature. Anyway, I however really loved your point that 'find searches the subdirectories also'. Now, that was a really good point that you had there.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Deepak Mahalingam wrote: If posting a reply after 2 months is wrong, I have no idea why the discussion is still open and available:)

Because discussions are open in general. Maybe there is some need to come back to an older thread.

Anyway, my idea is not to give the exact solution, rather my idea was to give a hint, or idea so that based on the solution provided, the person can build up further.

Yes, and I would consider that a good habit, if better solutions and solutions for the problem mentioned haven't been posted.

Or if you would have introduced your solution with it's advantage: "In case you're interested in searching the subdirs, consider using find like that ..."

your comments like 'line numbers were not needed, ~ searched from home, 'etc. were not very mature. Anyway, I however really loved your point that 'find searches the subdirectories also'. Now, that was a really good point that you had there .


I'm glad to learn here from your mature comments.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

Guys, I'd like you to keep this thread on topic. You have stopped talking about the problem domain and started to comment each other's posting style.
This is not the forum to do that. Back to the subject please.
Deepak Mahalingam
Greenhorn

Joined: Jul 09, 2010
Posts: 12
you can use the -maxdepth option in 'find' to restrict the level of search. this will thus search only your current directory and perform the action on the zipped files.
 
 
subject: Grep in a directory
 
Similar Threads
Create a file at runtime
where is WSAD log directory ?
$CATALINA_HOME/conf, $CATALINA_HOME/work, $CATALINA_HOME/logs
problem writing tomcat valve
File.renameTo() Failing On Solaris