Skip to main content
Rephrase the edit to "respect the original intention of the author"
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

You can couple find with the -exec-exec argument. exampleExample:

find . -maxdepth 1 -type f -exec grep -H foo {} \;

This can be scaled, i.e. -maxdepth 2.

Edit

As mentioned in the [answer by @Stéphane Chazelas], it is advisable to restrict -type ffind option is usedto regular files so that grep does notgrep doesn't produce an error when itsthe argument is a path to a directory

-H{} grep optionactually is used to print a filename for every match (desired behavior when more than one file match)directory path:

find . -maxdepth 1 -type f -exec grep -H foo {} \;
  • -type f is a filter for find that limits the search results to files
  • -H is a grep option used to print a filename for every match (desired behavior when more than one file match)

You can couple find with the -exec argument. example:

find . -maxdepth 1 -type f -exec grep -H foo {} \;

This can be scaled, i.e. -maxdepth 2

-type f option is used so that grep does not produce an error when its argument is a path to a directory

-H grep option is used to print a filename for every match (desired behavior when more than one file match)

You can couple find with the -exec argument. Example:

find . -maxdepth 1 -exec grep foo {} \;

This can be scaled, i.e. -maxdepth 2.

Edit

As mentioned in the [answer by @Stéphane Chazelas], it is advisable to restrict find to regular files so that grep doesn't produce an error when the argument {} actually is a directory path:

find . -maxdepth 1 -type f -exec grep -H foo {} \;
  • -type f is a filter for find that limits the search results to files
  • -H is a grep option used to print a filename for every match (desired behavior when more than one file match)
-type f option is used so that grep does not produce an error when its argument is a path to a directory
Source Link

You can couple find with the -exec argument. example:

find . -maxdepth 1 -type f -exec grep -H foo {} \;

This can be scaled, i.e. -maxdepth 2

-type f option is used so that grep does not produce an error when its argument is a path to a directory

-H grep option is used to print a filename for every match (desired behavior when more than one file match)

You can couple find with the -exec argument. example:

find . -maxdepth 1 -exec grep foo {} \;

This can be scaled, i.e. -maxdepth 2

You can couple find with the -exec argument. example:

find . -maxdepth 1 -type f -exec grep -H foo {} \;

This can be scaled, i.e. -maxdepth 2

-type f option is used so that grep does not produce an error when its argument is a path to a directory

-H grep option is used to print a filename for every match (desired behavior when more than one file match)

Rollback to Revision 4
Source Link
Chris Down
  • 130.3k
  • 26
  • 277
  • 268

You can couple find with the -exec argument. example:

find . ! -name . -prune -typemaxdepth f1 -exec grep /dev/null foo {} +\;

This can be scaled, i.e. -maxdepth 2

You can couple find with the -exec argument. example:

find . ! -name . -prune -type f -exec grep /dev/null foo {} +

You can couple find with the -exec argument. example:

find . -maxdepth 1 -exec grep foo {} \;

This can be scaled, i.e. -maxdepth 2

Rollback to Revision 2 - Rollback to revision 2, this is a wiki, you should expect your answers to be improved. Meta question seems to have stagnated.
Source Link
Chris Down
  • 130.3k
  • 26
  • 277
  • 268
Loading
Stephane, I found your edit to be quite obstructive. Perhaps you should provide your own answer? please see http://meta.unix.stackexchange.com/questions/2702/proper-way-to-undo-an-edit-made-to-a-working-answer
Source Link
David Wilkins
  • 1.1k
  • 7
  • 16
Loading
Rollback to Revision 1
Source Link
David Wilkins
  • 1.1k
  • 7
  • 16
Loading
`! -name . -prune` being the standard equivalent of `-maxdepth 1 -mindepth 1`. Add `-type f` to avoid `grep` complaining on directories. Adding /dev/null to make sure the filename is always printed. No reason to use ; instead of + here.
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k
Loading
Source Link
David Wilkins
  • 1.1k
  • 7
  • 16
Loading