LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-03-2015, 07:43 AM   #1
Nitron
LQ Newbie
 
Registered: Oct 2015
Posts: 12

Rep: Reputation: Disabled
Find files using grep and find


I need to find all text files with extension .log, which have the last access more than 24 hours ago and contain the required text.

I am new to linux. I can partly use find and grep command. But in task above I need to use both of this commands. My brain has broken.
Help me o_O please
 
Old 10-03-2015, 08:11 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825
Why do you need to use both? What have you tried and where are you stuck? You will need to show examples of the files you need to find as in the current state I see no complexity.
 
Old 10-03-2015, 08:53 AM   #3
jmccue
Member
 
Registered: Nov 2008
Location: US
Distribution: slackware
Posts: 218

Rep: Reputation: 106Reputation: 106
I am not sure if this is a homework question or not. But maybe a hint ? do
Code:
man find
and there is a option to list just specific name of files. There are also some options related to the age of the files.
 
Old 10-03-2015, 09:15 AM   #4
Nitron
LQ Newbie
 
Registered: Oct 2015
Posts: 12

Original Poster
Rep: Reputation: Disabled
Ok. Here is What I have already tried:

find / *.log -mtime +1 -print | grep "next" *.log

but the second part doesn't work. And I am not sure in my attempt.
 
Old 10-03-2015, 11:41 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825
If you are feeding output to grep via the pipe then that is where grep will gets its data from. If you are trying to grep data from within a file, which is what the part after grep is doing,
then you need to explain further??

As an example, are you trying to find all files within the last 24 hours ending in .log which contain the string 'next'?
 
Old 10-03-2015, 01:40 PM   #6
Nitron
LQ Newbie
 
Registered: Oct 2015
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post

As an example, are you trying to find all files within the last 24 hours ending in .log which contain the string 'next'?
Exactly!
 
Old 10-03-2015, 04:49 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825Reputation: 2825
So I think you need something like:
Code:
find / -mtime +1 -name '*.log' -exec grep -l 'next' {}\;
 
1 members found this post helpful.
Old 10-04-2015, 01:36 AM   #8
Nitron
LQ Newbie
 
Registered: Oct 2015
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thanks a lot! I have almost forgotten about option -exec.
 
Old 10-04-2015, 01:37 AM   #9
Nitron
LQ Newbie
 
Registered: Oct 2015
Posts: 12

Original Poster
Rep: Reputation: Disabled
Could you explain,please, what these options mean {}\ ????

Last edited by Nitron; 10-04-2015 at 01:59 AM.
 
Old 10-04-2015, 02:39 AM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,027
Blog Entries: 1

Rep: Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112
Quote:
Originally Posted by Nitron View Post
Could you explain,please, what these options mean {}\ ????
Since no one else seems to be in at the moment...

The {} is replaced with each filename found by find, the \; hides the ';' from the shell so that it is passed to the exec command line instead of being interpreted by the find process.

For completeness, from man find:

Quote:
-exec command ;
Execute command; true if 0 status is returned. All following arguments to find are taken to be
arguments to the command until an argument consisting of `;' is encountered. The string `{}' is
replaced by the current file name being processed everywhere it occurs in the arguments to the
command, not just in arguments where it is alone, as in some versions of find. Both of these
constructions might need to be escaped (with a `\') or quoted to protect them from expansion by
the shell. See the EXAMPLES section for examples of the use of the -exec option. The specified
command is run once for each matched file. The command is executed in the starting directory.
There are unavoidable security problems surrounding use of the -exec action; you should use the
-execdir option instead.

Last edited by astrogeek; 10-04-2015 at 02:40 AM. Reason: tpos, typs, typos
 
2 members found this post helpful.
Old 10-04-2015, 06:39 AM   #11
Nitron
LQ Newbie
 
Registered: Oct 2015
Posts: 12

Original Poster
Rep: Reputation: Disabled
It is a great pleasure to be the member of this forum. I have never had so complete and comprehensive answers on other communities!!!
 
Old 10-04-2015, 12:59 PM   #12
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,027
Blog Entries: 1

Rep: Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112
Quote:
Originally Posted by Nitron View Post
It is a great pleasure to be the member of this forum. I have never had so complete and comprehensive answers on other communities!!!
It is equally a pleasure to help new members who actually want to learn and understand!

You are welcome - and welcome to LQ!
 
  


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
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 9 06-30-2016 06:30 PM
[SOLVED] grep: find files that do not have multiple different strings kmkocot Linux - Newbie 4 04-22-2015 01:55 PM
[SOLVED] how to use cp find and grep together to copy a list of files using find with grep babhijit Linux - Newbie 10 07-03-2013 01:25 PM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 06:55 PM
searching within files. cat grep find w3bd3vil Linux - Newbie 4 10-13-2008 04:59 AM


All times are GMT -5. The time now is 04:48 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration