user2147075
user2147075

Reputation: 4201

How to perform grep operation on all files in a directory?

Working with xenserver, and I want to perform a command on each file that is in a directory, grepping some stuff out of the output of the command and appending it in a file.

I'm clear on the command I want to use and how to grep out string(s) as needed.

But what I'm not clear on is how do I have it perform this command on each file, going to the next, until no more files are found.

Upvotes: 420

Views: 727281

Answers (6)

ccalvert
ccalvert

Reputation: 4466

tldr:

grep -r --include=*.md --include=*.markdown --exclude-dir=node_modules "postgres"
grep -r -o --include=*.md --include=*.markdown --exclude-dir=node_modules "postgres"

I'm on Ubuntu using bash with GNU grep 3.7:

$ grep -V
grep (GNU grep) 3.7
Copyright (C) 2021 Free Software Foundation, Inc.

I wanted to search for anything in files with a .md or a .markdown extension.

The first example produced results like this:

elves/_posts/2023-09-11-postgres.markdown:postgres
elves/_heroku-guide/HerokuDataStarter.md:https://elements.heroku.com/addons/heroku-postgresql
elves/_heroku-guide/HerokuDataStarter.md:heroku addons:create heroku-postgresql:hobby-dev
vendor/bundle/ruby/3.0.0/gems/zeitwerk-2.6.7/README.md:# my_gem/db_adapters/postgresql.rb

The second:

elves/_posts/2023-09-11-postgres.markdown:postgres
elves/_heroku-guide/HerokuDataStarter.md:postgres
elves/_heroku-guide/HerokuDataStarter.md:postgres
vendor/bundle/ruby/3.0.0/gems/zeitwerk-2.6.7/README.md:postgres
vendor/bundle/ruby/3.0.0/gems/eventmachine-1.2.7/docs/GettingStarted.md:postgres
vendor/bundle/ruby/3.0.0/gems/zeitwerk-2.6.6/README.md:postgres

I found something like the following helpful:

#! /bin/bash

GREP='grep -r --include=*.md --include=*.markdown --exclude=bundle.js etc...'

Upvotes: 1

Narain
Narain

Reputation: 5421

In Linux, I normally use this command to recursively grep for a particular text within a directory:

grep -rni "string" *

where

  • r = recursive i.e, search subdirectories within the current directory
  • n = to print the line numbers to stdout
  • i = case insensitive search

Upvotes: 479

Noam Manos
Noam Manos

Reputation: 16971

To search in all sub-directories, but only in specific file types, use grep with --include.

For example, searching recursively in current directory, for text in *.yml and *.yaml :

grep "text to search" -r . --include=*.{yml,yaml}

Upvotes: 23

bryan
bryan

Reputation: 808

If you want to do multiple commands, you could use:

for I in `ls *.sql`
do
    grep "foo" $I >> foo.log
    grep "bar" $I >> bar.log
done

Upvotes: 2

Rob
Rob

Reputation: 11733

Use find. Seriously, it is the best way because then you can really see what files it's operating on:

find . -name "*.sql" -exec grep -H "slow" {} \;

Note, the -H is mac-specific, it shows the filename in the results.

Upvotes: 54

umi
umi

Reputation: 3892

grep $PATTERN * would be sufficient. By default, grep would skip all subdirectories. However, if you want to grep through them, grep -r $PATTERN * is the case.

Upvotes: 354

Related Questions