LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-25-2009, 01:32 PM   #1
mkyp
LQ Newbie
 
Registered: Sep 2009
Posts: 1

Rep: Reputation: 0
find: missing argument to `-exec'


I'm having a problem with -exec on the find command.

I would like to get a count of a particular string in each file.

I tried:
logs> find . -name "*access*2009.09*" -exec grep aString {} | wc \;
find: missing argument to `-exec'
wc: ;: No such file or directory
How do I pipe the output of grep into another command using the find -exec?

NOTE: I tried the following, but that gives the wc for all of the files, not for EACH file.
logs> find . -name "*access*2009.09*" -exec grep aString {} \; |wc
1257 30271 830725
 
Old 09-25-2009, 08:10 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
You could do away with find for your example:

Code:
grep -c aString . -R --include="*access*2009.09*"
For exec commands, you can place complex sequences inside a script file, and just '-exec sh script.sh', or do it as a one liner, eg:

Code:
find . -name "*access*2009.09*" -print -exec sh -c 'grep aString $0 | wc' {} \;

Last edited by neonsignal; 09-26-2009 at 03:33 AM.
 
Old 09-25-2009, 09:57 PM   #3
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Code:
find . -name "*access*2009.09*" -exec sh -c "grep aString '{}' | wc" \;
can't use pipes within find -exec unless you spawn a shell.
 
Old 09-26-2009, 02:49 AM   #4
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by mkyp View Post
I'm having a problem with -exec on the find command.

I would like to get a count of a particular string in each file.

I tried:
logs> find . -name "*access*2009.09*" -exec grep aString {} | wc \;
find: missing argument to `-exec'
wc: ;: No such file or directory
How do I pipe the output of grep into another command using the find -exec?

NOTE: I tried the following, but that gives the wc for all of the files, not for EACH file.
logs> find . -name "*access*2009.09*" -exec grep aString {} \; |wc
1257 30271 830725
Code:
How do I pipe the output of grep into another command using the find -exec?
Why are you assuming "-exec" is the solution to your problem? It isn't. Do it this way:

Code:
search="search-spec"
path="/path/of/interest"

find $path | grep -iP "\.html?$" | while read filepath
do
   count=$(grep -c "$search" < $filepath)
   if [ $count -gt 0 ]
   then
      echo "$count occurrence(s) of \"$search\" found in \"$filepath\""
   fi
done
Rule 1: Do not ever use the "-exec" option from "find". It is an express path to the dark side. This forum contains any number of endless threads from people trying to make "-exec" work for them, and all that is resolved is that some people sound extremely clever in creating incomprehensible, untrustworthy one-liners, while others (e.g. newbies) end up with their problems still unresolved (people just like you).

Rule 2: If you do not understand how it works, do not use it. Computer science is not about applying magic solutions to ordinary problems, it is about applying ordinary solutions to ordinary problems.

The above script is very easy to understand, and is certainly more robust and reliable than anything that uses -exec. It works this way:

1. Recursively search for files along a specified path.
2. Filter by a specified filename suffix (you may want to edit this to suit your own needs).
3. Read the resulting file paths -- this method correctly handles paths with embedded spaces.
4. Read the file itself and get a total of the number of occurrences of the search string in each file.
5. Test the result and only print file paths that yielded successful searches.

Many people in forums like this cannot resist saying, "But I can write that in one line, and everyone will think I'm a genius!" To which I reply:

1. Any program can be written in one line.
2. The fact that (1) is true is not by itself a justification for actually doing it.
3. One-line programs, originally incomprehensible only to their victims, eventually become incomprehensible to their authors.

Notwithstanding the valiant efforts of many candidates, computer programming isn't a priesthood. Knowing how to code should eventually become more like knowing how to write.
 
Old 10-09-2009, 11:13 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by mkyp View Post
Code:
find . -name "*access*2009.09*" -exec grep aString {} | wc \;
try
Code:
find . -name "*access*2009.09*" -exec grep aString {} \| wc\;
find . -name "*access*2009.09*" -exec 'bash eval grep aString' '{}' '| wc' \;
the latter might be simpler by removing eval, not sure.
 
  


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
Find with -exec argument - not giving proper output..how to find... hinetvenkat Linux - Server 4 01-25-2010 06:19 AM
find: missing argument to `-exec' pavodive Linux - Newbie 4 10-05-2009 02:24 PM
freeradius missing exec ??? hackintosh Linux - Networking 0 06-26-2008 01:54 AM
find -exec syntax: bug in find? mfcarroll Programming 5 06-21-2007 07:13 PM
Kylix (delphi) missing exec command? RayJ Programming 2 09-24-2003 12:35 AM

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

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