1

My requirenment is to Search the logs and find some specific entry. Log files are situated at two different paths. And I want to search in both.

Usually I login to server, go to the path and execute grep -i word *filename*

I want to prepare a script which will accept input from users for word and filename and search for it.

  • this should help http://stackoverflow.com/questions/16956810/finding-all-files-containing-a-text-string-in-linux – rakib_ Feb 18 '14 at 08:12

3 Answers3

3

You can search in as many paths as you want:

grep -i "word" *filename* /some/other/path/*filename*
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • _as many paths as you want_ - up to shell's maximum command line length. Possible easily reach when here is many logfiles with `/some/very/long/path/name/*files.log`. – clt60 Feb 18 '14 at 09:34
0

Something like this should help

#!/bin/sh
#Purpose: To find a given text in a file
if [ -z "$3" ]
then
  echo Usage: $0 DirectoryToSearchIn testToSearch filePattern  
  exit 1 
fi

find $1  -name '$3' | while read f
do   
  cat "$f" | grep "$3" && echo "[in $f]"
done
0

With find and grep you can do it over multiple directories (without knowning the directory name before)

find /path/to/files -type f -name '*filename*' -exec grep -i word /dev/null {} \;

This example finds files that contain filename (but could be anything) . I also added a sample path but you can use . in order to start the search in the current directories. The -type f returns files (not directories).

OR you can use recursive grep, as pointed out here.

grep -rnw 'directory' -e "*filename*"
Community
  • 1
  • 1
philshem
  • 24,761
  • 8
  • 61
  • 127
  • 1
    you should change `grep -i word {} \;` into `-exec grep -i word /dev/null {} \;` (ie, add "-exec ", and also provide an additionnal (empty) file to grep so that it will output the filename in which he found occurences of "word".) This is the portable way (ie, not using GNU grep additionnal options to always display the filename) – Olivier Dulac Feb 18 '14 at 08:27