LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-04-2010, 03:57 PM   #1
andben
LQ Newbie
 
Registered: Jul 2010
Posts: 2

Rep: Reputation: 0
Search for string in new files


Hi,
I would like a command or a bash script that search all files in all sub, modified last 12 hours, in /var/logs and that contains "alfa" or "bravo". The output should be filename of the file or files that contains that.

I have tried different combinations of date, grep, egrep and find. Any ideas is welcome. Thanks!
 
Old 07-04-2010, 04:11 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by andben View Post
I have tried different combinations of date, grep, egrep and find.
Please post what variations you tried, that way it will be easier for us to correct you.
 
Old 07-05-2010, 01:24 AM   #3
andben
LQ Newbie
 
Registered: Jul 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Hi,
I am testing this with a Smoothwall firewall. I have activated IMspector and it generate a file for each chat named something like ****2010-07-05***. I would like to seach any file, created today, in any subfolder of /var/log/IMspector/MSN/ for a keyword, like "nuclear".

I did test "find /var/log/imspector/ -mtime -1" to find all files created today. I did test "date +%Y-%m-%d" to get a correct dateformat, to use when I search for files. I also tried "egrep -l -w 'nuclear|chemical' -r *" to get all files with the keywords.

But I cant get it to work together.

Thanks
 
Old 07-05-2010, 02:09 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You missed just a little step: pass the output of the find command as argument to grep. You can accomplish this in two ways: using the -exec action of find or piping the results to xargs. The difference is that -exec executes the command once for each file, whereas xargs takes all the files as argument and spawns a single process. Depending on the number of files and the performance required you can choose either one of the following:
Code:
$ find . -mtime -1 -type f -name \*$(date +%Y-%m-%d)\* -exec grep -E -lw 'nuclear|chemical' {} \;
$ find . -mtime -1 -type f -name \*$(date +%Y-%m-%d)\* -print0 | xargs -0 grep -E -lw 'nuclear|chemical'
Regarding the date in the file name, as you can see shell's command substitution is your friend. Please, take a look at man xargs and at the GNU find manual for details.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help with recursive string-search in files alexzive Linux - Newbie 11 10-29-2009 01:10 PM
Need to search all files for string - strings command? jmur Programming 4 03-28-2008 02:35 PM
Search and replace string in executable files aalex77 Programming 2 05-26-2006 06:32 PM
Is it possible to search for a string from all the files on the harddisk? Akhran Linux - Newbie 8 09-13-2005 06:09 AM
how do i search files for a particular string? darkpark Linux - Newbie 4 07-05-2005 05:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:29 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